0% found this document useful (0 votes)
7 views9 pages

L6 Conditions and Looping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

L6 Conditions and Looping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Manipal University, Jaipur

Object Oriented Programming


(AI2104)

Lecture 6

Data Structure in Python


Dr. Yadvendra Pratap Singh
Course Details Asst. Professor(SS)
(B. Tech. III Sem) CSE

Object oriented Programming Dr. Y.P.Singh


1
12/03/2024
Control Structures in Python

• In general, statements are executed sequentially, top to bottom.


• There are many instances when you want to break away from this e.g,
• Branch point: choose to go in one direction or another based on meeting
some condition.
• Repeat: You may want to execute a block of code several times.
• Programming languages provide various control structures that allow for
more complicated execution paths.
• Here we will focus on two such examples conditionals and loops:

• Will see the if, while and for statements and associated else, elif, range,
break and continue commands

12/03/2024 Object oriented Programming Dr. Y.P.Singh 2


The if statement is used to check a condition: if the condition is TRUE, we run a block
of statements (the if-block), else (FALSE) we process another block of statements
(else-block) or continue if no optional else clause.

if guess==number: #if followed by condition we


are testing, colon indicates begin the if-block
<Block of statements> # indent shows
that you are in the if-block, when the block of
statements is completed you will drop back into
the main program.

if guess==number: #if TRUE execute statements


<Block of statements> # skips else
else:#if FALSE executes the second block
<Block of statements> # returns to main
program after these statements.
12/03/2024 Object oriented Programming Dr. Y.P.Singh 3
The elif statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE..
if guess==number: #1st condition
<Block of statements> #
elif condition2: # checks for a 2nd condition which if TRUE runs
second block of statements.
<Block of statements> #
elif condition3: # checks for a 3rd condition which if TRUE runs 3rd
block of statements.
<Block of statements> #
elif condition4: # checks for a 4th condition which if TRUE runs 4th
block of statements.
<Block of statements> #
else: # optional else if all above statements are FALSE
<Block of statements> #

12/03/2024 Object oriented Programming Dr. Y.P.Singh 4


You may want to check for another condition after a condition resolves to TRUE. Here
you can use the nested if construct.
In a nested if construct, you can have an if...elif...else construct inside another
if...elif...else
if condition1: #
<Block of statements> # runs statements then checks next if.
if condition2: #checks for a 2nd condition which if TRUE runs second block of
statements.
<Block of statements> #
elif condition3: # checks for a 3rd condition which if TRUE runs 3rd block of
statements.
<Block of statements> #
else: # if condition 1 is TRUE, but 2 and 3 are FALSE.
<Block of statements> #
else: # optional else if condition 1 is FALSE
<Block of statements> #

12/03/2024 Object oriented Programming Dr. Y.P.Singh 5


Loops: for loops are traditionally used when you have a piece of code which you
want to repeat number of times. As an alternative, there is the while Loop, the while
loop is used when a condition is to be met, or if you want a piece of code to repeat
forever.
for letter in 'Python': # loops for each character in string
print 'Current Letter :', letter

fruits = ['banana', 'apple', 'mango']


for fruit in fruits: # loops for each element in list
print 'Current fruit :', fruit

for index in range(len(fruits)): # loops for each element in list


print 'Current fruit :', fruits[index]

FruitsA=[x for x in fruits if x[0]==‘a’] # returns [‘apple’]

12/03/2024 Object oriented Programming Dr. Y.P.Singh 6


Control Structures in Python: range()
The range() function creates a list of numbers determined by the input parameters.
range([start,] stop[, step]) -> list of integers
When step is given, it specifies the increment (or decrement).
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
e.g. How to get every second element in a list?
for i in range(0, len(data), 2):
print data[i]

12/03/2024 Object oriented Programming Dr. Y.P.Singh 7


Loops: the while loop is used when a condition is to be met, or if you want a piece of
code to repeat forever.

while expression: #while TRUE execute block of statements


<Block of statements>

var = 1
while var == 1 : # This constructs an infinite loop
num = raw_input("Enter a number :")
print "You entered: ", num

if num=='Q': # This gives a route out of loop


print “Goodbye”
break # break will terminate the loop

12/03/2024 Object oriented Programming Dr. Y.P.Singh 8


End

Thank You

12/03/2024 Object oriented Programming Dr. Y.P.Singh 9

You might also like