Basic Operators in Python

Originally published at https://www.geeksforgeeks.org

Python divides the operators in the following groups:

  • Arithmetic operators
  • Relational Operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Special operators

1 - Arithmetic operators:

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.

# Examples of Arithmetic Operator 
a = 9
b = 4

Addition of numbers 

add = a + b 

Subtraction of numbers 

sub = a - b 

Multiplication of number 

mul = a * b 

Division(float) of number 

div1 = a / b 

Division(floor) of number 

div2 = a // b 

Modulo of both number 

mod = a % b 

print results 

print(add) 
print(sub) 
print(mul) 
print(div1) 
print(div2) 
print(mod) 

Output:

13
5
36
2.25
2
1

2 - Relational Operators:

Relational operators compares the values. It either returns True or False according to the condition.

# Examples of Relational Operators 
a = 13
b = 33

a > b is False 

print(a > b) 

a < b is True 

print(a < b) 

a == b is False 

print(a == b) 

a != b is True 

print(a != b) 

a >= b is False 

print(a >= b) 

a <= b is True 

print(a <= b) 

Output:

False
True
False
True
False
True

3 - Logical operators:

Logical operators perform Logical AND, Logical OR and Logical NOT operations.

# Examples of Logical Operator 
a = True
b = False

Print a and b is False 

print(a and b) 

Print a or b is True 

print(a or b) 

Print not a is False 

print(not a) 

Output:

False
True
False

4 - Bitwise operators:

Bitwise operators acts on bits and performs bit by bit operation.

# Examples of Bitwise operators 
a = 10
b = 4

Print bitwise AND operation 

print(a & b) 

Print bitwise OR operation 

print(a | b) 

Print bitwise NOT operation 

print(~a) 

print bitwise XOR operation 

print(a ^ b) 

print bitwise right shift operation 

print(a >> 2) 

print bitwise left shift operation 

print(a << 2) 

Output:

0
14
-11
14
2
40

5 - Assignment operators:

Assignment operators are used to assign values to the variables.

6 - Special operators:

There are some special type of operators like

  • Identity operators-

is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

is          True if the operands are identical
is not True if the operands are not identical


# Examples of Identity operators 
a1 = 3
b1 = 3
a2 = ‘GeeksforGeeks’
b2 = ‘GeeksforGeeks’
a3 = [1,2,3] 
b3 = [1,2,3] 

print(a1 is not b1) 

print(a2 is b2) 

Output is False, since lists are mutable. 

print(a3 is b3) 

Output:

False
True
False
  • Membership operators

in and not in are the membership operators; used to test whether a value or variable is in a sequence.

in            True if value is found in the sequence
not in True if value is not found in the sequence


# Examples of Membership operator 
x = ‘Geeks for Geeks’
y = {3:‘a’,4:‘b’} 

print(‘G’ in x) 

print(‘geeks’ not in x) 

print(‘Geeks’ not in x) 

print(3 in y) 

print(‘b’ in y) 

Output:

True
True
False
True
False

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

Exponentiation Operator in JavaScript

Basic Operators in Java with Example

Top 5 Uses for the Spread Operator in JavaScript

Writing Your First Kubernetes Operator with Python and SDK



#python

Basic Operators in Python
5 Likes16.25 GEEK