Python Beginner Tutorial #5 - Loops

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches the basics of while loops and for loops in Python, including how to use loop control statements to automate processes and count. It provides a comprehensive introduction to loops in Python and demonstrates how to use them in practice.

Key Takeaways
  1. Use a for loop to automate a process
  2. Use a while loop to repeat a piece of code
  3. Use a break statement to terminate a loop
  4. Use a continue statement to skip an iteration
  5. Use a pass statement to fill in code without functionality
💡 The pass statement is used to make code runnable when there's no functionality to execute, and it does not execute any code, it simply passes the control to the next statement.

Related Reads

📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Learn how to build a free online image and PDF processing platform using Vue 3 and FastAPI, and discover the benefits of combining these technologies for efficient file processing
Dev.to · IAMUU
📰
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
Up next
I Asked Gemini to Build a Dashboard... I Didn't Expect This
Patech
Watch →