Operators and Expressions
D r . K . Vallidevi
VIT Chennai
K . Vallidevi Operators an d Expressions 1 / 21
Table of contents
1 Operators
Arithmetic Operators
Comparsion Operators
Logical Operators
Assignment Operators
Membership Operators
Unary Operator
Bitwise Operators
Identity Operators
Precedence of Operators
2 Expression and Statements
3 Keywords
4 Tuple Assignment
5 Comments
6 Test Your Knowledge
7 Reference
K . Vallidevi Operators an d Expressions 2 / 21
Operators and Expressions
Operators are special symbols that represent computations like
addition, multiplication using + and *.
A n expression is a combination of values, variables, and operators.
In an expression, an operator is used on operands
a + b → expression
a, b → operands
+ → operator
K . Vallidevi Operators an d Expressions 3 / 21
Operators
Python supports following types of Operators:
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Membership Operators
Unary Operators
Bitwise Operators
Identity Operators
K . Vallidevi Operators an d Expressions 4 / 21
Arithmetic Operators
a=10 b=20
K . Vallidevi Operat ors an d Expressions 5 / 21
Processing
K . Vallidevi Operat ors an d Expressions 6 / 21
Assignment Operators
K . Vallidevi Operat ors an d Expressions 7 / 21
D e c i s i o n Operators
K . Vallidevi Operat ors an d Expressions 8 / 21
Unary Operator
Unary operator act on single operand
Unary minus (-) negates the value
Different from arthimetic operator that operates on two operands
and subtract the second operand from the first
b=10
a=−b
p r i n t ( b) 10
print ( a) −10
K . Vallidevi Operators an d Expressions 9 / 21
Connectors
K . Vallidevi Operat ors an d Expressions 10 / 21
Subroutines
K . Vallidevi Operat ors an d Expressions 11 / 21
Expression and Statements
A n expression is a combination of values, variables, and operators.
To print an expression, the interpreter evaluates the expression and
displays the result.
The evaluation of an expression produces a value
p r i n t ( 1 + 1) 2
p r i n t ( len ( ” h el l o ” )) 5
A statement is an instruction that the Python interpreter can execute.
Expression is also a statement
Eg: assignment statement(=), Conditional statement (if), alternative
statement(if else, if elif else), Iterative Statement(while)
K . Vallidevi Operat ors an d Expressions 12 / 21
Keywords
Keywords define the language’s syntax rules and structure, and they
cannot be used as variable names.
The interpreter uses keywords to recognize the structure of the
program, and they cannot be used as variable names.
and as a s s er t break class continue
def del elif else except e x ec
finally for from global if import
in is lambda n o n lo c a l n ot or
pass raise return try while wi th
y i el d True False none
K . Vallidevi Operat ors an d Expressions 13 / 21
Tuple Assignment
Swapping two variables a and b:
temp = a
a = b
b = temp
Python provides a form of assignment called as tuple assignment
that solves swap neatly:
a, b = b, a
The left side is a tuple of variables.
The right side is a tuple of values.
Each value is assigned to its respective variable
K . Vallidevi Operators an d Expressions 14 / 21
Tuple Assignment
All the expressions on the right side are evaluated before any of
the assignments.
This feature makes tuple assignment quite versatile.
x=1 5
x , y = 5 , x+ 2
pri nt ( x , y) 5 17
The number of variables on the left and the number of values on
the right have to be the same:
a, b, c, d = 1, 2, 3
ValueError: unpack tuple of wrong size
K . Vallidevi Operators an d Expressions 15 / 21
Comments
Comments are the non-executable statements in a program.
Added to describe the statements in the program code.
Comments make the program easily readable and understandable
by the programmer and others.
The interpreter simply ignores the comments.
In Python, a hash sign # that is not inside a string literal begins a
comment.
All characters following the # and up to the end of the line are
part of the comment
K . Vallidevi Operators an d Expressions 16 / 21
Comments
# program s t a r t s
. . . a=5 # a ssi gn me n t v a r i a bl e
> > > b=a+6
> > > p r i n t ( b , a ) # d i s p l a y ing output
11 5
> > > # pr og r am ends
K . Vallidevi Operat ors an d Expressions 17 / 21
Test Your Knowledge
1
What is answer of this expression, 22 % 3?
2 Operators with the same precedence are evaluated in which
manner?
3
The expression Int(x) implies that the variable x is converted to
integer. State whether true or false.
K . Vallidevi Operators an d Expressions 18 / 21
Reference
Think Python 2 nd Edition by Allen B . Downey.
K . Vallidevi Operators an d Expressions 19 / 21