A post for For Loops

A For loop in python is used to loop over elements in a list,tuple,dictionary. So other then the before introduced “while loop” here you don’t perform a certain task so long as a defined condition remains true, but until you cover the elements defined in a list, tuple, dictionary etc.

Let’s start with a simple example. The fans want to shout every letter of their favorite player in this example its Kroos:

Like this every single letter will be printed.

We can print lists as well and notice here that they can include different datatypes:

An important factor while using For loops is the range()-function. Here you can e.g. define a range of numbers which you would like to have printed. Be aware that Python starts counting at zero and counts up. If you want to count down you have to explicitly state this with a “-1” at the end:

Or imagine you want to print the number from 1 to 20 and leave out numbers which can be divided by 5 you can combine the loop with an IF-clause:

I hope you understood the functionality of the For Loop in Python. I can recommend this video to get more information about them:

#Mastery14

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s