Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views26 pages

Decision Making - Loops in Python

python decision making material

Uploaded by

perarasum19
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)
8 views26 pages

Decision Making - Loops in Python

python decision making material

Uploaded by

perarasum19
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
You are on page 1/ 26

Programming in Python

(Decision Making, Loops)

Dr. Faisal Anwer


Department of Computer Science
Aligarh Muslim University 1
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap
• Arithmetic Operators

• Relational Operators

• Assignment & Compound Operators

• Logical Operators

• Bitwise Operators

• Membership Operators

• Identity Operators
Contents
• Decision Making

• For Loop

• While Loop

• Nested Loops
Decision Making
 A program usually needs to take decisions based on some
conditions.
 According to the conditions-
 Either one set of statements is executed Or another set of
statements is executed.
 The conditions are expressions.
 The conditions may be either True or False.
 The basic rule is:
 The “empty” or “zero” is False
 True is anything else except (“non-empty” or “non-zero”)
4
Decision Making
 Four decision making structures
 If

 If-else

 if-elif-else

 Nested if

5
"if" Decision Making Structure
 “if” structure allows a group of statements to be executed or skip it
 All “if structures” (statements) have a “condition” that evaluates to
either “True” or “False”
 if structure
if expression:
 statement
 statement

 Example- It prints the message if the condition is “True”.


if x%2 == 0:
print ( x, ”is even number”)

 The statements in if structures (or any control structure) must be


indented.

6
"if-else" Decision Making Structure
 if statement can be followed by an optional else statement.
 Sometimes programs need to make a choice
 If the condition is “True”, Execute block of code after if statement.
 If the condition is “False”, Execute block of code after else statement.
 If structure
if expression:
statement
statement
else:
statement
statement
 Example
if x%5==0:
print(x, “is multiple of 5”)
else:
print(x, “is not multiple of 5”)
7
“if-elif-else” Decision Making Structure
 If the program needs to choose from more than two possibilities
 Use the if-elif-else structure.
 According to the value of condition in if-elif-else structure, the
computer executes the corresponding block.
 The other conditions are not evaluated

 if-elif-else structure
if expression 1:
statement 1
statement 2
elif expression 2 :
statement 3
statement 4
else expression 3 :
statement 5
statement 6
8
Nested " if-else" Decision Making Structure
 You can use “if” or “if-else” or “if-elif-else” statement/statements inside
another “if” or “if-else” or “if-elif-else” statement(s).

 Example
if x == 1:
y = y+1
else:
if x== 2:
y = y+2
else:
if x == 3:
y = y+3
else:
y = y-10
9
Pass Statement
 It is written when a statement is required syntactically but you
do not want any code to execute.

 When pass is executed, nothing happens.

 It is useful in places where your code will eventually go, but


has not been written yet.

Example
if x != 100:
sum += x
else:
pass

10
Lines & Indentation
 The line indentation is used to identify the block of code.

 The line indentation is mandatory for block.

 The number of spaces in the indentation may vary, but all statements
within the block must be indented the same amount.

Example-Valid Code Example-Invalid Code


x =10 x =10
if x%2 == 0: if x%2 == 2:
print ( x, ”is even number”) print ( x, ”is even number”)
print(x, „is even integer‟)
Programs
1. Write a program to read an integer and use bitwise operators to check
the nature (even or odd) of the integer.
 Property:
 The binary representation of every odd integer consists of 1 at
the rightmost position.
 The binary representation of every even integer consists of 0 at
the rightmost position.

2. Write a program to find the maximum of three numbers.


If-elif-else statement

3. Write a program to read the values of two points on x-y plane and find
out the distance between two points.
 Use math functions (sqrt, pow)

12
Loops in Python

13
Loops
 Situation may arise, when the programmers need to execute a
block of code multiple times.

 A loop statement allows us to execute a statement or block of


statements multiple times.
 Control mechanism is required

 Loop structures
 For
 While
 Nested

