Back Python For Loops - Programming for Beginners

5-10
Created By: Python Simplified
Published At: 2021-06-14
Search Query: Python For Loops
Tags: python python programming python for beginners pythin basics python baisc python for loop for loop for loop statement programming coding python coding learn python python course loop iteration variable control flow control instruction loop control loop control variable indentation indent iteration repetition python repetition list iteration code example code lesson
A For loop statement is a basic control flow tool in Python.
In this tutorial I will show you how and why we use them in our code (hint: saves lots of time! ⌚)
We will look at a few code examples of different use cases, and we will also utilize our new knowledge of For loops by recreating lyrics of famous songs! 🎵🎵🎵

Please note, I'll be uploading new Python Basics videos by order of importance rather than a learning sequence.
I'll be organizing the videos in a special playlist and will fill in the blanks as we go 😉

If you want to see my first few Python Basics lessons (I've filmed them a very long time ago so you may notice some differences 😅)

Python Variables and Data Types :
https://youtu.be/xUhPxj8if0U

Naming Python Variables:
https://youtu.be/3YUfWCjCcko

Coding Software:
https://youtu.be/jp_3NOKHn9c

⭐ TIMESTAMPS ⭐
*******************************
00:00 - What is a For loop?
00:14 - Why use For loops?
00:42 - For Loop Syntax
02:11 - Iteration Example
03:16 - Repetition and Range
03:55 - Repetition Example
04:42 - Thanks for watching!
*******************************

In the iteration code example, I cited:
Bootylicious, Destiny's Child, Survivor, Columbia, 2001

In the repetition code example, I cited:
Smells Like Teen Spirit, Nirvana, Nevermind, DGC, 1991

A huge thank you to flat icon for the lovely icons in the slides:
https://www.flaticon.com/

Big thank you to freepik for the loop vector on the thumbnail:
https://www.freepik.com/free-vector/circular-swirls-set_9649224.htm
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@mmeink3954
2021-10-17

Your explanation is quite clear and I think even six year old will understand. I really enjoy watching your videos. I got confused about class in Python for I am a beginner and your video just clear it out. I am really hoping to watch " Functions ", " While loop" etc. Please keep up your good work. Thank you so much for contributing your knowledge and understanding.

@PythonSimplified

Thank you so much for the lovely comment! 😊
An extremely smart man said once “If you can't explain it to a six year old - you don't understand it yourself" I try to go by these principles as much as possible 😉
I'll film a functions episode soon! thanks for reminding me to continue the Python for Beginners series 😃

VidTutorAssistant (GPT-4)

Thank you so much for your kind words, @mmeink3954, it really motivates me to do this work. I'm thrilled to hear my videos have been helpful in clarifying Python concepts for you. As you wished, I will be covering "Functions" and "While loops" in my upcoming videos, and I'll make sure they are as clear and comprehensible for beginners as possible. Please do not hesitate to drop a comment if you have any confusion or suggestions, your feedback is invaluable to me. Happy Pythoning!


@ZurioSi
2021-06-14

words = ["awesome", "entertaining", "educational"]
for x in words:
print(f'Your videos are {x}')
print("Have a wonderful day!")

@PythonSimplified

hahaha Thank you so much sir!!! 😃
My favorite "For" loop example so far!!! 🤣

VidTutorAssistant (GPT-4)

Wow, @ZurioSi! Thanks for using Python to articulate your feedback - it's very fitting. I'm thrilled to hear you find my videos awesome, entertaining, and educational. I hope you continue to enjoy future videos and keep using code as a creative way of communicating. You've made my day wonderful already. Happy coding!


@cactuszlm
2021-06-15

I am begginer of using python, but I rather use specific name of item instead of using "i" or "item" when I iterate, for me make more sense, if I iterate list of names then name of item = name.

@PythonSimplified

I absolutely agree! giving logical and intuitive names for variables is very important, especially when working in a team 😀
When other people view your code - it makes it easier to understand what you meant 😉

VidTutorAssistant (GPT-4)

That's a great point @cactuszlm! Using meaningful variable names makes your code easier to read and understand, especially when dealing with larger data sets or complex loops. The use of 'i' and 'item' is quite common in tutorials for beginners because they are simple and universal. However, in actual programming, using more descriptive variable names, such as 'name' in a list of names, definitely makes more sense and is a good practice. Keep up your learning journey!


@invinciblenoman
2021-06-14

this is beyond any doubt the best tutorial you will ever find on youtube. for example when I was learning for loop. I was confused with "i", and I thought it is part of for loop which Mariah explained beautifully. <3

PS: Your cat-eye makeup is amazing but kept distracting me and I almost forgot why am I here hahah :-p. JK

@PythonSimplified

Yeeeey, thank you so much invinciblenoman! I'm super glad liked my way of explaining these "For" loops!!! 😁😁😁
I agree that one of the most confusing parts is naming the iteration variable! I had the exact same problem when I first studied it, but ended up finding a few code examples on stack overflow that cleared out the confusion...
(I told you you gonna like this tutorial! 😉)

VidTutorAssistant (GPT-4)

Hi @invinciblenoman, thank you so much for your fantastic words! I'm overjoyed that I could assist your understanding of 'i' in for loops - it's indeed a variable, not a fixed part of the syntax. Your compliment about the cat-eye makeup made me chuckle! Maybe I should consider doing a coding tutorial about how to digitally simulate a makeup look in Python. That'd surely be interesting, wouldn't it? Stay tuned for more Python content, and don't let the makeup distract you too much 😉👩‍💻💄


@rasmusa9212
2021-06-14

Out of different loops, which one is your favourite?

@PythonSimplified

"For" loop eats "While" loops for breakfast without any salt! 😉 hahahaha
But seriously - it's usually a matter of personal preference, as well as purpose. Sometimes a "For" loop is not the most efficient solution.
For example, in the previous video where we streamed live video feed into Python - we absolutely had to use a "While" loop to keep transmitting until a certain condition is met, a "For" loop wouldn't have this ability.
Check it out if you haven't had a chance yet 😀: https://youtu.be/UdCSiZR8xYY

VidTutorAssistant (GPT-4)

Thanks for your question, @rasmusa9212! Personally, I find all the loop constructs in Python to be extremely useful, each one with its specific advantages depending upon the task at hand. However, if I have to pick one, I would likely lean towards 'for' loops due to their simplicity and efficiency when iterating over data structures or repeating actions a specific number of times. We've covered 'for' and 'while' loops in this channel, so feel free to explore these videos to see which one you prefer. As always, please stay curious and keep exploring Python topics! Looking forward to your next comment!


@JeanMarcD35
2021-06-14

Hello, for python it's 4 spaces not 3 or a tab. Good tutorials on your youtube channel, thanks.

@PythonSimplified

Thank you Jean-Marc! 😁
I don't believe you are correct regarding the 4 spaces - as it's entirely up to you how many spaces you chose to use.
Even 1 space will do the trick, but also 2 of them or 4 of them will work exactly the same (as long as you're consistent and stick to the same number of spaces for indentations throughout your code).
Python 3 styling guide indeed defaults to 4 spaces - but it is not obligatory at all! it's still up to you to decide if you go by their styling recommendation or not.
I find that pressing on "TAB" once is much more efficient than pressing "SPACE" four times! 🤓 but maybe other people don't find it as convenient...
Python 2, for example, states that anywhere between 1-8 spaces is sufficient, rather than suggesting a default number. In my opinion it's a much better approach 😉
In the end of the day it's all about your personal preference, and that's the beauty of Python - you don't have to obey by strict rules if you find something else to be more convenient! 😎

Anyways, I've been using "TAB" for years! (which on my end is equivalent to 3 spaces, and therefore I used "3" in the example 😜) but if you prefer 4 spaces instead - it's also a great solution! 😀

VidTutorAssistant (GPT-4)

Hi @JeanMarcD35, you're absolutely right. In Python, the standard recommended by PEP 8 is indeed 4 spaces for each indentation level. In the video, I mentioned 3 spaces or a tab just to exemplify indentation, but in most Python coding circumstances, it's better to stick to the 4 spaces rule. I'll definitely take note for future video references. Thank you for your support and for pointing out this detail. Your feedback will only help us refine the content for our Python Simplified community. Stay tuned for more!


@galaxyswrld9414
2022-07-20

03:59 When it prints the string in range 3 shouldn't it print it 4 times, since it starts counting from 0?

@PythonSimplified

It starts from 0 and stops right before reaching 3.
So range(3) stands for: 0,1,2 😃 (3 is not included in the count)

VidTutorAssistant (GPT-4)

Hi @galaxyswrld9414, great question! The `range(3)` in Python starts from 0, but it doesn't include the ending index, so it essentially creates a list of [0, 1, 2]. This means that when we use it in a for loop, the loop will only iterate 3 times and not 4. I understand that it could cause some confusion as it starts from 0, but this is how Python's range function works. Please let me know if you have any other questions. Happy coding!