0% found this document useful (0 votes)
67 views30 pages

Python Operators Guide

This document discusses Python operators and expressions. It covers arithmetic operators like +, -, *, /, //, %, ** and relational operators like ==, !=, <, >, <=, >=. It explains how each operator works and provides examples of using the operators in Python code. It also discusses operator precedence and covers assignment operators. Problems involving using various operators to calculate values are presented at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views30 pages

Python Operators Guide

This document discusses Python operators and expressions. It covers arithmetic operators like +, -, *, /, //, %, ** and relational operators like ==, !=, <, >, <=, >=. It explains how each operator works and provides examples of using the operators in Python code. It also discusses operator precedence and covers assignment operators. Problems involving using various operators to calculate values are presented at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming

Operators and Expressions

Assistant Professor & Technical Trainer


Madanapalle Institute of Technology & Science
sivakumarm@[Link]
9500600868
Operators

2
[Link] Operators

3
[Link] Operators
 ‘+ and –’ can be used in both unary and binary category
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
4
[Link] Operators {+ , -}
Test yourself
Example -1 Example -2 Example -3
a = ++10 a = -10 a = 1020
print(a) -a print(‘Ans:’,a+10)
Output print(a) print(a)
10 Output Output
Output: -10 Output: 1030
1020

Example -4 Example -5
a = 10++10 a = 10
print(a) a+20 = 10
Output print(a)
Output: 20 Output
Output: Syntax Error
5
[Link] Operators {‘*’}
 Multiplication(*) operator can be used only in the binary category.
Prototype
*(int,int) - > int
*(float,float) - > float
*(complex,complex) - > complex

 Example
Example Uses
a = 5*5 # prints a -> 25 a =‘mits’
a = 5--5 # prints a -> 10 print(a*2) #mitsmits
a = 5.5*5.5 # prints a -> 30.25 Print(2*a) #mitsmits
a = 5 * 5.5 # prints a -> 27.5
a = 3j * 5j # prints a -> (-15+0j)
a = -5 * 5.5 # prints a -> -27.5

6
[Link] Operators {‘/’}
 Division(/) operator can be used only in the binary category.
Prototype
/(int,int) - > float
/(float,float) - > float
/(complex,complex) - > complex

