Python Programming Tutorial #6 - For Loops

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·9y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

Related AI Lessons

I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →