FOR LOOPS IN PYTHON

 

For Loops In Python

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, which are the loop's basis.

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 programming, 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 the song's 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 the song playlist, the song keeps on playing until we stop it. The same scenario can be made in the case of loops, and 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 a 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, dictionaries, and tuples.

Why do we use loops?

  • Complex problems can be simplified using loops
  • Less amount of code required for our program
  • Lesser code so lesser chance or error
  • Saves a lot of time
  • Can write code that is practically impossible to be written
  • Programs that require too many iterations such as searching and sorting algorithms can be simplified using 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 it's simple and easy in Python. We just have to declare a variable so we can print the output through it during different iterations and use the keywords “for” and “in”. More explanation could be easily obtained about working and syntax through the video tutorial.

 Advantages of loops:

  • The reusability of code is ensured
  • We do not have to repeat the code, again and again; we just have to write it one time
  • We can transverse through data structures like list, dictionary, and tuple
  • We apply most of the finding algorithms through loops

Example of a for loop:

dict1= {"Best Python Course": "CodeWithHarry",
        "Best C Languge Course": "CodeWithHarry",
        "Harry Sir":"Tom Cruise Of Programming"
        }

for x,y in dict1.items():
    print(x, y)
Output:
Best Python Course: CodeWithHarry
Best C Languge Course: CodeWithHarry
Harry Sir: Tom Cruise Of Programming

You can see that how I've printed the key-value pairs of the dictionary dict1 using a for loop.

Code file as described in the video

# 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)
#list1 = [ ["nitin", 1], ["neeraj", 4], ["sachin", 7], ["sapna", 10], ["shalu", 15] ]
#for item in list1:
# print (item)
#dict1 = dict(list1)
#print (dict1)
#for item, lollypop in list1:
#for item, lollypop in dict1.items():
# print (item, "lollypop is", lollypop)
#for item in dict1:
#print (item)
list1 = ["shaly", "herry", 6, 25, 65, 2]
for item in list1:
if str(item).isnumeric() and item>=6:
print (item)

Comments

Popular posts from this blog

HOW IMPORT WORKS IN PYTHON?

Exercise 5: Health Management System

*args and **kwargs In Python