6 Operators
6 Operators
Operators
Operator is a symbol that performs certain operations.
Python provides the following set of operators
1. Arithmetic Operators
2. Relational Operators or Comparison Operators
3. Logical operators
4. Bitwise oeprators
5. Assignment operators
6. Special operators
1. Arithmetic Operators:
+ ==>Addition
- ==>Subtraction
* ==>Multiplication
/ ==>Division operator
% ===>Modulo operator
// ==>Floor Division operator
** ==>Exponent operator or power operator
1) a=10 Output: Eg:
2) b=2 1) Python test.py or py 1) a = 10.5
3) print('a+b=',a+b) test.py 2) b=2
4) print('a-b=',a-b) 2) a+b= 12 3)
5) print('a*b=',a*b) 3) a-b= 8 4) a+b= 12.5
6) print('a/b=',a/b) 4) a*b= 20 5) a-b= 8.5
7) print('a//b=',a//b) 5) a/b= 5.0 6) a*b= 21.0
8) print('a%b=',a%b) 6) a//b= 5 7) a/b= 5.25
9) print('a**b=',a**b) 7) a%b= 0 8) a//b= 5.0
8) a**b= 100 9) a%b= 0.5
10) a**b= 110.25
Eg:
10/2==>5.0
10//2==>5
10.0/2===>5.0
10.0//2===>5.0
Note: / operator always performs floating point arithmetic. Hence it will always
returns
float value.
But Floor division (//) can perform both floating point and integral arithmetic. If
arguments are int type then result is int type. If atleast one argument is float type
then
Relational Operators:
>,>=,<,<= We can apply relational Eg:
Eg 1: operators for str types also 1) print(True>True) False
1) a=10 Eg 2: 2) print(True>=True) True
2) b=20 1) a="durga" 3) print(10 >True) True
3) print("a > b is ",a>b) 2) b="durga" 4) print(False > True) False
4) print("a >= b is ",a>=b) 3) print("a > b is ",a>b) 5)
5) print("a < b is ",a<b) 4) print("a >= b is ",a>=b) 6) print(10>'durga')
6) print("a <= b is ",a<=b) 5) print("a < b is ",a<b) 7) TypeError: '>' not
7) 6) print("a <= b is ",a<=b) supported between
8) a > b is False 7) instances of 'int' and 'str'
9) a >= b is False 8) a > b is False
10) a < b is True 9) a >= b is True
11) a <= b is True 10) a < b is False
11) a <= b is True
Relational Operators:
Eg:
1) a=int(input("Enter First Number:"))
2) b=int(input("Enter Second Number:"))
3) print("Both numbers are equal" if a==b else "First Number is Less than Second Number" if
a<b else "First Number Greater than Second Number")
Output:
D:\python_classes>py test.py
Enter First Number:10
Enter Second Number:10
Both numbers are equal
D:\python_classes>py test.py
Enter First Number:10
Enter Second Number:20
First Number is Less than Second Number
D:\python_classes>py test.py
Enter First Number:20
Enter Second Number:10
First Number Greater than Second Number
Special operators:
Python defines the following 2 special operators
1. Identity Operators
2. Membership operators
1. Identity Operators
We can use identity operators for address comparison.
2 identity operators are available
1. is
2. is not
r1 is r2 returns True if both r1 and r2 are pointing to the same
object
r1 is not r2 returns True if both r1 and r2 are not pointing to the
same object
Special Operators
Eg: Eg: Eg:
1) a=10 1) a=“Ravi" 1)
2) b=10 2) b=“Ravi" list1=["one","two","three"
3) print(a is b) True 3) print(id(a)) ]
4) x=True 4) print(id(b)) 2)
5) y=True 5) print(a is b)- True list2=["one","two","three"
6) print( x is y) True ]
3) print(id(list1))
4) print(id(list2))
5) print(list1 is list2) False
6) print(list1 is not list2)
True
7) print(list1 == list2) True
Note:
We can use is operator for address comparison where as == operator for content
comparison.
2. Membership operators:
We can use Membership operators to check whether the given object present in the
given collection.(It may be String,List,Set,Tuple or Dict)
not in Retruns True if the given object not present in the specified Collection
Eg: Eg:
1) x="hello learning Python is very 1)
easy!!!" list1=["sunny",“daany",“honey","pinky"]
2) print('h' in x) True 2) print("sunny" in list1) True
3) print('d' in x) False 3) print("tunny" in list1) False
4) print('d' not in x) True 4) print("tunny" not in list1) True
5) print('Python' in x) True
Operator Precedence:
If multiple operators present then The following list describes operator
which operator will be evaluated precedence in Python
first is decided by () Parenthesis
operator precedence. ** exponential operator
Eg: ~,- Bitwise complement operator,unary
print(3+10*2) 23 minus operator
print((3+10)*2) 26 *,/,%,// multiplication,division,modulo,floor
Eg: division
1) a=30 +,- addition,subtraction
2) b=20
3) c=10 <<,>> Left and Right Shift
4) d=5 & bitwise And
5) print((a+b)*c/d) 100.0 ^ Bitwise X-OR
6) print((a+b)*(c/d)) 100.0 | Bitwise OR
7) print(a+(b*c)/d) 70.0 >,>=,<,<=, ==, != ==>Relational or Comparison
9) operators
10) 3/2*4+3+(10/5)**3-2 =,+=,-=,*=... ==>Assignment operators
11) 3/2*4+3+2.0**3-2 is , is not Identity Operators
12) 3/2*4+3+8.0-2 in , not in Membership operators
13) 1.5*4+3+8.0-2 not Logical not
14) 6.0+3+8.0-2 and Logical and
15) 15.0 or Logical or
Mathematical Functions (math Module)
• A Module is collection of functions, variables and classes etc.
• math is a module that contains several functions to perform mathematical operations
• If we want to use any module in Python, first we have to import that module.
• import math
• Once we import a module then we can call any function of that module.
• import math
• print(math.sqrt(16))
• print(math.pi)
• 4.0
• 3.141592653589793
• We can create alias name by using as keyword.
• import math as m
• Once we create alias name, by using that we can access functions and variables of that
• module
• import math as m
• print(m.sqrt(16))
• print(m.pi)
• We can import a particular member of a module explicitly as follows
• from math import sqrt
• from math import sqrt,pi
• If we import a member explicitly then it is not required to use module name while
• accessing.
• from math import sqrt,pi
• print(sqrt(16))
• print(pi)
• print(math.pi) NameError: name 'math' is not defined
Mathematical Functions (math Module)
• important functions of math module:
• ceil(x)
• floor(x)
• pow(x,y)
• factorial(x)
• trunc(x)
• gcd(x,y)
• sin(x)
• cos(x)
• tan(x)
• ....
• important variables of math module:
• pi3.14
• e===>2.71
• inf ==>infinity
• nan ==>not a number
• Q. Write a Python program to find area of circle
• pi*r**2
• from math import pi
• r=16
• print("Area of Circle is :",pi*r**2)
• OutputArea of Circle is : 804.247719318987