H BKL GPQGWMX AMLy Vza 2 V1718707113936

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

🐍

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.

+ (Addition): Adds two values.

x = 5 + 3 # Output: 8

- (Subtraction): Subtracts the right value from the left.

x = 5 - 3 # Output: 2

* (Multiplication): Multiplies two values.

x = 5 * 3 # Output: 15

/ (Division): Divides the left value by the right.

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

* (Exponentiation): Raises the left value to the power of the right.

x = 2 ** 3 # Output: 8

2. Comparison Operators
These operators compare two values and return a boolean result ( True or False ).

== (Equal to): Checks if two values are equal.

x = (5 == 3) # Output: False

!= (Not equal to): Checks if two values are not equal.

x = (5 != 3) # Output: True

> (Greater than): Checks if the left value is greater than the right.

x = (5 > 3) # Output: True

< (Less than): Checks if the left value is less than the right.

x = (5 < 3) # Output: False

>= (Greater than or equal to): Checks if the left value is greater than or equal
to the right.

x = (5 >= 3) # Output: True

<= (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.

= (Assign): Assigns the right value to the left variable.

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.

x /= 3 # x is now 4.0 (12 / 3)

//= (Floor divide and assign): Performs floor division on the left variable by
the right value and assigns the result to the left variable.

x //= 2 # x is now 2.0 (4.0 // 2)

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.

x %= 2 # x is now 0.0 (2.0 % 2)

*= (Exponentiate and assign): Raises the left variable to the power of the right
value and assigns the result to the left variable.

x **= 3 # x is now 0.0 (0.0 ** 3)

4. Logical Operators
These operators are used to combine conditional statements.

and : Returns True if both statements are true.

x = (5 > 3 and 5 < 10) # Output: True

or : Returns True if one of the statements is true.

x = (5 > 3 or 5 > 10) # Output: True

not : Reverses the result, returns False if the result is true.

x = not(5 > 3) # Output: False

Understanding these operators and their usage is fundamental in Python


programming, as they form the basis for performing various operations and
building complex expressions.

Operators In Python 4

You might also like