Module 2
Module 2
Session - 6
L.Pavithra
Assistant Professor
SCOPE
VIT -AP
1
Operators and Expressions
in Python
2
Python - Basic Operators
Python language supports following type of
operators.
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
3
Python Arithmetic Operators:
Operator Description Example (a=10,b=20)
+ Addition - Adds values on either side of the a + b will give 30
operator
- Subtraction - Subtracts right hand operand a - b will give -10
from left hand operand
* Multiplication - Multiplies values on either a * b will give 200
side of the operator
/ Division - Divides left hand operand by right b / a will give 2
hand operand
% Modulus - Divides left hand operand by right b % a will give 0
hand operand and returns remainder
** Exponent - Performs exponential (power) a**b will give 10 to the
calculation on operators power 20
// Floor Division - The division of operands 9//2 is equal to 4 and
where the result is the quotient in which the 9.0//2.0 is equal to 4.0
digits after the decimal point are removed.
4
Python Comparison Operators:
Operator Description Example(a=10,b=20)
== Equal -Checks if the value of two operands are equal or (a == b) is not true.
not, if yes then condition becomes true.
!= Not equal - Checks if the value of two operands are equal (a != b) is true.
or not, if values are not equal then condition becomes true.
<> Not equal - Checks if the value of two operands are equal (a <> b) is true. This is
or not, if values are not equal then condition becomes true. similar to != operator.
> Greater than - Checks if the value of left operand is (a > b) is not true.
greater than the value of right operand, if yes then condition
becomes true.
< Less than -Checks if the value of left operand is less than (a < b) is true.
the value of right operand, if yes then condition becomes
true.
>= Greater than or equal to - Checks if the value of left (a >= b) is not true.
operand is greater than or equal to the value of right
operand, if yes then condition becomes true.
<= Less than or equal to - Checks if the value of left operand (a <= b) is true.
is less than or equal to the value of right operand, if yes
then condition becomes true.
5
Python Assignment Operators:
Operator Description Example
= Simple assignment operator, Assigns values from right side c = a + b will assigne
operands to left side operand value of a + b into c
+= Add AND assignment operator, It adds right operand to the left c += a is equivalent to
operand and assign the result to left operand c=c+a
-= Subtract AND assignment operator, It subtracts right operand c -= a is equivalent to
from the left operand and assign the result to left operand c=c-a
>> Binary Right Shift Operator. The left operands a >> 2 will give 15 which
value is moved right by the number of bits is 0000 1111
specified by the right operand.
7
Python Bitwise Assignment Operators:
Operator Example (a=5)
&= a&=3 will give 1
^= a ^= 3 will give 6
8
Python Logical Operators:
Operator Description Example
and Called Logical AND operator. If (a and b) is true.
both the operands are true then
then condition becomes true.
or Called Logical OR Operator. If (a or b) is true.
any of the two operands are non
zero then then condition becomes
true.
not Called Logical NOT Operator. not(a and b) is
Use to reverses the logical state false.
of its operand. If a condition is
true then Logical NOT operator
will make false.
9
Python Membership Operators:
In addition to the operators discussed previously, Python has membership
operators, which test for membership in a sequence, such as strings, lists,
or tuples.
10
Python Identity Operators:
Identity operators compare the memory locations of two objects. There are
two Identity operators explained below
13
Order of Operations
14
The computer scans the expression from left to right,
• first clearing parentheses,
15
2**3+2*(2+3)
• 8+2*5
• 8+10
• 18
16
Example 1 – Order of operations
>>> 1 + 2 * 3
7
>>> (1 + 2) * 3
9
• In the first example, the computer calculates 2 * 3 first, then adds 1 to it.
This is because multiplication has the higher priority (at 3) and addition is
below that (at a lowly 4).
• In the second example, the computer calculates 1 + 2 first, then multiplies
it by 3. This is because parentheses have the higher priority (at 1), and
addition comes in later than that.
17
Example 2 – Order of operations
Also remember that the math is calculated from left to right, unless you put in
parentheses. The innermost parentheses are calculated first. Watch these
examples:
>>> 4 - 40 - 3
-39
>>> 4 - (40 - 3)
-33
• In the first example, 4 - 40 is calculated, then - 3 is done.
• In the second example, 40 - 3 is calculated, then it is subtracted from 4.
18
Quotation in Python
• Python accepts single ('), double (") and triple (''' or """)
quotes to denote string literals, as long as the same type of
quote starts and ends the string.
• The triple quotes are used to span the string across multiple
lines. For example, all the following are legal −
• word = 'word'
• sentence = "This is a sentence."
• paragraph = """This is a paragraph. It is made up of multiple
lines and sentences."""
19
Built-in format Function
• Because floating-point values may contain an arbitrary
number of decimal places, the built-in format function can
be used to produce a numeric string version of the value
containing a specific number of decimal places.
20
format Function
21
• For very large (or very small) values 'e' can be
used as a format specifier,
22
23
Python is a Dynamic Type language
• Same variable can be associated with values of
different type during program execution, as
indicated below.
• It's also very dynamic as it rarely uses what it
knows to limit variable usage
24
25
Bitwise Operations
• This includes operators that treat integers as
strings of binary bits, and can come in handy if
your Python code must deal with things like
network packets, serial ports, or packed binary
data
• >>> x = 1 # 1 decimal is 0001 in bits
>>> x << 2 # Shift left 2 bits: 0100
• 4
26
• >>> x | 2 # Bitwise OR (either bit=1): 0011
• 3
• >>> x & 1 # Bitwise AND (both bits=1): 0001
• 1
• In the first expression, a binary 1 (in base 2, 0001) is
shifted left two slots to create a binary 4 (0100).
• The last two operations perform a binary OR to
combine bits (0001| 0010 = 0011) and a binary AND
to select common bits (0001&0001 = 0001).
27
• To print in binary format use bin function:
• >>> X = 0b0001 # Binary literals
• >>> X << 2 # Shift left 4
• >>> bin(X << 2) # Binary digits string
'0b100‘
• >>> bin(X | 0b010) # Bitwise OR: either
'0b11'
• >>> bin(X & 0b1) # Bitwise AND: both
'0b0'
28
Logical Operators
Assume a = 10 and b = 20
Operator Description Example
and If both the operands (a and b) is true.
are true then
condition becomes
true.
Or If any of the two (a or b) is true.
operands are non-
zero then condition
becomes true.
not Used to reverse the Not(a and b) is
logical state of its false.
operand.
29
Python is a Strongly Typed language
30
31
32
Python features….. lambda
operator
The lambda operator or lambda function is a
way to create small anonymous functions,
i.e. functions without a name.
SYNTAX:
lambda arguments : expression
34
Flow chart
35
Python code
36
Output
37
38