Python Beginner Tutorial #5 - Loops
Key Takeaways
This video tutorial covers the basics of while loops and for loops in Python, including loop control statements such as break, continue, and pass statements, and demonstrates how to use these loops to automate processes and count.
Full Transcript
what is going on guys welcome to this Python tutorial series for beginners today's episode number five will be about loops now in the last video we talked about conditions and if statements and today we're going to take a step further and start automating processes with loops now in Python we have two types of loops while loops and for loops and these work quite differently so let us just start out with the first type of loop which is the while loop so let's open up our script and what a while loop basically does is it executes a certain piece of code over and over again as long as a certain condition is met or in other words we set a condition and as long as this condition returns true we run the same code with every iteration over and over again when the condition returns false we terminate the loop so for example we can define a variable x equals 0 and we can say while X is less than 10 so while it's the keyword here and we can say wow this condition returns true we execute a statement now of course this condition right now is true so if I just let it that way and say for example print index or something we would get into an endless loop because you know X will always be less than 10 here and it will always execute the statement so it will go on forever so what we could do is we could increase X by 1 with every iteration so what happens then is we say okay X is less than 10 because it's 0 then we increase it by 1 and X is 1 so we print 1 then we get again into the loop so we check ok 1 is less than 10 so we again make or do an iteration so we do this over and over again until X reaches 10 because then 10 is no longer less than 10 and we terminate the loop so in this case what would happen is we would count from 1 to 10 a popular way to make an endless loop is to just say while true because sometimes in programming we need to endless loops that go on as long as we or until we terminate them manually so we say while true and something I don't know prints something usually we don't print in endless loops but this would happen so if you want an endless loop for some reason you can use while true our for loops are the second type of loops and they work quite differently a full loop actually iterates over a sequence but since sequences are the topic of the next video and we haven't talked about them yet we're going to look at for loops in a practical way for now so we we can use for loops for counting or telling Python how many times to do or to execute a certain piece of code so basically how I do this is I say for and then I say for X in range I don't know some number 10 or 20 whatever let's say 20 and then I can put my code here so I can save print X and what this does is it prints from 0 to 19 because actually we start counting from 0 20 times so we got 20 numbers but we start counting from 0 so we get from 0 to 19 obviously if you want to change that we can also go with 1 and 21 because the last number it doesn't count so basically I say 1 or 21 would give me 1 to 20 actually so what I'm doing here is I have my control variable this is a control variable X here that iterates over this sequence so as I said we're not going to talk about sequences but you can imagine this range function to give me some collection of numbers in this case the numbers from 1 to 20 and what I do with access I say with every iteration X becomes the next number so in the first iteration X is 1 and the second actor iteration is it's 2 and so on but if I would have different numbers for example 21 to 41 and the first iteration acts with 21 in the second 22 and so on so I can use this to just count basically or I can use this to tell the interpreter how many times to do something so for X and R age 20 let's say print the world 20 times this would also work now we're going to talk about the application of full loops in the next video in more detail because when we deal with sequences for loops are quite important there but as I said for now we're not going to talk about for loops and sequences so just know for this episode that you can count and you can automate processes with for loops now one thing is quite useful when it comes to loops are the so-called loop control statements these allow us to manually control our loops now imagine we have a loop again from X 0 while X is less than 10 and at some point I want to break this loop just manually so I say I don't know again X plus equals 1 and print X but if X reaches the value 5 I just want to terminate the loop so it doesn't matter if the condition is met or not if X reaches the value of 5 what I do is I break the loop so this is the first loop control statement that we're going to talk about the break statement a break statement just terminates there's nothing more to it it just says okay it's over now we continue with the rest of the code because the while loop is now broken and in this case doesn't make a lot of sense but in some cases when we have some loops some very big loops some some loops with a lot of if statements in there we might have a certain point in the loop where we don't care about the condition because if something happens we would just want to break the loop it doesn't matter especially when we have while true loop so endless loops we use break statements quite often because it's basically the only way to exit in an endless loop so if you want to terminate just use the break statement another statement that would be quite useful is the continuous statement because the continue statement does not break the whole loop it just keeps the current iteration so let's say I want to increase the numbers all the time but when ax reaches the value 5 I still increase it but I don't want to print a value 5 for some reason so I can say if x equals nope actually wrong if x equals 5 I say continue this is the next statement the loop control statement so if X is 5 we still get to this part of the loop because it says okay X plus equals 1 but then X reaches 5 and we just skip this iteration so we don't print X and the next thing that happens is again increasing X by 1 so we will print the number 4 and the number 6 but not the number 5 this is basically how it works and you'll see that the 5 is missing here and actually I can also also show you the break statement because I think I didn't demonstrate what it does in this case it just breaks the loop and the continue statement is as a sad only used to skip one iteration so if something is is the case we can check if something happens that that causes us to want to skip this iteration we can do this and this is what the continue statement is for basically now the last statement that we're going to talk about is not really a loop control statement but it is a very useful statement when it comes to loops if statements functions and so on it is a so-called path statement and this statement allows us to fill places in our script where code is needed but we don't really know what we want to put in on this place yet so for example we could say if X is I don't know 25 just something and we want to do something here but we wanted to find what to do later on so for now we just want to have this if if statement here but we still have some code here afterwards that we want to execute but our compiler or in this case or interpreter will not allow us to run the module when we have an empty statement so to just fill it without any functionality to just make it runnable if you want you can use a past statement because let's say I have a print statement here one two three whatever and I don't have this past statement here I will get an error because when I run it will say expect it an indented block because you know there's nothing there but if I add this past statement actually nothing happens but okay in this case ax is not too fine of course so I'd say X is 25 now the condition is met but nothing happens we just print a statement so it basically passes this if statement so we just use this keyword to fill our code to make it runnable if we don't know what to do yet and we can do this not only with if statements we can do this with while loops with full loops with everything actually so we can do this wherever we want so that's everything about loops for this video in the next video we're going to talk about sequences lists and tuples there's a lot more to learn here so stay tuned and keep watching these videos if you liked the video please hit the like button and also if you want to see more subscribe to this channel and feel free to ask questions and give feedback in the comment section down below thank you very much for watching and see you in the next video [Music]
Original Description
In today's episode we are learning how to work with while loops and for loops!
Website: https://www.neuralnine.com/
Instagram: https://www.instagram.com/neuralnine
Twitter: https://twitter.com/neuralnine
GitHub: https://github.com/NeuralNine
Programming Books: https://www.neuralnine.com/books/
Outro Music From: https://www.bensound.com/
Subscribe and Like for more free content!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 6 of 60
1
2
3
4
5
▶
7
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
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
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
🎓
Tutor Explanation
DeepCamp AI