Skip to main content

Python Operators

 
Operators:
 
Arithmetic Operators:
Perform basic arithmetic operations.

 Operators         

Description      

+

Addition   

-

Subtraction

*

Multiplication

/

Division

//

Floor Division (returns the integer part of the division result)

%

Modulo (returns the remainder of the division)

**

Exponentiation


For example:
a = 10
b = 3
print (a + b) # 13
print (a - b) # 7
print (a * b) #
30print (a / b) # 3.3333333333333335
print (a % b) # 1
print (a ** b) # 1000
print (a // b) # 3
 
Comparison Operators:
Compare values and return True or False.
 

 Operators  

Description

==

 Equal to 

 !=

 Not equal to 

 <

 Less than 

  >

Greater than

  <= 

Less than or equal to

  >= 

Greater than or equal to

 

For example:
x = 5
y = 10
print(x == y)   # False
print(x != y)   # True
print(x < y)    # True
print(x > y)    # False
print(x <= y)   # True
print(x >= y)   # False

Logical Operators:
Perform logical operations and return True or False.
 

 Operators  

Description      

and

 Logical AND 

or

 Logical OR

not

 Logical NOT


For example:
p = True
q = False
print(p and q)   # False
print(p or q)    # True
print(not p)     # False
 
Assignment Operators:
Assign values to variables and perform operations in a concise way.

 Operators  

Description

=

Assignment

+=

Add and assign

-=

Subtract and assign

*=

Multiply and assign

/=

Divide and assign

//=

Floor divide and assign

%=

Modulo and assign


 For example:
  x = 5
  y = 2
  x += y   # equivalent to x = x + y
  print(x)  # 7
 
Bitwise Operators:
Perform bitwise operations on integers.
 

 Operators 

Description

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

~

Bitwise NOT 

<< 

Left shift

>> 

Right shift


For example:
a = 5 # 0101 in binary 
b = 3 # 0011 in binary 
print(a & b) # 1 (0001 in binary) 
print(a | b) # 7 (0111 in binary)
print(a ^ b) # 6 (0110 in binary) 
print(~a) # -6 (complement of 0101 is 1010 in binary) 
print(a << 1# 10 (shift left by 1, 01010 in binary)
print(a >> 1# 2 (shift right by 1, 0010 in binary)
 
 
Membership Operators:
Test whether a value is a member of a sequence.
 

 Operators 

Description

in

 True if a value is found in the sequence 

not in

 True if a value is not found in the sequence


For example:
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)      # False
print(a is not b)  # True
 
 
Identity Operators:
Compare the memory locations of two objects.
 

 Operators  

Description

is

True if both variables point to the same object

is not

True if both variables do not point to the same object


For example:
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)      # False
print(a is not b)  # True
 
In Python, operators are special symbols or keywords that perform operations on operands. Operands can be variables, values, or expressions. Python supports various types of operators, including arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators. Here's an overview of the main types of operators in Python:
These operators play a crucial role in performing various operations and comparisons in Python. Understanding how to use them effectively is fundamental to writing clear and concise code.
 

 


Comments