Python for and while Loops with basic Examples

deshmukhkartiki98 266 views 24 slides Jul 31, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

This is the basic ppt for 1st practical or lecture for Python loops.


Slide Content

Python Loops for loops While loops f or loops A for loop is used for iterating over a sequence (that is either a list, a tuple , a dictionary, a set, or a string ). Iterating means Repetitive execution of the same block of code over and over. With the for loop we can execute a set of statements, once for each item in a list, tuple , set etc. Prof. Kartiki Deshmukh

Example fruits = ["apple", "banana", “mango"] for x in fruits:   print(x) Output: apple banana mango Prof. Kartiki Deshmukh

Looping Through a String S trings are iterable objects, they contains a sequence of characters. Prof. Kartiki Deshmukh

Example for x in  “mango":   print(x ) Output : m a n g o Prof. Kartiki Deshmukh

The break Statement With the break statement we can stop the loop before it has looped through all the items. Prof. Kartiki Deshmukh

Example fruits = ["apple", "banana", "cherry"] for x in fruits:   print(x)   if x == "banana":      break Output: apple banana Prof. Kartiki Deshmukh

The continue Statement With the continue statement we can stop the current iteration of the loop, and continue with the next. Prof. Kartiki Deshmukh

Example fruits = ["apple", "banana", "cherry"] for x in fruits:   if x == "banana":     continue   print(x ) Output: apple cherry Prof. Kartiki Deshmukh

The range() Function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Prof. Kartiki Deshmukh

Example for x in range(6):   print(x ) Output: 1 2 3 4 5 Example for x in  range(2, 6):   print(x ) Output: 2 3 4 5 Prof. Kartiki Deshmukh

Else in For Loop The  else  keyword in a  for  loop specifies a block of code to be executed when the loop is finished. Prof. Kartiki Deshmukh

Example for x in range(6):   print(x) else:   print("Finally finished !") Output: 1 2 3 4 5 Finally finished! Prof. Kartiki Deshmukh

The  else  block will NOT be executed if the loop is stopped by a  break  statement . Example for x in range(6):   if x ==  2:  break   print(x) else:   print("Finally finished !") Output: 1 Prof. Kartiki Deshmukh

Nested Loops A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop" Prof. Kartiki Deshmukh

Example color = ["red", "big", "tasty"] fruits = ["apple", “mango", "cherry"] for x in color: for y in fruits: print(x, y) Prof. Kartiki Deshmukh

Output: red apple red mango red cherry big apple big mango big cherry tasty apple tasty mango tasty cherry Prof. Kartiki Deshmukh

while loops With the while loop we can execute a set of statements as long as a condition is true. Prof. Kartiki Deshmukh

Example i = 1 while i < 6: print(i) i += 1 Output: 1 2 3 4 5 6 Prof. Kartiki Deshmukh

The break Statement With the break statement we can stop the loop even if the while condition is true: Prof. Kartiki Deshmukh

Example i = 1 while  i < 6:   print( i )   if  i == 3:     break    i +=  1 Output: 1 2 3 Prof. Kartiki Deshmukh

The continue Statement With the continue statement we can stop the current iteration, and continue with the next Prof. Kartiki Deshmukh

Example i = 0 while  i < 6:    i += 1   if  i == 3:     continue   print( i ) Output: 1 2 4 5 6 Prof. Kartiki Deshmukh

The else Statement With the else statement we can run a block of code once when the condition no longer is true. Prof. Kartiki Deshmukh

Example i = 1 while  i < 6:   print( i )    i += 1 else:   print(" i is no longer less than 6 ") Output: 1 2 3 4 5 i is no longer less than 6 Prof. Kartiki Deshmukh