14
Creating For Loops
 The for loop executes a sequence of statements multiple times.
 Python’s for statement iterates over the items in the order that
they appear in the sequence.
 The for loop is created in the following way-
 for variable in range(constant/variable/expression)/list/tuple/string/etc:
statement/statements
 Procedure
 The variable starts from first item and executes the statements
within the loop.
 The value of the variable is changed to next item in the
sequence datatype and the statements within the loop are
executed again.
 This procedure is repeated for all items in the sequence.
Creating For loops using range functions
for variable in range(bound):
statements

 The range function creates a sequence from 0 to bound-1.


 The variable starts from first item (0) and executes the statements
within the loop.
 The value of the variable is changed to next item in the sequence
datatype and the statements within the loop are executed.
 This procedure is repeated for all items in the sequence data type.

Example:
for x in range(2):
print(“Hello, Participant”)
print(“The value of x is”, x)

16
Creating For loops using range functions
for variable in range(LB, UB):
statements

 The range function creates a sequence from LB to UB-1.


 The variable starts from first item and executes the statements
within the loop.
 The value of the variable is changed to next item in the sequence
datatype and the statements within the loop are executed.
 This procedure is repeated for all items in the sequence data type.

Example-
for x in rang(7, 10):
print(“Hello, Participants”)
print(“The value of x is”, x) 17
Creating For loops using range functions
for variable in range(LB, UB, stepvalue):
statements

 The range function creates a sequence from LB to UB-1 with given


step value.
 The variable starts from first item and executes the statements
within the loop.
 The value of the variable is changed to next item in the sequence
datatype and the statements within the loop are executed.
 This procedure is repeated for all items in the sequence data type.

Example:
for x in range(7, 10, 2):
print(“Hello, Participants”)
print(“The value of x is”, x) 18
Creating For loops
Example: Loop with tuple
for x in (7, 10, 2):
print(“Hello, Particiapnts”);print(“The value of x is”, x)

Example: Loop with list


for x in [12, 3, 4]:
print(“Hello, Participants”)
print(“The value of x is”, x)

19
Creating While loops
 At each iteration, the while loop checks the value of condition.
 If the condition is True, the statements within the loop are
executed
 If the condition is False, the loop gets terminated.
 The general syntax is
 While Condition (Expression/Value/Variable) :
 statement/statements
 Values and Conditions
 All expressions have a conditional value either True or False
 The empty string and zero are false.
 True is anything else (“non-empty” or “not zero”)

 Example
 0 is False, „‟ is False
 450 is True, “Hello Participants” is True 20
Creating While loops
Example
x =5
while x < 10:
print('Hello, Participants’)
print('The value of x is', x)
x=x+1

Example: Infinite Loop


counter = 20
while counter:
print(counter)
Example: Finite Loop
counter = 10
while counter <=100:
print(counter)
counter = counter +1 21
break statement
 It terminates the loop prior to the exit condition becoming false.
 It breaks out of a loop

While condition 1:
Statements
if condition 2:
break;
Statements
Example-
for i in range(2, 10):
if i == 3:
print(i)
break 22
“else” clause with Loops
(Additional Feature)
 Loop statements may contain an else clause.
 It is executed when the loop terminates through
exhaustion of the iteration in for loop

 The else clause with loop is executed when no break occurs.

 The else clause will not executed if the loop gets terminated
by a break statement.

23
“else” clause with Loops
(Additional Feature)
Example 1:- To check if an even no is present in a list
for x in [3,5,7,9]:
if x % 2 == 0:
print(“X is having an even integer: “,x)
break
else:
# loop fell through without finding a factor
print(“List is not having any even integer”)

24
Programs
1. Write a program to compute the factorial of an integer.

2. Write a program to check whether the integer is prime.

3. Write a program to find prime numbers between two numbers.

25
Summary
 Decision Making

 For Loop

 While Loop

 Nested Loops

You might also like