Saturday, 6 August 2016

6.OPERATORS IN PYTHON


                                   Operators  In Python

Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.

Arithmetic operators:


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

Eg:
x = 15
y = 4
print('x + y = ',x+y)
print('x - y = ',x-y)
print('x * y = ',x*y)
print('x / y = ',x/y)
print('x // y = ',x//y)
print('x ** y = ',x**y)

o/p
x + y =  19
x - y =  11
x * y =  60
x / y =  3.75
x // y =  3
x ** y =  50625

Comparison operators:


Comparison operators are used to compare values. It either returns True or False according to the condition.


Eg:
x = 10
y = 12
print('x > y  is',x>y)
print('x < y  is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
Output
 
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 the and, or, not operators.

Eg:
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
x and y is False
x or y is True
not x is False
Here is the truth table for these operators.

Bitwise operators:


Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name. For example, 2 is 10 in binary and 7 is 111.
Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Assignment operators:

Assignment operators are used in Python to assign values to variables. a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left. There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.

Special operators:


Python language offers some special type 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 does not imply that they are identical.

x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
print(x1 is not y1)
print(x2 is y2)
Output
False
True

Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).

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 presence of key, not the value.

Eg:
 
x = 'Hello world'
print('H' in x)
print('hello' not in x)
Output
True
True
Here, 'H' is in x but 'hello' is not present in x (remember, Python is case sensitive).






No comments:

Post a Comment