Python Decision Making and Loops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Python Decision Making

And Loops
Decision making

● Decision making is the most important aspect of almost all the


programming languages.
● Decision structures evaluate multiple logical expressions which produce
TRUE or FALSE as outcome.

● non-zero and non-null values as TRUE, otherwise FALSE


Decision making

Decision making statements in python

1. If

2. If else

3. nested if
Indentation in Python

● For the ease of programming and to achieve simplicity, python doesn't allow
the use of parentheses for the block level code.
● In Python, indentation is used to declare a block.
● If two statements are at the same indentation level, then they are the part of
the same block.
● Generally, four spaces are given to indent the statements which are a typical
amount of indentation in python.
The if statement

The syntax of the if-statement is given below.

if expression:

statement

E.g.
a=10
if a :
print(“ value is ”,a)
or
a=10
if a : print(“ value is ”,a)
The if statement

e.g. X= int(input(“Enter a number”)) o/p:


r=X%2 Enter a number : 3
if (r==0): bye
print(“Even”) Enter a number : 50
print(“Bye”) Even
Bye
The if-else statement

The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.

If the condition is true, then the if-block is executed.

Otherwise, the else-block is executed.


The if-else statement

Syntax:
if expression:
statement(s)
else :
statement(s)
The if-else statement

age = int (input("Enter your age? "))

if age>=18:

print("You are eligible to vote !!");

else:

print("Sorry! You are not eligible to vote !!");


The elif statement

● The elif statement enables us to check multiple conditions and execute the
specific block of statements depending upon the true condition among them.
● We can have any number of elif statements in our program depending upon
our need.
● However, using elif is optional.
● The elif statement works like an if-else-if ladder statement in C. It must be
succeeded by an if statement.
The elif statement
Nested if
• Can use one if or else if statement inside another if or else if statement(s).
• Can have an if...elif...else construct inside another if...elif...else construct.
LOOPS
● Allows us to execute a statement or group of statements multiple times.

1. While loop

2. For loop

3. Nested loop
while loop
● Repeatedly executes target statement(s) as long as a given condition is true

Syntax: while condition:

statement(s)

● Python uses indentation as its method of grouping statements


● In Python, all the statements indented by the same number of character
spaces after a programming construct( if, for , while etc….) are considered to
be part of a single block of code

● While loop needs,

counter variable

condition

increment or decrement

● Infinite loop – Cntl + C


while loop
i=1 Output:
number = int(input("Enter the number:")) 1
2
while i<=10: 3
print("%d X %d = %d \n" %(number,i,number*i)) 4
5
i = i+1 6
7
8
9
10
While with Else
Print a message once the condition is false:
● Python supports to have an else statement
associated with a loop statement. i=0
while i < 5:
● If the else statement is used with a while print("%d is less than 5" %i)
i += 1
loop, the else statement is executed when
else:
the condition becomes false. print("%d is not less than 5" %i)

Output


Python for loop

The for loop in Python is used to iterate the statements or a part of the program
several times.
It is frequently used to traverse the data structures like list, tuple, or dictionary.
➔ The syntax of for loop in python is given below.
for iterating_var in sequence:
statement(s)
Python for loop

Example-1: Iterating string using for


loop

str = "Python"

for i in str:

print(i)

Output

P
y
t
h
o
n
Python for loop

Program to print the table of the given number .

list = [1,2,3,4,5,6,7,8,9,10]
n=5
for i in list:
c = n*i
print(c)
For loop Using range() function

The range() function is used to generate the sequence of the numbers.

If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the
range() function is given below.

Syntax:

range(start,stop,step size)

● The start represents the beginning of the iteration.


● The stop represents that the loop will iterate till stop-1. The range(1,5) will
generate numbers 1 to 4 iterations. It is optional.
● The step size is used to skip the specific numbers from the iteration. It is optional
to use. By default, the step size is 1. It is optional.
For loop Using range() function

Program to print numbers in sequence.


for i in range(10):
print(i,end = ' ')
Output:
0123456789
For loop Using range() function

Program to print table of given number.


n = int(input("Enter the number "))
for i in range(1,11):
c = n*i
print(n,"*",i,"=",c)
Output:
For loop Using range() function
Enter the number 20

Program to print even number using step size in 2


range(). 4

n = int(input("Enter the number ")) 6


8
for i in range(2,n,2):
10
print(i)
12
14
16
18
Nested for loop in python

Python allows us to nest any number of for loops inside a for loop.

The inner loop is executed n number of times for every iteration of the outer loop.
The syntax is given below.

Syntax

for iterating_var1 in sequence: #outer loop

for iterating_var2 in sequence: #inner loop

#block of statements

#Other statements
Nested for loop in python

Output:
# User input for number of rows
rows = int(input("Enter the rows:")) Enter the rows:5

# Outer loop will print number of rows *


**
for i in range(1,rows+1):
***
# Inner loop will print number of Astrisk ****
*****
for j in range(i):
print("*",end = '')
print()
Using else statement with for loop

● Unlike other languages like C, C++, or Java, Python allows us to use the else
statement with the for loop which can be executed only when all the iterations
are exhausted.

● Here, we must notice that if the loop contains any of the break statement then
the else statement will not be executed.
Using else statement with for loop

for i in range(0,5):
print(i)
else:
print("for loop completely exhausted, since there is no break.")

Output :
0
1
2
3
4
for loop completely exhausted, since there is no break.

You might also like