0% found this document useful (0 votes)
15 views46 pages

Control Statements in Python

This document covers control statements in Python, including decision-making statements (if, if-else, nested if, and switch) and looping statements (while and for loops). It explains the syntax and provides examples for each type of statement, as well as manipulating loops with break, continue, and pass statements. Additionally, it discusses the range function and nested loops, illustrating their use with practical programming examples.

Uploaded by

dakshini742006
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)
15 views46 pages

Control Statements in Python

This document covers control statements in Python, including decision-making statements (if, if-else, nested if, and switch) and looping statements (while and for loops). It explains the syntax and provides examples for each type of statement, as well as manipulating loops with break, continue, and pass statements. Additionally, it discusses the range function and nested loops, illustrating their use with practical programming examples.

Uploaded by

dakshini742006
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

Unit - II Control Statements

in Python
Syllabus:
• 2.1 Types of Control Statements – Decision making
statements, Looping statements
• 2.2 Decision Making Statements: - if, if….else, else-if ladder,
nested if and switch statement
• 2.3 Looping statement: - while loop, for loop, nested loop
• 2.4 Manipulating Loops- use of break, continue and pass
statements
Types of Control Statements:
1. Decision making statements
2. Looping statements
Decision making statements:
• if,
• if….else,
• else-if ladder,
• nested if
• switch statement
If statement:
• if statement is written using if keyword and condition.
• if keyword will be followed by condition and condition will be
followed by colon.
• Unlike C, C++ python uses identation (colon) to declare scope of code
in if statement.
• No need to use round brackets for conditions
• After condition part colon (:) must be used
• Example- if a>b:
• Syntax:
if condition:
statement1
statement2
-----------
• Example:
if 10 > 5:
print("10 greater than 5")

Output:
10 greater than 5
Program:
• Program to check positive number
• Program to check equality of numbers
• Program to check even number
if….else statements:
• if keyword will be followed by condition and condition will be
followed by colon.
• All statements within block of if will come with identation
• Next else keyword will be followed by colon
• else will have its own block of statement
• Syntax:
if condition:
statement1
statement2
--------------
else:
statement1
statement2
--------------
• Example:
x=3
if x == 4:
print("Yes")
else:
print("No")

Output:
No
Program:
• Program to check number is positive or negative
• Program to check numbers are equal or not
• Program to check number even or odd
else-if ladder:
• You can also chain if..else statement with more than one condition.
• The code uses a nested if..else chain to check the value of the variable
letter. It prints a corresponding message based on whether letter is
“B,” “C,” “A,” or none of the specified values, illustrating a hierarchical
conditional structure.
• Example:

