Operators In python
Types of Operators:
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Bitwise Operators
- Special Operators
For example:
>>>2+3 5
In the above example, we have done an arithmetic operation between values 2 and 3. The program output was 6.
Let us discuss different types of operators that are used in python.
ARITHMETIC OPERATORS:
These operators are responsible for performing arithmetic operations.
Example -1
x = 20 y = 3 # Output: x + y = 23 print('x + y =',x+y) # Output: x - y = 17 print('x - y =',x-y) # Output: x * y = 60 print('x * y =',x*y) # Output: x / y = 6.666666666666667 print('x / y =',x/y) # Output: x // y = 6 print('x // y =',x//y) # Output: x ** y = 8000 print('x ** y =',x**y)
Output of example-1
x + y = 23 x - y = 17 x * y = 60 x / y = 6.666666666666667 x // y = 6 x ** y = 8000
COMPARISON OPERATORS:
Comparisons operators are used to comparing values. If the argument is True it returns True or it returns False.
Example -2
x = 11 y = 15 # Output: x > y is False print('x > y is',x>y) # Output: x < y is True print('x < y is',x<y) # Output: x == y is False print('x == y is',x==y) # Output: x != y is True print('x != y is',x!=y) # Output: x >= y is False print('x >= y is',x>=y) # Output: x <= y is True print('x <= y is',x<=y)
Output of example-2
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
LOGICAL OPERATORS:
Logical operators are also used to comparing values. If the argument is True it returns True or it returns False.
Example -3
x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x)
Output of example-3
x and y is False x or y is True not x is False
ASSIGNMENT OPERATORS:
Assignment operators are used in Python to assign values to variables.
BITWISE OPERATORS:
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.
SPECIAL OPERATORS:
Python language offers some special types of operators like the identity operator or the membership operator. They are described below with examples.
Identity operators:
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
Example -4
x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = [1,2,3] y3 = [1,2,3] # Output: False print(x1 is not y1) # Output: True print(x2 is y2) # Output: False print(x3 is y3)
Output of example-4
False True False
Membership operators:
in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).
In a dictionary, we can only test for the presence of a key, not the value.
Example -5
x = 'Hello world' y = {1:'a',2:'b'} # Output: True print('H' in x) # Output: True print('hello' not in x) # Output: True print(1 in y) # Output: False print('a' in y)
Comments
Post a Comment