Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Operators


Operators

By using operators you can perform operations on the numeric values (operands).

Example
print(5 + 4)
9

In the above example, using the (+) plus operator, we have added two numeric values. In this case, the (+) plus is an operator, and numeric values (5 and 4) are operands.


Operators in Python

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Identity operators
  6. Membership operators
  7. Bitwise operators

Arithmetic Operators in Python

The arithmetic operators are used to perform mathematical operations in Python.

Addition

To perform addition on two or more numeric values, you can use the addition (+) operator.

Python uses the mathematical plus (+) sign to denote addition.

Example
print(5 + 4)
9

In the above example, we have added two integer numbers (4 and 5) by using the plus (+) mathematical sign, and the addition of the two numbers is displayed in the output (9).


Subtraction

To perform subtraction on two or more numeric values, you can use the subtraction (-) operator.

Python uses the mathematical minus (-) sign to denote subtraction.

Example
print(5 - 3)
2

In the above example, we have subtracted two integer numbers (5 and 3) by using the minus (-) mathematical sign, and the subtraction of the two numbers is displayed in the output (2).


Multiplication

To perform multiplication on two or more numeric values, you can use the multiplication (*) operator.

The asterisk (*) sign in Python denotes multiplication.

Example
print(5 * 3)
15

In the above example, we have multiplied two integer numbers (5 and 3) by using the multiplication operator (asterisk (*) sign), and the multiplication of the two numbers is displayed in the output (15).


Division

To perform division on numeric values, you can use the division (/) operator.

Python uses the forward slash (/) sign to denote division.

Example
print(6 / 3)
2

In the above example, we have performed division operations on integer numbers (6 and 3) by using the division operator (forward slash (/) sign), and the division of the numbers is displayed in the output (2).


Modulus

In Python, the modulus operator is used to get the remainder of a division.

Here, use the percentage (%) sign for modulus operation.

Example
print(10 % 2)
print(9 % 2)
0
1

In the above example, by using the modulus (%) operator we perform a modulus operation on the numbers 10 modulus by 2. Here in the output, the modulus result is 0.

Similarly, 9 modulus by 2, then the result is 1.


Exponentiation

In Python, you can use the double asterisk ** operator to perform exponential operations, which means raising a number to a certain power.

Here's how it works:

result = base ** exponent

Here, 'base' is the number you want to raise to a power, and 'exponent' is the power to which you want to raise it.

Example
print(2 ** 3)
8

In this example, the exponentiation 'result' equals 8, as 2^3 is 8.


Floor Division

In Python, floor division is done using double forward slashes (//).

It divides the left operand by the right operand and returns the largest integer less than or equal to the quotient.

You can use it when you want to obtain the whole number part of a division result without decimal point.

Example
print(7 // 3)
2

In the above example, the result will be 2, as it discards the decimal part of the division.