🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Python FOR Loops (Iterators in Python) – Definition and Syntax
Loops in Python are an efficient method for optimising your code, and executing multiple statements. If the same block of code has to be executed, multiple times, over and over: you can put it into a loop, to perform multiple iterations, and get the same desired output. Consequently: it saves a lot of effort, and reduces complexity of the code, as well.
Basically, there are two types of iteration in programming languages:
– Indefinite iteration: In which the code block executes, until some condition is met. This iteration is performed using while loops, and will be discussed in the very next upcoming video.
– Definite iteration: In which the number of repetitions is specified, explicitly – and in advance. This kind of iteration is performed using for loops, and will be discussed in the next section.
○ FOR Loop Syntax
for var in sequence:
statement_1
statement_2
...
Here, is a simple declaration of FOR loop, in which the variable “var” takes the value of an item inside the sequence, and on each iteration. Loop executes ALL statements and it continues until we reach the last item of the sequence. The body of loop, which includes some statements, is separated from the rest of the code, using indentation.
This loop can be described entirely in terms of the concepts you have just learned about. Thus, in order to carry out the iteration this FOR loop describes, Python does the following steps:
– Calls iter(), to obtain an iterator for the iterable ;
– Calls next() repeatedly, to obtain each item from the iterator ;
– Executes the loop body: once for each item, and set the variable to the given item ;
– Terminates the loop, when next() raises the StopIteration exception, then continues.
○ Basic Example of FOR Loop in Python
Let’s have a more practical and very simple example, so you can discover and understand how to properly use Python FOR loops, in your program. First, declare a list of strings and add a few strings to this list. Then, you will iterate through this list using FOR statement, and print each item. Very simple, Right?
colours = ['red', 'green', 'blue', 'yellow']
for colour in colours:
print(colour)
The execution will start and look for the first item in the sequence or iterable object. It will check whether it has reached the end of the sequence or not. After executing all of the statements in the block, it will look for the next item in the sequence and the process will continue over and over again, until the execution has reached the last item in the sequence.
In the above example, the execution started from the first item in the list colours, and it went on until the execution has reached its last item – which is yellow. This is a very simple example of how you can use FOR Loops in Python.
Of course, there are many other alternatives you can use, and go through any iterable object. Here is another example when you want to iterate through every single character of a string – because a string is a list of character, remember?
my_string = 'python'
for letter in my_string:
print(letter)
And here you are, you just wrote your first Python iteration, using FOR loops. This very simple code will iterate through a list and print out every single item of that list. Was not that hard? Let’s move forward to the next section!
Let’s play this video, stick around and watch until the end of this video! 👍🏻
– Digital Academy™ 🎓
***
☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho
☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw
📖 Blog: http://digital.academy.free.fr/blog/
📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners
🛒 Shopping and Discounts: http://digital.academy.free.fr/store/
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧ https://twitter.com/DigitalAcademyy
✧ https://www.facebook.com/digitalacademyfr
✧ https://www.instagram.com/digital_academy_fr/
✧ https://www.youtube.com/c/DigitalAcademyOnline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ https://www.patreon.com/digital_academy
✧ https://www.buymeacoffee.com/digital_academy
***