letter = "A"
if letter == "B":
print("letter is B")
else:
Output:
if letter == "C":
print("letter is C") letter is A
else:
if letter == "A":
print("letter is A")
else:
print("letter isn't A, B and C")
nested if:
• if statement can also be checked inside other if statement.
• This conditional statement is called a nested if statement.
• This means that inner if condition will be checked only if outer if
condition is true and by this, we can see multiple conditions to be
satisfied.
• Example:
# Nested if statement example
num = 10
Output:
if num > 5:
Bigger than 5
print("Bigger than 5") Between 5 and 15
if num <= 15:
print("Between 5 and 15")
if…..elif statement:
• The if-elif statement is shortcut of if..else chain. While using if-elif
statement at the end else block is added which is performed if none
of the above if-elif statement is true.
• Example:
# if-elif statement example
letter = "A"
if letter == "B":
print("letter is B")
elif letter == "C": Output:
print("letter is C") letter is A
elif letter == "A":
print("letter is A")
else:
print("letter isn't A, B or C")
Looping statement:
• while loop,
• for loop,
• nested loop
while loop:
• ‘while’ loop is same as that of C programming
• ‘while’ keyword will be followed by condition and ends with colon.
• Condition does not demand for ( )
• Syntax:
• while condition:
------
statement1
statement2
-------
• Example:
• while i<10:
print(i)
i+=1
• Program: to display ‘hello’ message 10 times using while loop
• Program: Program to display series of 5 using while loop
Program:
• Program to display ‘hello’ message 10 times using while loop
• Program to display series of 5 using while loop
• Program to display odd numbers between 100 to 110
• Program to display fibonacci series upto given number
For loop:
• for loop is used to iterate over a sequence of
statements
• Unlike C programming python neither demand for
variable initialization neither demand to give
condition and increment/decrement part
• for loop syntax is:
• for i in range(----):
• For loop is always ends with colon (:)
RANGE() FUNCTION:
• To calculate the total number of iteration, for loop in
python uses range() function
• Hence range() with integer value fixes the total
iterations (loops) to be done over block of statement.
• range ( ) function have three different types:
• 1. range(start) :
• 2. range(start, stop) :
• 3. range(start, stop, step)
1. RANGE(START):
• Here ‘start’ is an integer value
• Example: range(10) //means total 10
iteration.
• Range(10)=[0,1,2,3,4,5,6,7,8,9]
• eg. For x in range(10):
here value of x starts with 0 and ends with 9
2. RANGE(START, STOP):
• Here ‘start’ and ‘stop’ both are an integer values
• Example: range(10,20) //means total 10
iteration.
• Range(10,20)=[10,11,12,13,14,15,16,17,18,19]
• eg. For x in range(10,20):
here value of x starts with 10 and ends with 19
Program:
• Program to display ‘hello’ message 10 times
• Program to display numbers from 31 to 40
• Program to display all prime numbers between 100 to 110
• Program to display factorial of given number
• Program to do addition of numbers between 1 to 15
3. RANGE(START, STOP, STEP):
• Here ‘start’ , ‘stop’ and ‘step’ all are an integer
values
• Example: range(10,20,2) //means total 5
iteration.
• Range(10,20,2)=[10,12,14,16,18]
• eg. For x in range(10,20):
• here value of x starts with 10
• It increment by 2 at every iteration
• Hence ends with 18
PROGRAM:
• Program to display ‘hello’ message 10 times using range(start, stop,
step)
• Program to display series of 5
• Program to display odd numbers between 100 to 110
• Program to display factorial of given number
NESTED LOOP:
• In Python programming language there are two types of loops which
are for loop and while loop. Using these loops we can create nested
loops in Python. Nested loops mean loops inside a loop. For example,
while loop inside the for loop, for loop inside the for loop, etc.
SYNTAX:
Outer_loop Expression:
Inner_loop Expression:
Statement inside inner_loop
Statement inside Outer_loop
Example:
• 1: Basic Example of Python Nested Loops
x = [1, 2] Output:
y = [4, 5] 14
15
for i in x: 24
for j in y: 25
print(i, j)
• 2: Printing multiplication table using Python nested for loops
# Running outer loop from 2 to 3
Output: Output:
2*1=2 3*1=3
for i in range(2, 4): 2*2=4 3*2=6
2*3=6 3*3=9
2*4=8 3 * 4 = 12
# Printing inside the outer loop 2 * 5 = 10 3 * 5 = 15
2 * 6 = 12 3 * 6 = 18
# Running inner loop from 1 to 10 2 * 7 = 14 3 * 7 = 21
for j in range(1, 11): 2 * 8 = 16 3 * 8 = 24
2 * 9 = 18 3 * 9 = 27
2 * 10 = 20 3 * 10 = 30
# Printing inside the inner loop
print(i, "*", j, "=", i*j)
# Printing inside the outer loop
print()
Manipulating Loops:
• use of break,
• continue and pass statements
Use of break:
• The break statement in Python is used to terminate the loop or statement
in which it is present. After that, the control will pass to the statements
that are present after the break statement, if available. If the break
statement is present in the nested loop, then it terminates only those loops
which contain the break statement.
• Syntax:
for / while loop:
# statement(s)
if condition:
break
# statement(s)
# loop end
• Example:
for i in range(10):
print(i)
if i == 2:
break

• OUTPUT:
0
1
2
num = 0
OUTPUT:
for i in range(10): The num has value: 1
The num has value: 2
num += 1 The num has value: 3
if num == 8: The num has value: 4
The num has value: 5
break The num has value: 6
The num has value: 7
print("The num has value:", num) Out of loop
print("Out of loop")
continue statements:
• Python Continue Statement skips the execution of the program block
after the continue statement and forces the control to start the next
iteration.
• SYNTAX:
while True:
...
if x == 10:
continue
print(x)
• Example: Output:
H
for var in "HELLOW EVERYONE HOW ARE YOU": E
L
if var == "O": L
W
continue
E
print(var) V
E
R
Y
N
E

H
W

A
R
E

Y
U
for i in range(1, 11):
Output:
# If i is equals to 6, 1 2 3 4 5 7 8 9 10
# continue to next iteration
# without printing
if i == 6:
continue
else:
# otherwise print the value
# of i
print(i, end=" ")
Pass statements:
• When the user does not know what code to write, So user simply places a
pass at that line. Sometimes, the pass is used when the user doesn’t want
any code to execute. So users can simply place a pass where empty code is
not allowed, like in loops, function definitions, class definitions, or in if
statements. So using a pass statement user avoids this error.
• Why Python Needs “pass” Statement?
• If we do not use pass or simply enter a comment or a blank here, we will
receive an IndentationError error message.
• Example:
n = 26 IndentationError: expected an indented block after 'if' statement
if n > 26:
# write code your here

print('Geeks')
• Example:
n = 10
for i in range(n):
pass

• Output:
li =['a', 'b', 'c', 'd']

for i in li: Output:


if(i =='a'): b
c
pass
d
else:
print(i)
a = 10
b = 20

if(a<b):
pass
else:
print("b<a")

You might also like