Starting with the technical definition of for loops:
Like all the other functions we have seen so far in the video tutorials, for loop is also just a programming function that iterates a statement or a number of statements based on specific boundaries under certain defined conditions, that are the basis of the loop.
Note that the statement that the loop iterates must be present inside the body of the loop.
Regarding loops, iteration means going through some chunk of code again and again. In programing it has the same meaning, the only difference is that the iteration depends upon certain conditions and upon its fulfillment, the iteration stops, and the compiler moves forward.
For a beginner or layman, the concept of the loop could be easily understood using an example of songs playlist. When we like a song, we set it on repeat, and then it automatically starts playing again and again. The same concept is used in programming, we set a part of code for looping and the same part of the code executes until the certain condition that we provided is fulfilled. You must be thinking that in song playlist the song keeps on playing until we stop it, the same scenario can be made in case of loops, if we put a certain condition that the loop could not fulfill, then it will continue to iterate endlessly until stopped by force.
An example of where loop could be helpful to us could be in areas where a lot of data has to be printed on the screen and physically writing that many printing statements could be difficult or in some cases impossible. Loops are also helpful in searching data from lists, dictionary, and tuple.
Why do we use loops?
How to write a for loop?
For loop basically depends upon the elements it has to iterate instead of the statement being true or false. The latter one is for the While loop which is the topic for the next tutorial i.e. tutorial# 17. In different programming languages the way to write a loop is different, in java and C it could be a little technical and difficult to grasp for a beginner but in Python, it's simple and easy. We just have to declare a variable so we can print the output through it during different iterations and just have to use the keyword “for” and “in”, more explanation could be easily obtained about working and syntax through the video tutorial.
Advantages of loops:
# list1 = [ ["Harry", 1], ["Larry", 2], # ["Carry", 6], ["Marie", 250]] # dict1 = dict(list1) # # for item in dict1: # print(item) # for item, lollypop in dict1.items(): # print(item, "and lolly is ", lollypop) items = [int, float, "HaERRY", 5,3, 3, 22, 21, 64, 23, 233, 23, 6] for item in items: if str(item).isnumeric() and item>=6: print(item)
Name: Manav Sengupta Solution: list = ['Eggs', 'chicken', 5050, 12,56,345,35355] for list in list: if(type(list)==int): print(list) print(147*"*"+"\n*"+145*" "+"*\n*"+9*" "+"Thanks for checking out my code, and a ton of thanks to CodewithHarry Channel for all the Quality Content for Free"+22*" "+"*\n*"+145*" "+"*\n"+147*"*") # Gratitude
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials
#To check in the list whether the numbers are less than 6 using for loop list1=[5,6,8,4,9,1,2,13] for item in list1: if item>6: print("The number is greater than 6=",item) else: print("Number is less than 6=",item)
Thank you sir
list1 = ["alpha", "Charlie", "Hexa", 4, 45, 87, 1, 2, 8, 3, 5] for items in list1: if (type(items)) == int and items > 6: print(items)
name - Arvin CODE list1 = ["harry" , "arvin",2,3,4] for item in list1: ty = type(item) if ty == int : print(item) elif ty == str: print("Can't iterate string")
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here