MITS Python Operators Expressions Final
MITS Python Operators Expressions Final
Mr. K. Chandran
Assistant Professor & Technical Trainer
Madanapalle Institute of Technology & Science
Topics to be Covered
• Introduction to Python
• Python Installation
• First Program in Python
• Data Types
• Variables
• Numbers
Introduction to Python
• Python is a general-purpose interpreted,
interactive, object-oriented, and high-
level programming language.
• It was created by Guido van Rossum
during 1985- 1990.
• Python got its name from “Monty
Python’s flying circus”. Python was
released in the year 2000.
Introduction to Python
x = 10
print(x)
print(type(x)
Y = 123.456
print(y)
X = “MITS Institute of Engineering and Technology”
print(x)
print(type(x)
Example of Python Variable
Run in Terminal
Python.demo.py
Output
10
int
123.456
MITS Institute of Engineering and Technology
str
Python - Statements
a=2, b=3
print (a,b)
temp = a
a=b
b = temp
print (a,b)
Python – TUPLE ASSIGNMENT
One way to think of tuple assignment is as tuple
packing/unpacking.
In tuple packing, the values on the left are ‘packed’ together in a
tuple:
>>> b = ("George", 25, "20000") # tuple packing
-In tuple unpacking, the values in a tuple on the right are
‘unpacked’ into the variables/names on the right:
>>> b = ("George", 25, "20000") # tuple packing >>>
(name, age, salary) = b # tuple unpacking
>>> name 'George'
>>> age 25
>>> salary
'20000'
Python – TUPLE ASSIGNMENT
-The right side can be any kind of sequence
(string,list,tuple)
Example:
-To split an email address in to user name and a domain
>>> mailid='[email protected]'
>>> name,domain=mailid.split('@')
>>> print name god
>>> print (domain) abc.org
Numbers of Python
as elif if or yield
print("Sum of ",first_number,"and",second_number,"is",add)
Sum of 10 and 20 is 30
print("Sum of %d and %d is %d"%(first_number,second_number,add))
Sum of 10 and 20 is 30
print("Sum of {} and {} is {}".format(first_number,second_number,add))
Sum of 10 and 20 is 30
• Format specifiers are also supported
• {%d,%f,%s}
name = 'Peter'
age = 23
print('%s is %d years old' % (name, age))
print('{} is {} years old'.format(name, age))
print('{name} is {age} years old')
Runtime Input
a=input("Enter a number")
print(a) // string data
b=input()
print(b) // string data
Runtime Input
• To convert the string data into specific
format, use the conversion method
a=int(input("Enter a number"))
print(a) // int data
b=float(input())
print(b) // float data
Numbers in python : int
345
3*82
4*81
5*80
192+32+5 = 229
(345)8 = (229) 10
Numbers in Python : int
Example -1 Example -2
a = 10,20 a = 10_20_30
print(a) print(a)
Output Output
(10,20) #tuple 102030
Example -3 Example -4
a = 10_20_; a = 0o84;
print(a) print(a)
Output Output: Syntax Error
Syntax Error
Example -5
print(oct(74))
Output: 112
Numbers in Python : float
Example :
123.56
3.142
-1.546
0.23
Numbers in Python : float
• The float beyond its max.size referred as
‘inf’,’Inf’,’INFINITY’ or infinity
Example
f=2e400
print(f)
inf
Numbers in Python : float
• Scientific Notation is used as a short representation to express floats having many digits
• Example
• a = 1e3
• print (a) # 1000.0
• a = 1e5
• print (a) # 100000.0
• a = 3.4556789e2
• print (a) # 345.56789
• a=5.678848
• print('{:.2f}'.format(a))
• #5.68
Numbers in Python : float
• To convert any type into float, use float( )
function
Calculation:
= 36 + 24j + 24j + (16*-1)
= 36 + 48j – 16
= 20 + 48j
Numbers in python : Rational number
Example:
a=Fraction('1.33') # 133/100 a=Fraction(‘-1.5') # -3/2
Numbers in Python – Rational Number
• Prototype '+'
• (int,int) -> int # 5 + 3 = 8
• (float,float)-> float # 5.6+2.3= 7.9
• +(int) -> int # +3 = 3
• +(float) -> float # +3.5=3.5
• +(complex) -> complex # +5j = 5j
• (complex+complex)->complex # 5j+3j = 8j
• Prototype '-'
• (int,int) -> int # 5 - 3 = 2
• (float,float)-> float # 5.6-2.3= 3.3
• -(int) -> -int # -3 = -3
• -(float) -> -float # -3.5= -3.5
• -(complex) -> -complex # -5j = -5j
• (complex-complex)->complex # 3j-5j = -2j
Arithmetic Operator {+,-}
Example -4 Example -5
a = 10++10 a = 10
print(a) a+20 = 10
Output print(a)
Output: 20 Output
Output: Syntax
Error
Arithmetic Operator {*}
• Multiplication(*) operator can be used
only in the binary category.
Prototype
Example
Uses
a =‘mits’
print(a*2) #mitsmits
Print(2*a) #mitsmit
Arithmetic Operators {‘/’}
Division(/) operator can be used only in the binary category.
Prototype
/(int,int) - > float
/(float,float) - > float
/(complex,complex) - > complex
Example
Protype
//(int,int) - > int
//(float,float) - > float(Without values)
Arithmetic Operators {‘//’}
• Example
• >>> a = 10//5 # prints a -> 2
• >>> a = -10//4 # prints a -> -3
• >>> a = 10//-4 # prints a -> -3
• >>> a = -10//-4 # prints a -> 2
• >>> a = 123.5//4 #a -> 30.0
• >>> a = 123.5//-4 #a -> -31.0
• >>> a = 3//4 # prints a ->0
• Uses
• a =123
• b =a//10 #reduce unit digit
• print(b) # b -> 12
• a =123
• b =123//100 #gives MSB bit
Arithmetic Operators {‘%’}
Prototype
%(int,int) - > int
//(float,float) - > float
Formula
a%b = a – b * (a // b)
Arithmetic Operators {‘%’}
Example
>>> a = 10%3 # prints a -> 1
>>> a = 5%10 # prints a -> 5
>>> a = -10%3 # prints a -> 2
>>> a = 10%-3 # prints a -> -2
>>> a = -10%-3 # prints a -> -1
>>> a = -3%10 # prints a -> 7
>>> a = -3%-10 # prints a -> -3
>>> a = 3%-10 # prints a -> -7
Uses
a =123
b =a%10 # gives unit digit
print(b) # b -> 3
a =123
b =123%100 #Leaves MSB bit
Arithmetic Operators {‘**’}
Prototype
**(int,int) - > int
**(float,float) - > float
**(complex,complex) - > complex
Arithmetic Operators {‘**’}
Example
>>> a = 10**2 # prints a ->100
>>> a = -2**2 # prints a -> -4
>>> a = -2**-2 #prints a -> -0.25
>>> a = 2**-2 # prints a -> 0.25
Operator Precedence
Example 4 Example 5
a = 113//-4; a = 5.6+3*4-2
print(a) print(a)
Output -29 Output: 15.600000.
Problems
1. Collect three run-time input from user.
Add together the first two numbers
and then multiply this total by the
third. Display the answer as “The
answer is :“ [answer]
2. Program to print only the added
fraction of any two number {Input:
2.356 3.5678 Output: 0.9238]
3. Program to print current hours, minutes and
seconds?.
4. Program to print the unit digit of a given number
{ Input: 1567 Output: 7}
5. Program to calculate the sum from ‘M’ number to
‘N’ number. {Input: M-5 N-10 Output: 45}
6. Ramesh’s basic salary is input through the
keyboard. His dearness allowance is 40% of basic
salary, and house rent allowance is 20% of basic
salary. Write a program to calculate his gross salary
Relational Operators
Relational Operators
a = 1000
a=5
b=a
#Both 'a' and 'b' will point the same object print(a,b)
Assignment Operator
Multiple Assignment:
a = b = c = 50 #a b and c has value of 50
a, b =100,50 # a=100 and b =50
Assignment Operators
Python supports the following shortcut
assignment operators: +=, -=, *=, /=,
%=, //=, **=
a=10
a+=10
# a= a+10
print(a) #a=20
Assignment Operators
a=10.5
a+=10 # a= a+10.5
print(a) #a=20.5
a=10
a-=10 # a= a-10
print(a) #a=0
Assignment Operators
x=‘wel’
y=‘come’
x+=y #x=x+y
print(x) #a=‘welcome’
a=10
a**=2 # a= a**2
print(a) #a=100
Logical Operators
T T T T F
T F F T F
F T F T T
F F F F T
Logical Operators - Example
a=10 a=10 a=10
print(a<20 and a>5) print(a>20 and print(not a) #False
# True a<20) print(not -1) #False
#False
a=10 a=10
print(a<5 and print(a>5 or
((a:=15)>5)) ((a:=15)>5))
#False #True
print(a) #a=10 print(a) #a=10
Bitwise Operators
Bitwise Operators
• Bit–wise operations compute their
results on the individual bits – based on
integer operands
• Both Input and Output : Integers
• For Process: Binary format
Bitwise Operators
Truth Table
0 0 0 0 0 1
0 1 1 0 1 1
1 0 1 0 1 0
1 1 1 1 0 0
Bitwise Operators
• A bitwise operation operates on one or more bit patterns at
the level of individual bits
• Let us assume x = 10 (0000 1010 in binary and
y = 4 0000 0100 in binary)
Bitwise Operators
Not (~) - If the ith bit is zero, it will change to 1 and
viceversa
AND (&) -> if both the bits in the compared position of
the bit patterns are 1 the bit in resulting patthern is 1,
otherwise 0 Ex:a=5=(101)2 b=3=(011)2 A&B=(001)2=1
OR -> Both bits are 0 the result will be 0therwise 1
XOR (^) -> If both bits are 0 or 1 the result bit pattern is 0
or otherwise 1
Left Shift (<<)-> shift some no of bits in the given pattern
to left and append 0 at end
Right Shift (>>)-> Shift some of the bits in the given
pattern to the right and append 1 at end
Bitwise Operators
Example
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0
c = a & b # 12 = 0000 1100
print ("Line 1 - Value of c is ",c)
c = a | b # 61 = 0011 1101
print ("Line 2 - Value of c is ",c)
c = a ^ b # 49 = 0011 0001
print ("Line 3 - Value of c is ",c)
c = ~a # -61 = 1100 0011
Bitwise Operators
Output :
• Line 1 - Value of c is 12
• Line 2 - Value of c is 61
• Line 3 - Value of c is 49
Bitwise Operators
a=23
b = a>>1 #1011
print(b) #b=11
Bitwise Operators
a=23
b= a<<1 # 101110
Print(b) #b = 46
Membership Operators
in, not in
These operators are used to test whether a
value is found within a sequence or not
a=101
b=101
print(a is b) #True
a=101
b=102
print(a is b) #False
a=101
b=101
print(a is not b) #False
Operators – Test Yourself
Example - 1 Example - 2 Example - 3
Example – 4 Example – 5
a = -5; a = 10
print(~a) b = a<<1 + 20//5
print(320)
Output
4 Output
Output: 320
Questions
1. Calculate CGST and SGST for a product and print the total
price to pay {CGST = SGST = 9%}