Python Programming Tutorial #6 - For Loops
Key Takeaways
Explains for loops in Python programming using examples and syntax
Full Transcript
Hey guys, welcome back to another video. This is the sixth video in my Python programming series and today we're going to be talking about four loops. Again, if you missed the other videos in the series, please go back and watch them first as it's going to be hard to understand if you haven't seen them already. Okay, so let's get right into it. Today we're going to be talking about for loops. Um, and a loop is a new thing now if you're a beginner in Python. And I'll talk about how they work. We're going to start off by just writing one out. um getting right into it with the syntax. So for x in range and then we're going to put a number which is going to be our starting position. So in this case I'm going to put zero. Um and then a stop point. So 10. Okay. And then we're simply going to print out x. Now I know we did this pretty quickly but let's talk about what this is and the syntax. So a for loop is going to run a certain amount of times. So in this case, we're going to start at zero and we're going to go to 10. This means the for loop is actually going to run 10 times. So what's going to happen is we are going to have our variable x which is right here. And every time the for loop runs, x is going to be increased by one. Now we can increase it by more if we'd like to by adding another comma here, but by default it is set to one like this. I've just put it in so that you can visualize it. So really, we have something start, a stop, oops, stop, and an end. Start, stop, step. Sorry, my bad. I'll just put commas here to show you how it works. So you can see we have our zero. That's our starting number. Our stop, which is 10, and our step, which is going to be one. So I know you don't quite understand yet, but you will after I show you a few examples. So we have x is our variable which is going to be holding the number um of what iteration we are through. Now iteration uh just means how many times we've already looped through this loop. Okay. So let's uh do an example here. All right. So we have x and we've just started running this for loop. Now right now x is zero. We're going to print x. We're going to print zero to the screen and then we're going to add one to x. Now this time we come through the loop we say well is x less than 10? Yes it is. So now we're going to print x. x is one. Now we're adding um two. Okay. So now x is 2. Is 2 less than 10? Yes. 2 is less than 10. So let's print two. Okay. And we keep going until eventually we print n. Then x has one added to it. So it becomes 10. Well 10 is not less than 10. So we're not going to print out 10. All right. All right, I know I just talked a lot there, but let's see it in action. All right, so just like I said, we print out the numbers 0 through 9. Now, this may seem weird um because I have the value 10 here. So, you'd say, well, why doesn't it print out 10? It's because of the way the loop works. So, like I said, really what's happening here is this um plus equals 1. So, pretty much this means we're just adding one to the variable x. Um you can also write it like this. X= X + 1 like that. All right. So we print out X and then we add 1 to X and we have to be less than 10 for this loop to run. So on the 9th iteration we have well 9 is equal to 9 + 1. So X is equal to 9 + 1. Uh we get the value 10. Now again 10 is not less than 10. So we are not going to print out 10. Um yeah. So, that's pretty much how a for loop works. Um, it's pretty basic. This is the syntax. We have four and then a variable name. So, this doesn't have to be x. We could use anything we'd like to use. Could use hello. I could use um any other word. It could be as long as I want, as short as I want, doesn't matter. Um, then we have the word in, which is highlighted in orange. And then range. Now, the range takes a different amount of arguments. pretty much an argument is whatever's inside this brackets. So like at the beginning how I showed you we just did zero and 10. This means we have a start and we have a stop. So we start at zero and we go to 10. Now I can also I can actually put one argument in here like this 10. And that just means that we just have a stop. So by default Python will say well we're just going to start at zero and we're going to step by one. Now I haven't showed you the step yet. So we'll do that. Um, so again, we're going to start at zero. We're going to stop at 10. And we're going to step this time by two. Now, what step is is how much we add to x every time. So before I showed you we had x = x + 1. Um, in this case, since the step is 2, it's going to be by two like that. Okay. Um, so let's go ahead. Let's run this. We have 0 2 4 6 8. And you notice how we again do not print out 10 because 10 is not less than 10. Okay. Again, uh we'll do another example with the step. Let's put five in this time. Now you can guess what's going to happen. 0 five. It actually only prints out two things because we start at zero. So we print zero. And then we add five. We get five. Now 5 + 5 again is not less than 10. So we are not going to print out 10. Okay. So, that is the tutorial on the for loop. We're going to do another type of for loop where we're going to use something called a list a little bit later on, but right now we're just using this one. This is the basic one. I encourage you to play around with it yourself. Um, and yeah, so if you like the video, please go ahead and like it and subscribe, and I will see you again for another video. Thanks. Goodbye.
Original Description
This is the sixth video in my python programming tutorial series. In this video I explain in detail an important aspect of any language, the for loop.
Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/for-loops/
As always don't forget to LIKE and SUBSCRIBE!
Video Tags:
python,python tutorial,python language,python full course,python course,learn python,learn python programming,python tutorial for beginners,python tutorial 2018,python programming tutorial,python programming language,software development,programming tutorial,techwithtim
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 7 of 60
1
2
3
4
5
6
▶
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
🎓
Tutor Explanation
DeepCamp AI