How To Do Math in Python 3 with Operators

How To Do Math in Python 3 with Operators

How To Do Math in Python 3 with Operators

Python is a popular programming language that has powerful mathematical capabilities. In this tutorial, we will learn how to use operators in Python 3 to do math.

Arithmetic Operators

Python has several arithmetic operators that allow us to perform basic math operations:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus: %
  • Exponentiation: **
  • Floor Division: //

Addition

To add two numbers in Python, we can use the + operator:

x = 5
y = 10
z = x + y
print(z) # Output: 15

Subtraction

To subtract two numbers in Python, we can use the - operator:

x = 5
y = 10
z = x - y
print(z) # Output: -5

Multiplication

To multiply two numbers in Python, we can use the * operator:

x = 5
y = 10
z = x * y
print(z) # Output: 50

Division

To divide two numbers in Python, we can use the / operator:

x = 10
y = 5
z = x / y
print(z) # Output: 2.0

Modulus

The modulus operator % returns the remainder of a division operation:

x = 10
y = 3
z = x % y
print(z) # Output: 1

Exponentiation

The exponentiation operator ** raises the first operand to the power of the second operand:

x = 2
y = 3
z = x ** y
print(z) # Output: 8

Floor Division

The floor division operator // returns the quotient of a division operation, rounding down to the nearest integer:

x = 10
y = 3
z = x // y
print(z) # Output: 3

Assignment Operators

In

Комментарии

Популярные сообщения из этого блога

How To Modify CSS Classes in JavaScript

How To Backup MySQL Databases on an Ubuntu VPS

How To Backup PostgreSQL Databases on an Ubuntu VPS