H BKL GPQGWMX AMLy Vza 2 V1718707113936
H BKL GPQGWMX AMLy Vza 2 V1718707113936
H BKL GPQGWMX AMLy Vza 2 V1718707113936
Operators In Python
Operators in Python
Operators in Python are special symbols used to perform operations on variables
and values. Here is a comprehensive overview of the different types of operators
available in Python:
1. Arithmetic Operators
These operators perform basic arithmetic operations.
x = 5 + 3 # Output: 8
x = 5 - 3 # Output: 2
x = 5 * 3 # Output: 15
x = 5 / 2 # Output: 2.5
//(Floor Division): Divides the left value by the right and returns the integer
part.
x = 5 // 2 # Output: 2
Operators In Python 1
% (Modulus): Returns the remainder of the division.
x = 5 % 2 # Output: 1
x = 2 ** 3 # Output: 8
2. Comparison Operators
These operators compare two values and return a boolean result ( True or False ).
x = (5 == 3) # Output: False
x = (5 != 3) # Output: True
> (Greater than): Checks if the left value is greater than the right.
< (Less than): Checks if the left value is less than the right.
>= (Greater than or equal to): Checks if the left value is greater than or equal
to the right.
<= (Less than or equal to): Checks if the left value is less than or equal to the
right.
Operators In Python 2
x = (5 <= 3) # Output: False
3. Assignment Operators
These operators are used to assign values to variables.
x = 5 # x is now 5
+=(Add and assign): Adds the right value to the left variable and assigns the
result to the left variable.
x += 3 # x is now 8 (5 + 3)
-=(Subtract and assign): Subtracts the right value from the left variable and
assigns the result to the left variable.
x -= 2 # x is now 6 (8 - 2)
*=(Multiply and assign): Multiplies the left variable by the right value and
assigns the result to the left variable.
x *= 2 # x is now 12 (6 * 2)
/= (Divide and assign): Divides the left variable by the right value and assigns
the result to the left variable.
//= (Floor divide and assign): Performs floor division on the left variable by
the right value and assigns the result to the left variable.
Operators In Python 3
%= (Modulus and assign): Takes modulus using the left variable and the right
value and assigns the result to the left variable.
*= (Exponentiate and assign): Raises the left variable to the power of the right
value and assigns the result to the left variable.
4. Logical Operators
These operators are used to combine conditional statements.
Operators In Python 4