Looping in Python
Loops are an essential part of programming language.
They are used for iteration over data types such as strings, list, tuples, sets and dictionaries.
For Loops iteration over strings.
string = "Learning Python is easy." for i in string: print(i) #PYTHON OUTPUT P y t h o n i s e a s y .
Looping through lists, tuple, sets and dictionary.
new_list = [ 1,2,3,4, ] #Looping list print("Looping through Lists\n") for i in new_list: print(i) new_tuple = ( 'Jake', 'Ronny', 'Peter', 'Sam' ) print("\n\nLooping through Tuples\n") for j in new_tuple: print(j) sets = { "Orange", "mango", "pine-apple" } print("\n\nLooping through Sets\n") for k in sets: print(k) new_dict = { "user_id" : 15, "username" : "Sam1947", "first_name" : "Sam", "last_name" : "Paker", "profile_pic" : "profile_picture.jpeg", } print("\n\nLooping through Keys Dictonary\n") for k in new_dict: print(k) print("\n\nLooping through Keys and Values Dictonary\n") for k,v in new_dict.items(): print("{} : {}".format(k,v)) #PYTHON OUTPUT Looping through Lists 1 2 3 4 Looping through Tuples Jake Ronny Peter Sam Looping through Sets pine-apple Orange mango Looping through Keys Dictonary user_id username first_name last_name profile_pic Looping through Keys and Values Dictonary user_id : 15 username : Sam1947 first_name : Sam last_name : Paker profile_pic : profile_picture.jpeg
Using `else` statement with for Loop. The `else statement` will be executed after the end of for loop.
new_list = [ 'apple', 'grapes' ] for i in new_list: print(i) else: print("Banana") #PYTHON OUTPUT apple grapes Banana
Using the `break` statement. These are helpful when we want to exit from the loop when certain specify condition matches.
new_list = [ 'apple', 'Banana', 'grapes' ] for i in new_list: if(i == "Banana"): break else: print(i) #PYTHON OUTPUT apple
Using the ‘continue’ statement. This statement skips execution on certain condition but unlike the break statement does not exist loop.
new_list = [ 'apple', 'Banana', 'grapes' ] for i in new_list: if(i == "Banana"): continue else: print(i) #PYTHON OUTPUT apple grapes
Python range() method
In simple terms calling range() method with parameters will get us a list of numbers which can be used for iterating over loops.
syntax :
range('start number', 'stop at number', 'number to increament on each loop[Optional parameter]')
Printing list of whole numbers using `range()` method.
for i in range(1,5): print(i) #PYTHON OUTPUT 1 2 3 4
Printing list of odd numbers using `range()` method.
for i in range(1,10,2): print(i) #PYTHON OUTPUT 1 3 5 7 9
Nested for loop using dictionaries.
d={ 'users' : { 0 : { 'username' : 'Rocky1987', 'fname' : 'Rocky', 'lname' : 'S', 'DOB' : '1987-10-05', 'hobbies' : { 'swimming', 'reading books', 'football', } }, 1 : { 'username' : 'Harry12', 'fname' : 'Harry', 'lname' : 'J', 'DOB' : '1995-10-05', 'hobbies' : { 'driving cars', 'mountain climbing', 'volley ball', } } } } username = "Harry12" for k,v in d.items(): if 'users' in k: for i,j in v.items(): if username == j['username']: print('Username exits in dictonary \n {}'.format(j)) print('\n\n*******************************\n\n') print('user hobbies are \n {}'.format(j['hobbies'])) else: print('key not found') #PYTHON OUTPUT Username exits in dictonary {'username': 'Harry12', 'fname': 'Harry', 'lname': 'J', 'DOB': '1995-10-05', 'hobbies': {'mountain climbing', 'driving cars', 'volley ball'}} ******************************* user hobbies are {'mountain climbing', 'driving cars', 'volley ball'}
While Loop
This loop working is similar to for loop but assigning parameters to vary. Keyword `while` is used before calling condition.
To print even numbers from 1 to 10
k=1 while k<10: if k%2==0: print(k) k+=1 #PYTHON OUTPUT 2 4 6 8