Operators in Python (Part-1)
Operators in Python (Part-1)
XI – COMPUTER SCIENCE
Consider the
Operators are the expression
constructs which 4+5=9
can manipulate the 4 and 5 are called
value of operands. operands and + is
called operator
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
PYTHON OPEREATOR CLASSIFICATION
* / //
+ -
MULTIPLICATI FLOAT INTEGER
ADDITION SUBTRACTION ON DIVISION DIVISION
+
% ** -
Unary
REMAINDER EXPONENT Unary Minus
Operator +
Example 1 Example 3
a=20
#Precedence of * is more
x=7+3*2 b=2
#Precedence of ** is more than unary -
print(x) print(-a**b)
#Output : 13 #Output : -400
Example 2 Example 4
a = 20 a=20
b = 10 b=2
#Precedence of ** is more than /
print(400**1/2)
c = 15
d=5
e=0 #Precedence of ** is more
print(a/b**e) #Output : -200.0
#Output: 20.0 (Division
DEEPSHIKHA SETHI____CLASS XI __________AISresults
MAYUR VIHARin
float type)
TO DO ACTIVITY (FIND THE OUTPUT)
a=200
b=20
c=10
d=0 Output:
e=4
f=15.0 2.0
print(a/b//e) 201
print(a+b**d) 52.5
print((a+b-c)/(d+e)) 52
print((a+b-c)//(d+e)) 13.0
print(a//f) -3000.0
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
print(-a*f) -202.0
print(-a-b/c)
OPERATORS WITH SAME PRECEDENCE
9/5*4
9//5/5
9**4**2
9+5-2
print(2 ** 3 ** 2)
print(4.00/(2.0+2.0))
LET’S REVISE
num_int = 123
num_flo = 1.23 OUTPUT:
num_bool=True
a=num_int+num_flo
124.23 <class 'float'>
b=num_int+num_bool
print(a,type(a)) 124 <class 'int'>
print(b,type(b))
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
NOT ALL TYPES ARE PROMOTED
OUTPUT : TypeError
print(num_int+num_str)
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
We use the
predefined
In Explicit Type Conversion,
functions
users convert the data type
like int(), float(), s
of an object to required
tr(), etc to
data type.
perform explicit
type conversion.
a=122.0
Output:
b=22
c=a//b
5.0
print(c)
5.0 3
c=str(c)
print(c,len(c))