Chapter 2
Chapter 2
1
Python divides the operators in the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
2
Arithmetic Operators
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
3
Arithmetic Operators Example:
x=5
y=3
print(x + y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x**y)
print(x//y)
#the floor division // rounds the result
down to the nearest whole number
4
Assignment Operators:
Assignment operators are used to assign values to variables:
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
5
Assignment Operators Example:
x=5
x+=3
print(x)
x=5
x-=3
print(x)
x=5
x*=3
print(x)
x=5
x/=3
print(x)
x=5
x%=3
print(x)
6
Comparison Operators
Comparison operators are used to compare two values:
== Equal x == y
!= Not equal x != y
> Greater than x>y
7
Comparison Operators Example:
x=6
y=4
print(x == y)
print(x!=y)
print(x>y)
print(x>= y)
print(x<y)
print(x<=y)
8
Logical Operators
Logical operators are used to combine conditional statements:
x=5
print(x > 2 and x < 10)
print(x > 2 or x < 10)
print(not(x > 2 and x < 10))
10
Bitwise Operators
As the name suggests, bitwise operators perform operations at the bit level. These
operators include bitwise AND, bitwise OR, bitwise XOR, and shift operators. Bitwise
operators expect their operands to be of integers and treat them as a sequence of bits.
The truth tables of these bitwise operators are given below.
11
12
Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator Description Example
in Returns True if a x in y
sequence with the
specified value is
present in the object
not in Returns True if a x not in y
sequence with the
specified value is not
present in the object
13
Type Conversion
In Python, it is just not possible to complete certain operations that involves different
types of data. For example, it is not possible to perform "2" + 4 since one operand is an
Exampl and the other is of string type.
integer
e:
15
Conditional Statements
Conditional execution (if…)
CONDITIONAL EXECUTION (IF…)
Syntax:
if condition:
do_something
Condition must be statement that evaluates
to a boolean value (True or False)
16
Example:
a=55
b=500
if b>a:
print("b is greater than a")
Indentation:
Python relies on indentation (whitespace at the beginning of a line) to define
scope in the code. Other programming languages often use curly-brackets {}
for this purpose.
By default Python puts 4 spaces but it can be changed by programmers.
17
If-Else Statement
18
Example
:
age=int(input("Enter the age:"))
if age>=18:
print("You are eligible for vote")
else:
print("You are not eligible for vote")
19
If-elif-else Statement
Python supports if-elif-else statements to test additional conditions apart from the
initial test expression. The if-elif-else construct works in the same way as a usual if-
else statement. If-elif-else construct is also known as nested-if construct.
Example:
20
Example
:
num=int(input("Enter any number:"))
if num==0:
print("The value is equal to zero")
elif num>0:
print("The number is positive")
else:
print("The number is negative")
21
While Loop
22
Example:
i=1
while(i<=10):
print(i)
i=i+1
23
Python end parameter in print()
By default python’s print() function ends with a newline. Python’s print() function comes with a parameter
called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print
statement with any character/string using this parameter.
i=1
while(i<=10):
print(i,end=" ")
i=i+1
24
For Loop
For loop provides a mechanism to repeat a task until a particular condition is True. It is
usually known as a determinate or definite loop because the programmer knows exactly
how many times the loop will repeat.
The for...in statement is a looping statement used in Python to iterate over a sequence of
objects.
25
For Loop and Range() Function
The range() function is a built-in function in Python that is used to iterate over a
sequence of numbers. The syntax of range() is range(beg, end, [step])
The range() produces a sequence of numbers starting with beg (inclusive) and ending
with one less than the number end. The step argument is option (that is why it is placed
in brackets). By default, every number in the range is incremented by 1 but we can
specify a different increment using step. It can be both negative and positive, but not
If range() function is given a single argument, it produces an object with values from 0
zero.
to argument-1. For example: range(10) is equal to writing range(0, 10).
• If range() is called with two arguments, it produces values from the first to the
second. For example, range(0,10).
• If range() has three arguments then the third argument specifies the interval of the
26
sequence produced. In this case, the third argument must be an integer. For example,
Examples:
for i in range(0,10,2):
print(i,end=" ")
27
Nested Loops
Python allows its users to have nested loops, that is, loops that can be placed inside
other loops. Although this feature will work with any loop like while loop as well as for
loop.
A for loop can be used to control the number of times a particular set of statements will
be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Example:
Loops should be properly indented to identifyoutput
which statements are contained within
for i in range(5):
each for statement. *****
print() *****
*****
for j in range(5):
*****
print("*",end=" ") ***** 28
The Break Statement
The break statement is used to terminate the execution of the nearest enclosing loop in
which it appears. The break statement is widely used with for loop and while loop. When
compiler encounters a break statement, the control passes to the statement that follows
the loop in which the break statement appears.
Example:
29
for i in
range(1,10):
if i==5:
continue
print(i,end=" ")
Output
12346789
31
32
Write a program to print following Pattern
1
rows = 6 2 2
3 33
4 444
for num in range(1,rows): 5 5555
for i in range(num):
print(" ")
33
Pass statement
In Python programming, the pass statement is a null statement. The difference between
a comment and a pass statement in Python is that while the interpreter ignores a comment
entirely, pass is not ignored.
However, nothing happens when the pass is executed. It results in no operation (NOP)
Suppose we have a loop or a function that is not implemented yet, but we want to implement
it in the future. They cannot have an empty body. The interpreter would give an error. So, we
use the pass statement to construct a body that does nothing.
Example: Example:
def function(args): class abc:
pass pass
34