Operators are special symbols in python which are responsible for a particular operation between two or more operands.Operators are more helpful for doing operations between two operands.We will learn everything about different type of operators in python with examples.
e.g.
value of a+b= 11e.g.
a=5;b=6;
c=a+b
print("value of a+b= ",c)
Output of above program:-Descriptions:- In above program
operators --> =,+
operands --> a,b,c
There are many variety of operators present in python as follows:-
1.) Arithmetic operators:
Arithmetic operators are used to perform arithmetic operations like addition,
subtraction,multiplication and division etc.There are some arithmetic operators
as follows:-
e.g.
value of c= 31
value of d= 19
value of e= 150
value of f= 4.166666666666667
value of g= 1
value of h= 4
2.) Comparison operators:-a=25
b=6
#addition
c=a+b
print("value c= ",c)
#substraction
d=a-b
print("valueof d= ",d)
#multiplication
e=a*b
print("value of e= ",e)
#division
f=a/b
print("value of f= ",f)
#modulous
g=a%b
print("value of g= ",g)
#floor division
h=a//b
print("value of h= ",h)
Output of above Python program:- value of c= 31
value of d= 19
value of e= 150
value of f= 4.166666666666667
value of g= 1
value of h= 4
Comparison operators are used to compare the value of two operands and returns Boolean value True or False according to the condition.
There are some comparison operators as follows:-
a=25
b=20
#Equal to
c=a==b
print("value a==b",c)
#greater than
d=a>b
print("value a>b",d)
#less than
e=a<b
print("value a<b",e)
#greater than eual to
g=a>=b
print("value a>=b",g)
#less than equal to
h=a<=b
print("value a<=b",h)
Output of above python program:-value a==b False
value a>b True
value a<b False
value a>=b True
value a<=b False
3.) Logical operators :-
In python,AND ,OR & NOT operators are called logical operators.This is used primarily in the expression evaluation to make a decision.
a=25
b=20
#Equal to
c=a==b
print("value a==b",c)
#greater than
d=a>b
print("value a>b",d)
#less than
m=a=b
print("value a>=b",g)
#less than equal to
h=a<=b
print("value a<=b",h)
Output of above python program:value of a and b= False
value of a or b= True
value of not a= False
value of not b= True
value of a and b= 0
value of a or b= 1
value of not a= False
value of not b= True
4.) Assignment operators :-
The assignment operators are used to assign value to variables in python.It is used to assign the value of right operand to left operand.
e.g.
a = 5
(left operand)=(right operand)
5.) Bitwise operators:-The Bitwise operators perform bit by bit operation on the values of the two operands.
e.g.
We have to know truth table of AND ,OR,NOT & XOR first.It is given as below:-
AND (Y=AB):-
OR (Y=A+B):-
NOT (Y=~A):-
XOR(Y=AB + AB):-
Binary value = 1 1 1 1 1 1
Decimal value= 2^5+ 2^4 +2^3+2^2+2^1+ 2^0
e.g.
a=10 #(0000 1010 binary value)
b=5 #(0000 0101 binary value)
print("value of a&b=",a&b)
print("value of a|b=",a|b)
print("value of ~a & ~b=",~a,~b)
print("value of a⊕b=",a^b)
print("value of a⊕b=",a^b)
print("value of right shift a>>2=",a>>2)
print("value of right shift b>>2=",b>>2)
print("value of left shift a<<2",a<<2)
print("value of left shift a<<2",a<<2)
Output of above python program:-value of a&b= 0
value of a|b= 15
value of ~a & ~b= -11 -6
value of a⊕b= 15
value of a⊕b= 15
value of right shift a>>2= 2
value of right shift b>>2= 1
value of left shift a<<2 40
value of left shift b<<2 20
6.) Membership operators:-
python membership operators are used to check the membership of value inside a python data structure.If the value is present in data structure,then returns True otherwise False.
e.g.
a='welcome to MNT LAB'
b={1:'x',2:'y'}
print('w' in a) #output True
print('come' in a) #output True
print('MNT' in a) #output True
print('msdotnet' in a) #output False
print(1 in b) #output True
print(3 in b) #output False
print(2 in b) #output True
print('x' in b) #output False because it apply on key not value
True
True
True
False
True
False
True
False
7.) Identity operators:-
Identity operators are used to check if two variables (or values) are located on the same part of the memory.Two variables that are equal ,it does not imply they are identical.
e.g.
a=6
b=6
c='welcome'
d='welcome'
e=[4,5,6]
f=[4,5,6]
print(a is not b) #output False
print(a is b) #output Ture
print(c is d) #output True
print(c is not d) #output false
print(e is not f) #output True
print(e is f) #output false
Output of above python program:-False
True
True
False
True
False
Descriptions:-
- a&b are integers of same values,so this are equal as well as identical.
- c&d are strings of same values,so this is eual as well identical.
- e&f are list.It is equal but not identical because interpreter locates them separately in memory although they are equal.
The precedence of operators is more imported to find which operator should be evaluated first in any mathematical operation(arithmetic operation).The precedence table of operators in python given below:-
Special Notes:-I have explained some functions of time module in our previous post.You can read it previous post from here.In this notes we will learn gmtime() function of time module.
- gmtime()-->This function takes the argument in seconds and returns the struct time in UTC(universal time coordinated).If we passed zero argument in this function then it returns struct time of your current system time in UTC.There are many time zone present in the worlds.Each country have its own time zone.But they use only one UTC time. Asian and European Country have one time zone that is called Greenwich time(gmt) zone.British county uses BST (British summer time) zone. BST time is ahead one hour from Greenwich time .
#gmt=greenwich time,utc=universal time coordinated
import time
seconds=time.time()
second1=time.localtime()
greenwich_time=time.gmtime(1587537544) #(gmtime()function takes the aguments in
print("local time in UTC=\n",second1) #seconds and returns the struct_time in UTC)
print('')
print("Given time(seconds)in UTC=\n",greenwich_time)
print('')
print(" current system time in seconds",seconds)
Output of above python program:-#gmt=greenwich time,utc=universal time coordinated
import time
seconds=time.time()
second1=time.localtime()
greenwich_time=time.gmtime(1587537544) #(gmtime()function takes the aguments in
print("local time in UTC=\n",second1) #seconds and returns the struct_time in UTC)
print('')
print("Given time(seconds)in UTC=\n",greenwich_time)
print('')
print(" current system time in seconds",seconds)
Watch Lecture 11 complete video:-
0 comments:
Post a Comment