Operators in Python
Python : Operators
Operators are used to perform different operations like arithmetic, logical, and comparison operations on variables and values.
Python divides the operator in five major groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators:
These operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc.
a = 10
b = 5
c = a + b # addition operator
d = a - b # subtraction operator
e = a * b # multiplication operator
f = a / b # division operator
g = a % b # modulus operator (returns the remainder after division)
h = a ** b # exponentiation operator (returns a raised to the power of b)
Assignment Operators:
These operators are used to assign values to variables.
a = 10 # assigns the value 10 to variable a
b = 5 # assigns the value 5 to variable b
c += a # equivalent to c = c + a
d -= b # equivalent to d = d - b
e *= a # equivalent to e = e * a
f /= b # equivalent to f = f / b
Comparison Operators:
These operators are used to compare two values and return a Boolean value (True or False).
a = 10
b = 5
c = a == b # equal to operator (returns False as a is not equal to b)
d = a != b # not equal to operator (returns True as a is not equal to b)
e = a > b # greater than operator (returns True as a is greater than b)
f = a < b # less than operator (returns False as a is not less than b)
g = a >= b # greater than or equal to operator (returns True as a is greater than or equal to b)
h = a <= b # less than or equal to operator (returns False as a is not less than or equal to b)
Logical Operators:
These operators are used to perform logical operations such as AND, OR, and NOT.
a = True
b = False
c = a and b # AND operator (returns False as both a and b are not True)
d = a or b # OR operator (returns True as at least one of a or b is True)
e = not a # NOT operator (returns False as the value of a is True)
Bitwise Operators:
These operators are used to perform operations at the bit-level of the values.
a = 10
b = 5
c = a & b # bitwise AND operator (returns the result of ANDing the binary representation of a and b)
d = a | b # bitwise OR operator (returns the result of ORing the binary representation of a and b)
e = a ^ b # bitwise XOR operator (returns the result of XORing the binary representation of a and b)
f = ~a # bitwise NOT operator (returns the complement of the binary representation of a)
g = a << 2 # left shift operator (shifts the binary representation of a by 2 bits to the left)
h = a >> 2 # right shift operator (shifts the binary representation of a by 2 bits to the right)
Previous Article
Next Article
Python Tutorials
- Hello World
- Variables and Types
- Lists
- Tuple
- Basic Operators
- Strings
- Conditions
- Loops
- Functions
- Classes and Objects
- Dictionaries
- Map
- Filter
- Reduce
- Sets
- Decorators
- Generators
- Modules and Packages
- Numpy Arrays
- Pandas Basics
- List Comprehensions
- Lambda functions
- Multiple Function Arguments
- Partial functions
- Regular Expressions
- Exception Handling
- Serialization
- Code Introspection