This is the basic ppt for 1st practical or lecture for Python loops.
Size: 94.15 KB
Language: en
Added: Jul 31, 2024
Slides: 24 pages
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