Example
a = 13/5 # prints a -> 2.6
a = 3+10/5 # prints a -> 5.0
a = -10/4 # prints a -> -2.5
a = 10/-4 # prints a -> -2.5
a = -10/-4 # prints a -> 2.5
a = 123.5/-4 # a -> -30.875
a = 3/4 # prints a -> 0.75
7
[Link] Operators {‘//’}
 Floor Division(//) operator can be used only in the binary category. {Quotient}
Prototype
//(int,int) - > int
//(float,float) - > float(Without values)

Example
Uses
>>> a = 10//5 # prints a -> 2
a =123
>>> a = -10//4 # prints a -> -3
b =a//10 #reduce unit digit
>>> a = 10//-4 # prints a -> -3 print(b) # b -> 12
>>> a = -10//-4 # prints a -> 2
>>> a = 123.5//4 #a -> 30.0 a =123
>>> a = 123.5//-4 #a -> -31.0 b =123//100 #gives MSB bit
>>> a = 3//4 # prints a ->0

8
[Link] Operators {‘%’}
 Modulo(%) operator can be used only in the binary category. {Remainder}
 It takes the signature of denominator for answer.
Prototype
%(int,int) - > int Formula
//(float,float) - > float a%b = a – b * (a // b)

Example
Uses
>>> a = 10%3 # prints a -> 1
a =123
>>> a = 5%10 # prints a -> 5
b =a%10 # gives unit digit
>>> a = -10%3 # prints a -> 2 print(b) # b -> 3
>>> a = 10%-3 # prints a -> -2
>>> a = -10%-3 # prints a -> -1 a =123
>>> a = -3%10 # prints a -> 7 b =123%100 #Leaves MSB bit
>>> a = -3%-10 # prints a -> -3
>>> a = 3%-10 # prints a -> -7
9
[Link] Operators {‘**’}
 Power(**) operator can be used only in the binary category. {powered value}
 Left operand is raised to the power of right
Prototype
**(int,int) - > int
**(float,float) - > float
**(complex,complex) - > complex

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

10
Operator Precedence

11
Operator Precedence

Priority Operator Associativity


1st Power ** Right to left
2nd Multiplicative * / % Left to right
3rd Additive +- Left to right

Example
k = 8//4+3-5*6+8
Solving, '*' and '//' first from left to right.
k = 2+3-30+8
Now solving, '+' and '-' from left to right
k = -17
Now solving '=' from right to left
k is made -17

12
Arithmetic Operators

Test yourself
Example -1 Example -2
a = 10*5-2+4//2 a= 10*5-2+4**2
print(a) print(a)
Output Output
Example -5
50 64
a = 5.6+3*4-2
print(a)
Example -3 Example -4 Output
a = -33%-148; a = 113//-4; Output: 15.600000..
print(a) print(a)
Output Output
-33 -29

13
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 calculate hours, minutes and seconds for a given input days.
4. Program to print the unit digit of a given number { Input: 1567 Output: 7}
5. Program to print the MSB digit of given number {Input:1567 Output:1}
6. Program to calculate the sum from ‘M’ number to ‘N’ number. {Input: M-5 N-10 Output: 45}

14
[Link] Operators

15
[Link] Operators
 Relational operator will return answer as boolean value{True or False}

Prototype Operators Supported


(int, int) -> boolean == != < > <= >=
(float,float) ->boolean == != < > <= >=
(complex, complex) ->boolean == !=
(bool, bool) -> booleam == != < > <= >=
(str, str) -> boolean == != < > <= >=

Example
>>> a = 10!=3 # prints a -> True
>>> a = 5.6<5.6 # prints a -> False
>>> a = -10>-3 # prints a -> False
>>> a = False<True #prints a -> True
16
[Link] Operators

17
[Link] Operators
 Assignment operator is the one which helps to assign values or expression to Lvalue of
variable
a = 1000
Lvalue[meaningful variable] = Rvalue[can be value or variable]
Example:
a=5
b=a
#Both 'a' and 'b' will point the same object print(a,b)
Multiple Assignment:
a = b = c = 50 #a b and c has value of 50
a, b =100,50 # a=100 and b =50

18
[Link] Operators
 Python supports the following shortcut assignment operators:
+=, -=, *=, /=, %=, //=, **=

a=10 a=10.5 a=10


a+=10 # a= a+10 a+=10 # a= a+10.5 a-=10 # a= a-10
print(a) #a=20 print(a) #a=20.5 print(a) #a=0

x=‘wel’ a=10
y=‘come’ a**=2 # a= a**2
x+=y #x=x+y print(a) #a=100
print(x) #a=‘welcome’

19
[Link] Operators
 Python supports three logical operators
and, or and not Output:
Boolean value {True or False}
 Truth table

20
[Link] Operators
Example

a=10 a=10 a=10


print(a<20 and a>5) print(a>20 and a<20) print(not a) #False
#True #False print(not -1) #False

a=10 a=10
print(a<5 and ((a:=15)>5)) print(a>5 or ((a:=15)>5))
#False #True
print(a) #a=10 print(a) #a=10

21
[Link] Operators
 Python supports the following bitwise operators

22
[Link] Operators
 Bit–wise operations compute their results on the individual bits – based on integer
operands
Both Input and Output : Integers
For Process: Binary format
Truth Table:

23
[Link] Operators
 Bitwise representation using 16-bit

 Binary Numbers will be filled from LSB to MSB direction

24
[Link] Operators
 Example

a=10 a=10 a=10 a=10


b = a & 12 #1000 b= a | 12 #1110 b=10 ^ 12 # 0110 b = ~a
print(b) #b=8 print(a) #b=14 print(b) #b=6 print(b) #b=-11

a=-10
b=a&12 # 0100
print(b) #b=4

25
[Link] Operators
 Example

a=23 a=23 a=-10


b = a>>1 #1011 b= a<<1 #101110 b=a<<2
print(b) #b=11 print(a) #b=46 print(b) #b=?

26
[Link] Operators
 The following are the membership operators available in python:
Output:
in , not in Boolean Value {True or False}
 These operators are used to test whether a value is found within a sequence or not
 Example: Using String

27
[Link] Operators
 The following are the identity operators available in python:
Output:
is , is not Boolean Value {True or False}
 These operators are used to check if a particular value is of a certain class or type
 Example:

a=101 a=101 a=101


b=101 b=102 b=101
print(a is b) #True print(a is b) #False print(a is not b) #False

28
Operators

Test yourself
Example -1 Example -2
a = ‘mits’<‘Mits’ a= ‘5’ in ‘1115’
print(a) print(a)
Output Output
Example -5
False True
a = 10
b = a<<1 + 20//5
Example -3 print(320)
Example -4
a = -2<<4; a = -5; Output
print(a) print(~a) Output: 320
Output Output
-32 4

29
Problems
1. Calculate CGST and SGST for a product and print the total price to pay {CGST = SGST =
9%}
2. Write a program to read the marks of 5 subjects through the keyboard. Find out the
aggregate and percentage of marks obtained by the student. Assume maximum marks
that can be obtained by a student in each subject as 100.
3. Write a program to read a four-digit number through the keyboard and calculate the sum
of its digits
4. An ATM contains Indian currency notes of 100, 500 and 1000. To withdraw cash from this
ATM, the user has to enter the number of notes he/she wants of each currency, i.e. of 100,
500 and 1000. Write a program to calculate the total amount withdrawn by the person
from the ATM in rupees.
5. Write a program to calculate age based on input of date, month and year.

30

You might also like