Python Loops (Visually Explained) | For, While, Break, Continue, Else

Download information and video details for Python Loops (Visually Explained) | For, While, Break, Continue, Else
Uploader:
Data with BaraaPublished at:
9/2/2025Views:
7.6KDescription:
Python loops let you repeat code blocks until a condition ends the cycle. A **for** loop iterates over a sequence (list, tuple, string, range, etc.). It automatically creates an iterator, fetches each element, and runs the block until the iterator is exhausted. A **while** loop runs as long as a Boolean expression is true; the programmer must update the condition to avoid infinite loops. Both loops can be controlled with **break**, **continue**, and **pass**: * `break` leaves the loop immediately. * `continue` skips the rest of the current iteration and starts the next one. * `pass` does nothing, acting as a placeholder. The **else** clause on a loop executes only if the loop terminates normally (no `break`). It’s useful for “no‑match” cases, such as validating data or searching a list. Nested loops run one loop inside another, enabling Cartesian products, hierarchical traversal, or multi‑level processing (e.g., iterating tables → columns → rows). Typical real‑world uses: * **Data ingestion** – loop through files or database tables. * **Data cleaning** – iterate columns to strip whitespace or convert types. * **Validation** – search for missing or invalid entries; break on first error or finish all checks. * **Automation** – generate SQL queries or file names by nesting loops over tables, columns, and rows. Choosing a loop: * Use **for** when the number of iterations is known or bounded by a sequence. * Use **while** when the stopping condition depends on runtime events or user input. Both constructs, together with conditional statements, give you full control over the program’s flow, turning a linear script into a flexible, reusable tool for data pipelines and beyond.
Video Transcription
Hey friends, so now we're going to deep dive into something very powerful in Python.
We're going to talk about loops.
So now let's start with the first question.
What are loops?
A loop is like a tool that we use to control the flow of our codes.
And if you use it, you are saying like, hey, I would like to repeat this code, this thing over and over until I say stop or something happened.
Maybe a condition is not fulfilled anymore.
So now let's have a sketch to understand what this means.
So now normally as we are coding, we write instructions one after another.
And if you go and execute it, Python has to go from the top to the bottom, executing all your instructions one after another.
And all your instructions will be executed from Python.
So it is a straight line code.
And that's great for simple stuff.
But in reality, as you are coding, what could happen is that you will notice there is like a block of code that keep repeating.
It could be repeated like 5, 10 or even 100 times.
That means we are repeating ourselves.
And this is not really good because our code can explode.
Instead, what we could do, we could put our code in autopilot.
So what we can do, we write this block of code only once and then we attach it to a loop.
This loop gonna go and execute the block of code over and over as long as the condition is true.
So with that, you write your code only once and you repeat it as much as you need.
And in Python and as well in many other programming languages, there are two main types of loops.
We have the for loops and the while loops.
And each type has different types and purposes.
Now we're gonna deep dive into the for loops.
So let's start.
A for loop lets you go through a group of items one by one in order to do something for each item.
So it's like you are saying do something for each item in my list.
So let's understand exactly what this means.
So now we start in our code with the keyword for in order to build the loop.
And once Python sees the for keyword, it's gonna go immediately behind the scenes and start building a logic, a process.
So it has a start and as well a condition.
And of course, this condition is not specified from us in the code.
It is something that Python do once it sees for.
So the condition gonna be always like this.
Is it the last item?
So it is actually asking, am I done?
Is it the last item in the group of items?
You will understand exactly what I mean with this once we have an example.
And as usual, the answer for each condition could be either true or false.
So this is what can happen once we specify a for.
Now, next, we have to define the loop variable.
So it's like a variable that we're going to use to assign the current value of a sequence.
You could name it whatever you want, an item, user, whatever.
And then we use the operator in.
And after that, very important is the sequence.
You define here the group of items that should be iterated.
So with that, we give Python the list that we would like to iterate.
And this is what we call a sequence.
now python behind the scenes gonna take this sequence and create it out of it an object in the memory called iterator this iterator is responsible for knowing where we are currently in the loop and what is the next item in the sequence so it keeps track on what is already done and what is next so with that we have now the setup the configuration of our for loop we know how we have to iterate three times because we have three items but still we have to tell python
What to do?
Which line should be repeated, right?
Of course, in order to do that, we have to do the double points and then go to a new line.
And don't forget here the intendations.
So we have to leave four spaces and then write the line that should be iterated.
Like, for example, it could be print the item.
And now Python knows this block of code should be repeated.
And this can be if the condition is false.
So as long as we still have items, this code should be repeated.
And of course, after the execution of the print, it's going to go back to the start in order to grab the next item.
And of course, if the condition is true, so we are at the end, Python going to go and end the loop.
And that means we can go and execute the next instruction.
So now let's see how Python going to execute this.
So as usual, we start from the beginning, and now it goes to the condition.
It's going to say, is it the last item?
So Python is going to go and ask the iterator, what is next?
And since we are at the beginning, it's going to go and answer, okay, the first one is one.
So the iterator is going to answer Python with the value one.
And this means the condition is not fulfilled.
It is not the last item.
We still have values in the iterator.
That's why it's going to go to the path of the false.
Now it's going to go and execute our block of code.
print i and here python assigns the value one to the variable i and that's why we need the loop variable in order to assign the values that we are getting from the iterator or the sequence and now once our code is done it's gonna go back to the start and then what's gonna happen my friend it's gonna go and ask again give me the next item so now the iterator gonna say okay we are done with the first item then we're gonna go to the second item
and return two.
So Python sees, OK, we still have values.
That's why we are not done yet.
It's going to go back to the print.
And this time I is equal to two.
And it's going to go and print it in the output.
And again, it's going to go back to the start and ask the same question.
Are we done?
Do we have anything next?
Now the iterators say, OK, we are done with the two.
Let's go to the next one, the three.
So I'm going to get three.
It is not done yet.
We're going to go and print it in the output and again, go back to the starts.
But this time, if Python ask the iterator what is next, it's going to see the three is the last item in the group.
So it's going to say for Python, sorry, we don't have anything else you
have to stop now once python gets the stop it's gonna say okay we reached the end we have processed the last item that's why this time the condition is fulfilled and it's gonna break the cycle and get out of the loop so we reach the end so as you can see our block of code the print i is executed three times we didn't have to repeat it three times in our code we just build a for loop for it and it get iterated again it's like autopilot
And by the way, my friends, if you are enjoying this type of free tutorials where I'm sketching the concepts behind the scenes and showing the codes and you would like to see more like this, then support the channel by subscribing, liking, commenting.
This really helps a lot.
So now let's go back.
All right, now let's say that we have the following example that we are printing like rounds.
So we are seeing round one and then we repeat this like five times.
So round one, two, three, four.
So now let's go and execute it.
You can see we are getting the exact same stuff.
So everything so far static, of course.
And of course, we are repeating the same code over and over.
But just the only thing that is changing is the round number.
now of course this is not very smart because we are repeating the same code over and over and instead of that we can write our code once and then put it in a loop so we can go and get rid of everything and then we say for i this is our variable and then we define the sequence
Like this.
One, two, three, four, five.
Now we go and add double points.
With that we have defined the structure of the for loop.
We are saying let's iterate through this sequence.
And then we hit enter.
Now we have to be careful with the spaces.
The intenditions here should be four spaces.
for example, and then we can go and grab our prints and put it over here.
So now the print is inside the for loop.
And now if you go and execute it, what's going to happen?
Python going to go and repeat this code five times.
And that's because in our sequence we have five values.
So that means we have five items in our list and Python has to go through them one by one.
And with that, we are getting the output.
But we are not there yet because we are printing the same code.
I would like to have the value of the sequence in the output as well.
Of course, the value of the sequence are always assigned to the variable.
So what you can do, you can go and use our string format.
and then we have to go and replace this static part with the loop variable so we don't need you anymore and here we have an i so now for each round we will get the value from our sequence let's go and execute it now you can see we are getting the exact same output so round one two three four five and those values are coming actually from the sequence so it is very simple right you just have to define the loop and then tell python what to do for each iteration
Now I would like to show you something like a rule for the naming of the variable and the sequence that we usually use.
So for example, we could put our sequence in a variable called items equal to this value.
And then we could use this variable inside our for loop.
You can see items, but usually we're going to call it like this for item in items.
So the variable name is a singular and the sequence is going to be like plural for the same name.
And it is really nice to read.
for item in items and if you have like tables you could say for table in tables for file in files so this is really nice just use the same name as a singular for the variable and plural for the sequence and of course we can go over here and call item and if you go and excuse it you will get the same results it is really cool you know immediately this variable here gets its value from the items
Now, there is something important to understand about the for loop that we have always to specify a sequence, a group of items that has a start and end.
In the current example where we have the numbers one, two, three, this is what we call a tuple.
It is a data structure that we can learn about it later.
Don't worry about it.
But we could use as well other sequences.
Like, for example, we could use a list.
A list is as well a sequence.
Let's have an example.
so now let's check that out if we go and remove this and have the brackets at the start and as well at the end so if you go and now execute it you will get the same results this is as well a sequence of numbers and python gonna treat it as a sequence so we could use as well list in our example and by the way for example let's go and
remove the five and add a string value like high this and now if you go and execute it you will get the last one round high so there is no rule in the for loop that the items must be either be numbers or string values you can go and do like a mix okay the next one that we could use is a string a string value it might like looks like one value but actually it is a sequence of letters so each character each letter is actually one value that means we can go and iterate through a string value let's
an example okay so let's try this out i'm gonna go and remove the whole thing over here let's have a value like python so this time we have like one string value and we're gonna go and iterate it let's go and check that out now as you can see each letter gonna be like one value and python gonna go and iterate through the whole string and of course if you have here some spaces it's gonna be considered as well as a character so you're gonna execute it you will get for the first round a space so with that you can see
our string value is not only one value it is actually a sequence of characters that has a start and an end and you can use it in the for loop as a sequence okay moving on to the next one what else we could use in the sequence that we can use a build in function called range so let's say that you would like to iterate like 100 times it's going to be really annoying to make a tuple with
1, 2, 3, 4, and like you define 100 values, right?
Instead of that, we could use this build-in function range to generate a sequence of numbers.
Now in this function, we can define many things.
Let's start with the one that is very common.
We specify a stop.
So we use the stop to tell when it must stop generating numbers.
For example, if you go and say range 5, the sequence will look like this.
It starts with 0, 1, 2, 3, 4.
So that the total generated numbers are 5.
And you can see we don't have the number 5 in the output because otherwise it's going to be 6 numbers.
And this might be confusing for money where they think if you add 5, you will see it as well in the output.
Well, actually, the stop will not be included because it starts from 0.
So let's try this out.
So now we have here again our sequence of numbers using a tuple Now let's go get rid of this and instead of items we will use the function range And we have to pass for it a value, let's pass the 5 and try this out Now as you can see in the output we start with around 0, 1, 2, 3, 4
So this is really nicer instead of writing like five numbers, right?
Now if you go and say 100 and execute, it's gonna be very simple.
It's gonna go and generate 100 numbers.
And with that, you iterate our code 100 times.
So now let's go back to our five example here and execute.
Now let's say that this is not what exactly I need.
I would like to start from the round one, not from zero.
So how we can fix this?
Now we could use in the range another argument.
We could use the start arguments.
this is optional if you don't specify anything it's going to be always the zero right but now we want our start that's why we can't go and specify the start by for example one and with that we are forcing python to start generating from one so now if you
execute it we will get a sequence that starts from one two three four and then it stops it will not include the five in the sequence because it doesn't include the stop so by looking to this the start will be always inside your sequence but the stop will not be inside so let's go and try this out now if you go over here and say okay start from one and execute now
now in the output it looks nicer we are starting from the round one as we want but we have the issue we don't have any more than round five now in order to have it in the output we can go and increase the stop to six so if you go and execute it you will get the round five and we don't have the six of course
Okay, now moving on to the finer argument that we can pass to this function.
It is the step.
It is as well optional.
And we use it in order to define or specify the incrementation.
If you don't include it, it's going to be always the one.
So we always increment our sequence with one.
So let's say that I would like to go and generate all the even numbers between 0 and 10.
And now what we can do, we can specify the step 2.
So now the incremental is going to be 2.
Let's see what's going to happen.
So it starts from 0 and then the next generated number, it will not be 1 because the incrementation here is not 1, it is 2.
That's why the next number is going to be 2.
Then the next in the sequence is going to be 4.
Then 6 and 8.
And now, of course, we will not have the 10 because the 10 is our stop.
Now let's say that I would like to go and generate all the odd numbers between 1 and 10.
So we can have the same setup, but this time we start counting from 1.
So now we start from 1, then the next one, 1 plus 2 is going to be 3, and then 3 plus 2 is going to be 5, 7, and then 9.
And with that, we are generating odd numbers.
So it looks nice.
So let's try that out.
I'm going to go and add the stop 10 over here and execute.
You see, we have all the numbers between 1 and 10.
Then let's go and add the step 2 and execute.
You will have only the odd numbers.
So as you can see, the iteration is less than before because we are incrementing with 2.
Now if you go and say, let's for example, four and execute, you will get only three numbers.
So the step is bigger in the sequence.
So far we are generating only odd numbers.
Now let's say that I would like to start from two and end at 10 and the step should be two.
So if you go and execute it, you will get now even numbers.
So it is very simple with those three arguments, we can define exactly how the number is gonna be generated.
So this is how the range function works.
so those are the famous sequences that we usually use but you can as well use any other sequences like for example the dictionary or maybe files so any object in python that is iteratable you could use it in the for loop
okay so with that we have learned how to build a loop how to build iterations inside our code and now of course always the question is why do we need those stuff how to use the for loop in real projects so now in the data world usually we have like files or tables that are in one place and we would like to go and move all those tables from one place to another place
Now imagine that you have to write a code for each table.
So if you have like hundred tables, you're going to write like the same code hundred times.
Now, instead of that, we can go and build a loop in order to go through all the tables one by one and start loading it to the target
place so we write the code once and we iterate through all tables and of course it could be files as well you might have like hundred files and you would like to move them from A to B and for that we usually build a loop now if you are working with data we might go and let's say clean up those files those tables and if it is always the same action that we would like to use what we can do we can use as well a loop so we go again and iterate through all tables
and do the same data cleansing so with that we are preparing the data before maybe analyzing it now of course not only tables and files we can go and iterate through columns so let's say that we have in our table multiple columns and they are bad quality in the values of the columns like for example we have leading or trailing spaces so of course in python we could use the strip function in order to clean up the data
So now instead of applying this function for each column inside our code, you might have really big file or table with hundreds of columns.
Instead, we make a loop for all columns and we iterate through all the columns of our table where we apply the strip function.
So with that, instead of having hundreds of lines, you have only two lines of code, one line for the for loop and the second one for the function.
So as you can see, this can be like foundations for you as you are working with data.
You use it in order to load data.
We use it in order to do data preparations, data cleansing and many other stuff.
So the four loops are really amazing.
OK, so now I'm going to show you a few
use cases where I use for loop so we usually use it in order to do aggregations like summarizations finding the average so let's say that we have here scores a list of scores and here we have few values like 80 50 60 and 75 now I would like to go and find the total of those scores so we can go and create new variables in order to store the total score
And now in order to do the summarizations of all those values, we can go and use a loop.
So we can say for, and then I need a variable.
So it's going to be score in the sequence scores.
And of course, don't forget the double points.
And now what we're going to do, we're going to say total is equal.
equal to the current total plus the scores.
So we are accumulating the values we are getting from the loop and we are storing it to the total.
And of course, as we learned in the operations, we have some shortcuts in order to do this.
We could add a plus before the equal and then just remove this.
So I am adding the scores to the total.
And now we can go and print a few stuff.
Like, for example, we can say the current total and then we say the total.
And outside of the loop, I would like to see the final total.
So final total.
And as well here, the variable total.
So let's go and try that out.
We got an issue because this is wrong.
You cannot use the scores, the sequence.
You have to use the variables.
So the score, not the scores.
We use the scores only for the iteration.
So let's try again.
And now, as you can see, it is working.
So first we have the current total 80, then plus 50, 130, 190, and the final total is going to be 265.
So as you can see, we can use it in order to do some aggregations.
Okay, to the next one.
As we learned, we can do data cleanup, data preparations using for loops.
So let's say that we have a list of files like this.
So the first one can be reports.csv and the next can be data.csv and the third one final.txt.
And let's go and add some evil white spaces on left or right in order to have some bad data quality.
So now here we have two issues.
We have the leading and trailing spaces.
And as well, you can see we have sometimes lowercase and uppercase.
Now I am a data engineer or analyst and I would like to go and clean up the data before I do something about it.
So here I have to do two things.
First, removing the spaces.
Second, making everything either lowercase or uppercase.
So depends on the rule that I follow in my projects, I have to change the cases.
So in order to do that, we could go and create a for loop.
gonna say for file in files double points now what are the action we're gonna say file is equal to file dots and we have now to start clean up the data so we can start with the strip function to remove the white spaces and now in order to test it we're gonna go and print the results so we're gonna say processing and then the variable name file
So now let's go and test this out.
So now by looking to the output, you can see we don't have anymore any leading or trailing spaces.
So everything looks fine.
And with that, we have cleaned up the names of our files.
Now we are not done yet.
We still have this lower and upper cases.
Now we can go and add the second transformation.
I would like to say I'm going to go and make everything lowercase.
If you check the output, it looks way nicer.
Everything is lowercase and everything is following our rule.
And another thing, let's say that I would like to go and get rid of the TXT.
Actually, we only allow CSVs.
So in order to do that, we add another transformation and we say replace
now the old value gonna be .txt and the new value our standard is csv now it is very important you don't write it capitalized because we are doing this the replace after we made everything lower so it's always like this first clean up the data and then do data transformations where you adjust and manipulate the data the order is very important first clean up and then manipulate so now if you go and execute it with that the final is as well .csv
So this is really amazing.
We write our data preparations only once and then we build a for loop in order to iterate through all our data.
And this is exactly the power of the for loops.
And as a data engineer, analyst, scientist, you have to do a lot of data preparations in your pipelines and you will end up using those for loops many times.
So make sure to understand those concepts.
They're going to be very important for you.
And my friends, it is challenge time.
So you have to go and print 7 times table from 1 to 10, but you must use a loop and it must look exactly like this output.
Don't miss one character.
Okay, so if you are done with this, I have for you a second challenge.
You have to print the following left aligned star pyramids.
So the first line is one star, the second is two, three, and the last one should include six stars.
And of course, my friend, you must use for loop for this.
So go and solve that.
Okay friends, now so far we have been looming through items using a simple way, very simple design.
But now we're gonna level it up and we're gonna build and design an advanced loop.
In Python, there are like tools or add-ons that we can put in our loop in order to have more control, like when to stop, when to skip.
And here I'm talking about the special loop statements.
We have break, continue, pass.
So let's start with the first one, the break statements.
The break statement is used in order to immediately stop the loop.
No matter where you are currently in the loop, you will exit, you will stop everything and go out.
And once Python sees it, the loop ends right there and go to the next line of the code.
So let's understand how this looks like.
So now by looking back to our simple design of the for loop, you can see that Python can execute all the items inside our sequence.
without stopping at all from the start until the end.
There will be no break.
There will be nothing unless, of course, your PC crashes.
So everything will be executed.
But now sometimes we would like to have a check if a certain condition is met.
We have to do something.
So that means we have to build a condition using the F statement.
and we have of course to do it inside the for loop so let's say that if the item is equal equal to two something should happen so now by looking back to our flowcharts that means we are building a second condition inside our loop so again the first condition comes from the for loop
it is something built in and we didn't specify it.
But the second condition comes from the F statements that we defined inside the for loop.
And of course, the answer could be false or true.
So now we have to tell Python what can happen if the condition is fulfilled.
Of course, you could like do something like print or call a function.
But if you want to affect the iterations of the loop, we could use one of the three
statements.
So we could use break, continue or pass.
So now, since we are talking about the break, if you go and use it, what can happen?
Python gonna put a break if the condition is true.
And if this happens, everything gonna stop and Python gonna go immediately to the end.
But if the condition is not fulfilled, everything is like before.
Python has to go and print the item.
Now let's see how Python is going to execute it behind the scenes.
So it's going to start from the beginning and ask for the next item.
So that means we are not at the end of the loop and Python is going to continue.
Now Python is going to ask the second question.
Is i equal to two?
Well, it's not because one is not equal to two.
That's why it's going to go to false and Python is going to print one in the output.
Then jump back to the top and again ask the same question.
Are we done?
Well, the iterator is going to answer with a value.
We are not done yet.
So that's why it stays inside the loop and go and ask the second question.
Is I equal equal to two?
Well, this time, yes, this is true.
Two is equal to two.
And that's why Python can go to the break statements.
Now, what's going to happen once Python sees break?
It's going to destroy everything and stop everything.
So there will be no more iterators and Python jump immediately to the end and it can ignore everything else.
So with that, we didn't iterate through the whole sequence.
So the iterator is not done and we have only one successful round, one iteration.
But because we have fulfilled the condition and the break statements, everything stopped.
And in the output, you will get only one.
So as you look to this, it's like we are building an emergency exit right from the loop.
If something catastrophic or emergency happens, we have to stop everything that we are doing and get out.
This is exactly how the break statement works in Python.
Okay, so now let's have an example where we have list of names.
So we could have something like John and Maria.
And now we're going to have something empty.
So we don't have any value inside it.
And then let's say Kumar.
Now you can see we have four values, but one of them has an issue.
We have here empty.
Now let's just make a normal loop in order to print the names.
So we're going to say for name in empty.
names and now let's go and print so we're gonna say f name equal to our variable so name let's go and execute it now as you can see we have all the four values and the empty one even now let's say that if there is something empty in my data
i don't want to continue i want to break the loop and to stop the program so now in order to do that we have to make a check so first we're gonna say if name is equal equal to empty so with that we are checking whether then it doesn't have any value double point and now we hit enter and print a message now as you can see we are now at the second level of the intonation print is inside if and if is inside for
you have to be careful with that so we can say empty value detected and of course this is not enough we want to stop everything so we can write a break and now very important to understand the break must be inside an if otherwise you will break from the first iteration so now let's go and try this out so now in the output you can see we processed two values john maria and then once we
processed the empty this condition was true and we went inside it so we printed the message empty value detected and then the break is executed so that means we stopped over here and we didn't process those two values so they didn't have the chance to be printed over here so this is exactly what's going to happen if you use a break inside your full loop
Now let's go to the next one, the continue statements.
We use it in order to skip one loop cycle.
So that means once Python sees the continue, it's going to stop right there.
But instead of ending the whole thing, the whole loop, what's going to happen, it's just going to jump back to the top and start the next round.
So that means it is not that catastrophic like the break where it's going to stop everything.
We are just skipping the current round and go to the next one.
So let's have a simple example to understand it.
okay so now in our code after the condition this time instead of using break we will use continue so now what can happen behind the scenes at that if the condition is true of the f statements we will have continue instead of break but here comes the important points after continue we will not go to the end we will just go jump back to the top
So we're going to have an arrow from continue back to the start of the loop.
Of course, with that, we are not saying we are going to the beginning of the iterator.
So we are not resetting anything.
It is just simply we are skipping the current round.
So let's try this out and see what can happen if you do that.
Now, Python starts from the top and ask the iterator for the next item.
It's going to be the first one.
So the value number one and then go and ask the second condition is one equal to two.
Well, it is false.
That's why I'm going to go to the right side.
and execute the loop as normal so in the output you will get one and then jump back to the top to repeat and ask the same question what is the next item the iterator answer with two and then to the second question is two equal to two this time yes we have fulfilled the condition
and python goes to the left side and execute continue continue means skip the current one and just go to the top and go to the next round so with that we didn't do the normal iteration so python will not execute the print and you will not see the value two in the outputs so we skipped the two because it is fulfilling the condition and python has to start again from the top and ask for the next item it's gonna be three and then ask the second question is three equal to two well it is false and then go to the right side to the normal loop
It's going to print the value 3 and then go to the top and ask for the next item.
Well, we are at the end.
The iterator asks Python to stop.
So with that, the answer is going to be true and jump to the end of the loop.
So as you can see now in the output, we have 1 and 3, but we skipped one round.
So it's not like the break where we stopped the whole iteration and we stopped everything and went to the end.
We just skipped one single iteration because it was fulfilling the condition.
This is exactly what continue means.
Okay, so now back to our example where we have the four names and one of them is empty.
It was too much, you know, that once we found it, the empty over here, we stopped the whole loop because I would like to process as well Kumar over here.
So what you can do instead of having a break, we can say, you know what?
Just continue, not continue.
And with that, we will skip only this one bad value, but we will continue the loop.
So let's try this out and execute.
As you can see in the output we have now name is equal to Kumar.
So we processed this value and we still see our message empty value detected.
And this comes from here.
So that means, yes, we found the bad data, but we skipped it.
We didn't process it inside the branch.
And with that, we guarantee we are processing only the good data.
And in this scenario, I'm going to say continue here is more suitable than break because if I'm using here break, it is like an overreacting.
I would like to process all other values.
It is only bad value inside my data, and I would like to process all others.
And since this is naturally critical, I could go with the continue instead of break.
So that's all for the continue statement.
Now let's go to the third one.
We have the pass statement.
Pass is just like placeholder where nothing happens.
So this sounds a little bit strange, but all what we are saying here that I will put here code later, but for now, just keep going.
Don't bother.
The pass statement means for now.
do nothing so let's understand what exactly this means all right now let's go and put pass inside the f statements so that means instead of continue we can have pass and this can be executed if the condition is true now of course what is very important is where the arrow gonna go after the pass well this time it will not go to the end it will not go to the start of the loop
Well, actually, it's going to go to our block of code.
So it's going to go back to the normal way of our loop.
Now, if you look to this, do you see something strange?
Well, it doesn't matter if the answer is false and true.
Both of them are leading back to our block of code, right?
So that means we are not stopping anything like the break.
We are not skipping anything like the continue.
Everything is going to stay as it is.
So the big question is why we need it?
Well, we don't use pass very often in our coding, but we use it as a placeholder as we are planning the next steps.
So for now in my code, I am sure that I have to check for this situation where I is equal to two, but I still don't know what I'm going to do if the condition is true.
So it's like I'm planning something inside my code.
I don't want to forget about it.
And I'm building something temporary.
And the final solution should not, of course, include pass.
Now, let's have a quick example to see how this can work for the first value one.
Well, is one equal to two?
The answer is false.
Python going to go and execute the print normally and we will see one in the output and then go back to the top and ask for the next item.
You will get two.
Well, is two is equal to two.
This time we will go to the left side and execute the bus and nothing going to happen.
Python has to go back to the prints and print in the output too.
So you see, we are not skipping.
We are not ending anything.
then go back to the top and ask for the third item you will get three in the output like usual and the next one the iterator will give you no value and ask for stop that's why button gonna end and go outside so as you can see it's like you are iterating without having the condition you iterated through all the values one two three so again pass means just keep doing what you are doing keep doing the iterations and maybe later we're gonna add something in this place so it is like placeholder
So now back to our example where we have the four names and one of them is empty.
The thing is, this is, yes, an empty value, but I don't know yet what to do with it.
I could go and replace it with dummy values like unavailable, unknown, maybe some flags.
But still, I don't know what to do with this.
And in the other hand, I don't want to skip it.
I don't want now to break the whole program or skip this step.
I would like to decide about this later.
So what we can do, we can go over here and say, you know what?
We're going to make a pass and then I'm going to go and write a note for myself so that I don't forget about it.
So I'm going to say to do handle.
empty value and to be honest i don't want to print anything in the output because we don't want to see this yet so it is just something for me that i would like to plan for the future but at least i know about this scenario i know there is like an issue like this and i have already like a condition for it to check it but i don't know yet the action so now
If you go and execute it, you can see we're going to go and process all the names that we have in our list.
So we are not skipping anything.
Maybe after a week I meet my team and then we discuss about this issue.
I told them we have like some empty values in our data and I would like to know how to handle it.
And then we agreed all to have dummy value like unknown.
So now I come back to my code and say, you know what?
Let's go and do it like this.
Name is equal to name and replace.
And instead of empty, we're going to have unknown.
so it is like this and i can go and remove now the pass we don't need it i know the action and execute it you can see now in the output i'm getting the unknown so we didn't skip we didn't stop anything we just prepare it for the next steps and this is exactly how we use this statement pass in our code
so now let's have real examples on how to use continue and break so the first task it says loop through a list of days and print only the working days skipping the weekends so now let's go and create a list of days so for example let's have a monday
sunday wednesday and let's have it tuesday just randomly some days so now we want to print in the output only the working days so we don't want to see any weekends and for that we can go and create a for loop so we're gonna say for day and days and then double points and now we're gonna go and print the work day double points and then our variable
Okay, that's it.
Let's go and execute.
Now in the output, you can see we are printing everything, even the weekends, which is not really what we want.
So we have to check whether the day over here is a weekend.
Now for that, we have to go and create an if statement.
And we can say if day in.
And then we're going to have our two values, Saturday and Sunday.
Like this.
Now, if this is the scenario, what can happen?
Should we break or should we continue?
Well, as the task says, we have to skip the weekend.
We don't have to stop everything, yeah?
So it's not that catastrophic.
That's why we're going to go with the continue.
We want to skip those days.
Otherwise, go and print the day.
So let's try this out and execute.
You can see we have Monday, Wednesday, and Tuesday.
But the Sunday is not in the output.
And that's because it is fulfilling our condition here.
So as you can see, it's very simple why I decided for the continue, because I just want to skip.
I don't want to stop.
So now one thing quickly about the styling, as you can see, again, I'm always using the same name.
So they in days and one more thing, try to not put a list in conditions or maybe in the four loops.
So as you can see, for the days, I always define a variable outside.
And for this as well here, I'm going to go and take it out.
and make a variable for it so we're gonna say weekends and we're gonna put it outside and then over here we're gonna say weekends so now as you can see it is now nicer and shorter and easy to understand for day in days if day in weekends so it is easy to understand and you can hear extend it outside easily than extending stuff inside the for or the condition then things gonna be easy to write and easy to read
So with that we have solved that task.
Okay so now let's have the following task and it says scan through a list of email addresses and prevent unsafe data from entering your system.
So now as usual we're going to go and create some simple data.
So emails like for example data at gmail.com and another one para ad outlook.
dot d and then we're gonna have something strange like drop table users and semicolon and then we're gonna have something like maria at gmail.com so now let's say that i would like to process my data and i'm gonna say for email in emails and we're gonna print processing email and then double points and our variable
Like this.
So now let's go and execute it.
So now in the output, you can see we have processed all the emails, but actually we are not doing the job.
The task is to check whether everything is safe.
And my friend, this is not safe at all.
We have here an SQL injection.
SQL injections it's one of the most famous hacker attacks on your applications that could happen if you have any field where the user is entering data so maybe in the registration form or in the sign in you have a field to enter the emails and with that the hacker is going to use it in order to add an SQL injection
to destroy your database or maybe to read important informations and actually it is one of the easiest way in order to hack your system that's why most of the systems must protect their data and their applications from those SQL injections now we can go and check whether the email includes a semicolon semicolon is indicator that there is an SQL statement there is a command
So we can go and check for that for, say, if we have a semicolon in email.
So what's going to happen?
We're going to print and we can say something like this.
SQL injection hacker attack.
So with that, we get some panic.
Let's go and execute it now.
now you can see in the output we are getting the message right but still we are processing it which is really bad we have to add here a control flow statement so again continue or break well i would say i will not trust the data at all that i'm getting from this source that's why i'm gonna stop everything and i'm gonna go and use a break in order to protect my system so with that as you can see we are processing only the first two emails and everything else
gonna be stopped.
You can see in this example, we have high critical risk in our system and we use break in this scenario.
Now let's have a quick recap and compare them side by side.
The first statement is the break where we add a condition.
And if this condition is fulfilled, we execute the break and we exit the loop.
So you are building like an emergency exit if something goes really wrong.
That means all other values will be totally ignored.
We have to exit immediately.
Oh my God.
Okay, it's happening.
Everybody stay calm.
What's the procedure everyone?
What's the procedure?
Stay calm.
We usually use this only for high critical risks, like, for example, security issues.
As we learned about the SQL injections, if you encounter this, then you have to stop immediately.
Otherwise, you might destroy your system.
Now moving on to the next one to the continue.
This one is not that dramatic.
If the condition is fulfilled, we're going to say just skip this iteration and go to the next one.
We will not stop the loop.
And of course, we use this for medium risks cases like empty data, bad draws, empty files and any other weird special cases that you might encounter.
And you say, OK, just keep it.
now finally to the chilliest one of the three we have the pass so for this one if the condition is fulfilled it's gonna say you know what just keep doing what you are doing just execute the codes and do your loop do your thing so it is like a placeholder and you say you know what i have planned for this but for now you know what just keep walking don't skip don't stop anything
so again break if you have something critical go out now then continue if you have a medium risk and you say skip this one and the pass is it just for the planning and you say do nothing
Hey friends, so now we're going to talk about how to use the else statement inside a for loop.
So we use the else statement in order to run a block of code only if the for loop finishes naturally, normally without hitting a break.
So all what we are seeing here, okay, I went through the whole loop without any interruptions and at the end I'm going to do something extra.
So let's understand what this exactly means.
now as you remember we used already an else statement but together with the if statement as we are building the conditional logic so we use the if statement together with the condition and if it is true then we print for example okay and then after that we said okay if it is not true we could use the else statement in order to define what can happen if the condition is false
and for example we say print not okay so with that we use else in order to define what can happen if that condition is not true and then based on the execution either we go to the right side or to the left side but now if you look to the first statements we said okay we have a sequence and we're gonna repeat the same code multiple times until we don't have any value and once the loop completed and we went through all the items then we go and exit to the end
Now, if you look at this, you can see after the loop ended successfully, we don't have anything to do.
It will just go and exit.
But now, instead of that, what we can do, we can add an else statement.
So we put it outside the for loop.
It's like you are doing if else and then you define what can happen, like, for example, print int.
And with that, we have defined an action that's going to be executed once the loop is completed.
So as you can see, it is not exactly like the FL statements.
The FLs, we went either to the right or to the left.
With the four Ls, we're going to do everything.
We're going to iterate through the whole loop.
And as well, at the end, we will be executing the Ls as we are exiting to the end.
So now let's go deeper in order to understand how this exactly works.
So again, we have this example where we have the three values and now we add an else statement where we print an end.
So this block of code can be executed if we reach the last item.
So now if you go and execute it, the usual is going to happen.
So Python can ask, okay, what is the next item?
We will get one and then we're going to go and print it.
Then ask again to the next item.
We will get two, then print it again.
and then to the last iteration and then it's going to go and ask again for the next item we will get three and print it in the output as you can see so far we didn't execute yet the else statement because we are looping now gonna go back to the top and ask again what is the next item iterator gonna say stop because we don't have anything left and now the condition is true that means
python gonna exit the loop but before going to the end we still have an else statement so python has to execute now the else and we will get in the output an end and after that it can go to the end and stop everything so as you can see python executed the else statement at the end after it went through the whole loop and iterated through all the values in the sequence so this is exactly how the else statement works with the for loop
and by the way my friends if you are enjoying this type of free tutorials where i'm sketching the concepts behind the scenes and showing the codes and you would like to see more like this then support the channel by subscribing liking commenting this really helps a lot so now let's go back okay so now let's have a very simple example we're gonna go and create a classical for loop so let's go and create few items like numbers one three four and seven and then a very simple loop so for i in items and
and in the output we will print the i so let's go and execute it as you can see we have printed all the values in our sequence so nothing fancy so far but now what we can do we can go and add an else statement so now let's go to a new line and be careful you have to place the else at the same level as the four not inside the for loop so we're gonna say over here else double points and let's print something like loop is completed now let's go and execute it
now as you can see at the end of our iteration we have this nice message saying loop is completed so this is actually how you add an else statement to your loop but here we have an issue our code now makes no sense at all because what we are doing here we are looping through the items and at the end we are printing a message well actually the else here is totally useless because what i can do
Actually, I can go and remove it and it's just simply like an instruction after the for loop.
So after Python is done with the loop, just go and print the loop is completed.
So if you go and execute it, you will get exact same results.
So actually having an else like this is totally pointless and has no sense.
And now, of course, the question is, if this is useless, then why we have an else statement in the for loop?
Well, my friends, there is only one scenario where the else statement makes sense if you combine it with the break statements.
You have to combine the break and the else in order to have a real usage for the else statements.
So let's understand what this means.
okay so now again back to our example as we have a normal for loop with a break now if you go and add an l statement at the same level with the for loop and we say for example print ends so with that we are adding a block of code if we reached the last item in the loop so if this is true then print the end and then exit so now let's see how python can execute it as usual python can
ask for the first item from the iterator we will get one then it's gonna check the condition is one equal to two well it is false that's why it's gonna go to the print and print one in the output then it's gonna go back to the top and ask again give me the next item it's gonna be two is two equal to two well this time my friend's gonna be true and python gonna go to the break statements
and exit the loop exit everything and with that my friend you will get in the output only one value the one so python didn't execute the next value in the iterator the three and very important python didn't execute the else statements and that's because my friend the condition last item was never fulfilled we didn't get through that's why python didn't has the chance
to go and execute the else statements and this is exactly why we need the else statements we use it as an indicator to understand whether we iterated through all the items or we encountered an interruption so as you can see the break and the else statements they work together in order to define this logic so actually it looks like this now we have a for loop
And we want to repeat something over and over until the loop is finished.
And once the loop is completely successful, we do something else using the else statements and then go straight to the end.
So this is the normal flow.
If everything goes OK, otherwise we can go and build an F statement that we can check in each iteration.
And as long as this condition is not fulfilled, we stay in the loop.
But if this condition is fulfilled, then we make a break in order to exit the loop and then go straight to the end.
So now as I am drawing this, the break statements really work hand on hand with the else statements.
And they look like exactly the FL statements.
So either the loop is completed and we execute the else statements or the loop is actually broken.
and we are exiting using the break so now really I understand now exactly why we are saying else because either it's broken or else it is completed and this is exactly my friends why I love doing visuals because I understand then the logic how things works and this is exactly what I do usually I draw the logic and then start coding the solution
So now back to our example where the L statement is totally useless.
Now we can make a usage of it.
I would like to go now and build a check to understand whether in my list we have an even number.
And if we don't have an even number, then we have to print in the output that all the numbers are odd.
So we have to go and make a very simple if statement.
And we're going to say if the reminder when we divide the item by two is equal equal to zero, then actually we found an even number.
So we're going to go and print something like this instead of the I.
We're going to say even number found.
And then we're going to go and print this item.
Now, after that, we're going to go and break the for loop.
And if we didn't find any even numbers, that means all the numbers are odd.
So we're going to say all numbers
are odd so as you can see it sounds like an fl statement now let's try this out and execute you can see even number found and we have the four let me just add here a double point so what happens here that's python did check the first number one well it is not fulfilling the condition and nothing happened same thing for three
but for the for we have a true that's why python did execute this block of code so it printed the message and then executed their break that means everything stops and in this scenario python will not execute the l statements again the l statement is going to be executed if python did manage to iterate through all the items completely without hitting the break so that's why in the output you don't find this message over here
Now let's go and test something else where we can have all numbers are odd like this.
So we don't have an even number and then execute.
You can see in the output we get all numbers are odd.
So that means Python did try all those numbers and none of them fulfilled this condition over here.
So it didn't manage to execute the break.
That's why the loop was completed and Python didn't execute the else statements.
So now, as you can see, the else statement here is totally useful.
We use it in order to understand that Python did iterate through all the items and Python didn't execute the break and we didn't have any interruptions.
And as you can see, it only works if you combine it with the break statements.
And now we come to the question that why do we need those stuff and when we can use it in real projects?
Well, the main use case is that we use it in order to search and validate our data.
As we are getting new data, we have to search for bad data and report at the end whether everything is clean or whether we found some bad data and bad quality.
So let me show you now some real use cases.
Okay, so now let's have an example.
Let's say that I have a list of names and I would like to check whether we have any missing values.
So let's have a very simple list of names, like for example, Camara and then Tuba.
And the third one is actually missing.
We have a none.
And then the last name gonna be Monica.
okay so now i would like to go and check whether we have any missing values for that we have to go and iterate through the list so we have to go and create a for loop for name in names double points now we're gonna go and build a condition in order to check whether the name is empty and for that we're gonna use an if statement so if the name is none then what we're gonna do we're gonna go and print a message we're gonna say found a missing name
And after that, we don't want to continue.
We have already found an issue in the data and that's enough for now.
So I want to stop and exit the loop.
Now, let's say that we have checked all the names and we didn't fulfill this condition, so we didn't find any missing values.
Now, I would like to print a message saying that everything is OK. And for that, my friends, we use an else statement.
We're going to say all names are available.
okay let's try this out and execute and now in the output you can see you found a missing name that means it is working because we have here the third value as a none and this made our condition fulfilled and activated the print found missing a name and as well the break
now let's try another thing like for example we go and add a name here like mariam and then let's go and execute you can see in the output we are getting all names are available because all names are there and this condition is never fulfilled and since we iterated through all the values of our list
successfully we got at the end this print message from the else statements so as you can see we are using the break together with the else statement in order to search for something and validate our data and this is something that is very common as you are doing data quality checks and quality assurance
Okay, so now let's have another example where we use this.
Let's say that I would like to check whether all my files are CSV.
So that means we have to validate the type of the file.
And this is something that happens a lot as you process files.
Okay, so now as usual, we start with an example.
Let's have few data.
Like for example, we have a file name called data1.csv.
Another one, we have report.pdf.
And the last one, let's say that report to dot CSV.
So now, as you can see, not all my files are CSV.
We have one of them as PDF, which is not really good.
Now we have to check that.
So we're going to go and create a for loop for file in files.
So now we're going to go and create our checkpoints and we are checking the files.
What we are searching for is actually the last characters of each string.
So that means we're going to go and use the method ends with.
and we have to pass for it our expectation so it's gonna be dot csv but actually we are searching for bad data so that means we have to search whether the file does not end with csv so we have to add the not operator so what happens if your file is not csv first we're gonna print a nice message and we're gonna say for example let's print the file name so the file is not a csv
after that we want to break so we found something that is not correct and we end the loop now otherwise if everything is fine we have to just print successful message so else print all files are csv so now let's try this out as you can see we are getting a report.pdf is not a csv and that's because it is fulfilling this condition and this block of code is executed
now let's try the other one so if you remove the pdf to csv and execute you can see all the files are csv so as you can see again we use the break together with the else in order to check the quality of our data and now there is like one thing that i would like to show you
if we say okay this is a pdf and we have another one let's say it is data2.txt so we have two cases where it is bad now if you go and execute it as you can see we are getting in the output only one so report.pdf
is not a csv so once we cached the first issue the python gonna break the loop so actually we are not getting the second file over here and that's because the loop stops but now if you say you know what we can get as well the second one if you remove the break and you say you know what let's go and use the continue well let's see what gonna happen if you do that and now if you are looking to the output now we are getting the two files report.bdf and data2.txt
but the issue we are getting this as well all files are csv which is not correct so actually this makes no sense and that's because you cannot combine the continue with the else it's gonna work only if you use a break so the continue what can happen it will just skip one iteration it will not stop
the whole for loop so actually you cannot do that it makes for me no sense you have to go and use a break over here and with that you will get only one file if you are not happy about it you can just go and say here not all files are csv and then execute it so that we are not reporting as well where we have the issue so this is how we work with the break and the l statements with the for loops
And now my friends, as usual, I will not leave you without a challenge.
And we have a tricky one.
So the challenge says, check the following list of file names and find out whether we have duplicates or not.
So if you found duplicates, you have to tell us about it.
Otherwise, if everything is fine, just say all files are unique.
Now I'm really interested to see how you're going to solve it.
So let's go.
All right, friends, so now we can talk about something called nested for loop and nested for loop.
It is very simple.
We have a loop inside another loop.
So that means we have a big loop.
We call it the outer loop.
And for each run of this big loop, we can run completely smaller loop.
and we call this one an inner loop and once this small loop is completed we go to the next iteration of the outer loop and the same thing gonna happen we gonna run again completely this small inner loop and again once the smaller one is completed we run one more iteration for the bigger loop and of course we can keep nesting loops inside another loop so we could have like very small loop
inside the middle one so this is exactly what we mean with the loop inside another loop so now let's see how we're gonna build it now we have this classical for loop nothing fancy we are just iterating through a sequence now in order to make a nested loop we define a new for loop but inside the first one so for example for y in a sequence like one two
and let's call the first one an x so it sounds better and with that we are iterating through two loop variables the x and the y and we are printing both of them the x and the y now about the chart python has like two loops so they are connected like this but we are not finished yet once we reach the end of the second loop it will go back to the start of the first loop
And of course, once everything is completed, we will go to the end.
So as you can see, the start and the end of the second for loop is actually completely connected to the first loop.
And again, we call the whole block as the outer loop and the block of code that is inside it.
We call it an inner loop.
So the second loop.
OK, so now once you execute it, Python has to go and create two iterators, one for the outer loop and another one for the inner loop.
so now python start with the outer loop and ask for the next item and iterator gonna answer with one since we are getting a value we are not at the end that's why python now has to go and start the inner loop so here python gonna ask for the next item in the second iterator in the inner loop and here as well we will get one so since we are
getting a value we are not at the ends and now python has to go and finally execute the block of code print so now in the print the x is assigned to one the y is assigned to one as well that's why in the output you will see one one and now python will go and start from the top of the inner loop and it's gonna ask for the second item of the inner iterator
we will get two and since we are getting value we are not done yet with the inner loop so we're gonna go and print so the x is equal to one but now y is equal to two that's why you will see in the output one two so now we're gonna go to the top of the inner loop and ask give me the next item now the inner
iterator gonna answer with stop because we are at the end so with that python gonna go and exit the inner loop and go to the end but of course we are not at the end actually we will go to the top of the first loop so everything that we have done so far that was only for the first item of the outer loop
Now Python is going to go and ask the outer iterator for the next item.
It's going to be 2.
And guess what?
2 is now the last item of the outer loop.
That's why it's going to go and start again completely the inner loop.
So the inner iterator is going to answer with 1.
And you will have in the print now x is equal to 2 because the outer loop has the value 2.
And the y is 1.
So you will have 2 and 1.
and then the next value to two and the inner loop can be done then exit again the inner loop but you have to go again to the top of the first loop and the same thing happened you will go to the third value do the whole inner iteration
And then go back to the top of the loop.
So as you can see, this is the third time we are executing the inner loop.
And now Python is going to ask for the outer loop.
Give me the next item.
The iterator is going to say stop.
I don't have anything left.
And finally, you will exit the outer loop and go to the end.
And now we are at the end of the outer loop and everything stops.
So this is exactly how Python executes the nested loops.
And by the way, my friends, if you are enjoying this type of free tutorials where I'm sketching the concepts behind the scenes and showing the codes and you would like to see more like this, then support the channel by subscribing, liking, commenting.
This really helps a lot.
So now let's go back.
okay so let's have a very simple example we're gonna start with the outer loop for x in range let's generate like for example three numbers this is our outer loop our big loop now inside it we can add another loop so for y in range and this time for the smaller one let's go with two iterations so two numbers this is our inner loop and now we're gonna go inside the inner loop and print
So I'm going to print, for example, between two parentheses, we're going to have the first variable, the X, and then separate it with a comma and then the second variable.
So something like this.
Let's go and execute it.
Now, as you can see, for each value from the outer loop, we have two rows.
So 0, 0, 0, 1, then 1, 0, 1, 1.
And for the last one, 2, 0, 2, 1.
So that's it.
As you can see, very simple.
This is how you can nest.
two loops inside each others.
And of course you can go and add a third loop, like for example, for Z in range, let's add as well two.
So now we have to be careful.
This print should be inside the third for loop.
And here we're going to go and add our third variable, the Z.
So now let's go and execute it.
Now, as you can see, we are getting like a matrix of numbers.
So as you can see, we have a loop inside the loop, inside the loop.
This is pretty easy.
Okay, so now we come to the real talk.
Why do we need those stuff?
When we can use nested loops in our data projects?
And for that, we have actually two major use cases.
The first use case is for crossing and combining our data, or we call it pairing the data.
so it's all about we have two different lists and i would like to see all possible combinations of those two lists so that means i'm gonna go and combine and pair each value from one list with the other values of the other list okay so now let's have an example where we have two completely different lists the first one gonna be the colors so we have reds we have blue and green
And another list, it is about the sizes.
So we have stuff like maybe L, M and S. Now let's say that I would like to go and generate maybe a product catalog with all combinations of colors and sizes.
So I want to pair each color with each size.
Now, in order to do that, we can use the nested loops.
And here it doesn't matter where you start.
Like, for example, let's start with the colors.
So for color in colors.
Next one going to be about the size as an inner loop.
So for size in sizes.
Now, all what you have to do is to go and just print those two variables.
So print where we're going to have something maybe like this.
Maybe the first variable is going to be the color.
And then after that, I'm going to say size.
And we have the variable here, size.
So now let's go and execute it.
As you can see, for each color, I'm getting all the sizes.
So for the red, the blue and the green.
So this is what we call sometimes Cartesian or cross or combining the data where you pair all values with all values.
So this is a nice use case for the nested loops.
OK, so now moving on to the second use case and the most important one.
We use it in order to go through layers or we call it drilling into hierarchy.
Let's have this sketch to understand this Okay, the first example that we have hierarchy in the dates like for example, we start with the years 2026 and 2027 now for this we could make a for loop in order to iterate through all the years and
And this we consider as the first level.
And now we could drill down into more details if we go to the months.
So that means we add another loop inside the year, four months in months.
So we are at the second level.
Now we can go deeper where we go to the days.
We are now at the lowest level, at the level three, where we add a third loop where we say four day in days.
And after that, we have to go and do something about all those three informations.
So as you can see, we are using nested for loop in order to go through different layers in order to drill down into hierarchy.
Let's have an example.
Now let's have an example.
Let's say that I have to go and create multiple reports for each year, for each month and as well for each day.
And now instead of doing it manually, we can go and create for loops.
So as usual, let's go and create our data.
We're going to start with the years.
So let's say 2026 and 2027.
And the next level is going to be the month, where let's say that we have only two, for example, January and February.
And the last level, the days, I will go and generate it using the range.
So it starts with one and ends with 29.
So now I have all my data and all I have to do is now just to go and create the four loops.
so the first one gonna be for the years for y in years the next one for m in months and the last one for the days for d and days now we have our nested loops the three loops and all that you have to do for example let's go and just print the file name so it's gonna be f
and the naming convention start with report underscore and then the year so i'm gonna go and get the variable of the year then the month so that m and the last one gonna be the day d and as i said we are creating csv files so actually that's it let's go and try this out and execute so now in the output we're gonna get a huge list because we have here a lot of days
So we are going to have all those file names that combines the year, month and days.
And of course, we're going to learn later how to create files.
But for now, we are just printing it.
So look at this with just few lines of codes, you can generate massive amounts of data.
And as well, you're going to save a lot of manual work.
This is exactly why we are learning about the loops.
And another example for this use case, and this one is very common as you are working with data.
we navigate through tables and columns so if you know some database concepts you know that we store our data inside tables columns and rows so the table gonna be the highest in the hierarchy and if you drill down to the next level you go to the columns so each table contains multiple columns and now if you want to go deeper you go to the rows to the actual data and in order to navigate through this hierarchy
we could use nested loops so for the first level we could have like four table in tables then we drill down to the second level for column in columns and then to the last level where we say four row and rows and at the end as usual we have to do something like maybe cleaning up the data
doing different preparations and manipulations to the data depends on what you are doing.
And this setup you see now, those three different nested for loops, it's something that you're gonna do in each data projects.
So if I open now my projects, I'm always gonna see those combinations where I'm iterating through the tables and then the columns and then the rows, and then doing something.
So I'm going to show you an example that I just used in my project last week.
And I was running an SQL query on multiple tables and columns just to check whether we have nulls in the tables.
And now, of course, we will not talk about the SQL query itself.
It's all about that.
I had a huge list of tables and columns.
And if I'm going to go and write those queries manually, it's going to take really a lot of time.
Now, instead of that, I just wrote a few line of codes in Python and it solved the task.
So let me show you how I solved it.
It is very simple.
First, we're going to go and create a list of all tables that we are interested in.
So let's say we have the customers, we have orders and products.
So here we make a list of all tables that I would like to query.
And here, for example, the prices.
then next i need the columns that actually i am checking so we're gonna make another list for the columns and here i was checking only for the ids and the creation dates so those two columns are actually part of all my tables and then i made really nice two listed loops so i say it for t and tables
for C and columns.
And now what we're going to do, we're going to take the query over here and add for it the variables.
So I just made a print and then we start writing the query.
So select count star
from but now instead of saying customers i would like to get the variable from the list so instead of this one we're gonna have the dynamic table name it's gonna be the t and after that we have the static parts we have the where and then we have the column name and again i will not make it static i'm gonna go and get it actually from my list the last part is actually static so is null and then the semicolon so as you can see i have converted my query
into a dynamic query now let's go and execute it as you can see in the output i have automated everything i got all my queries and for each table we have two queries one for the id and another one for the create date and with that i don't have to write anything anymore manually i could just make few lines in python in order to automate the whole job
And if I got in the future more tables and columns, I'm just going to go and extend it over here and then automatically generate my SQL query.
And I know this sounds really simple, but this is what advanced data engineers actually do.
Especially if they are building systems like data warehouse, data lake, data lake house.
We will be dealing with a huge amount of tables and columns, and you cannot go and hard code everything.
So you have to load the tables from A to B.
You cannot go and script something for each table.
Instead, we build something called metadata driven pipelines.
We do exactly what we have done in this example, where we write the metadata, table names, the columns, and as well the data types and so on in variables and key values.
And then we make a script like this over here where we iterate through the metadata and we do something like maybe copying the tables, changing the schema, doing few preparations.
And with that, you will be saving as a data engineer or maybe as a data analyst.
If you are doing something huge in many tables, you save my friends a lot of time and I use it many times in my projects.
So I know we are talking about the nested for loops, but this is something very advanced and they're going to make your work highly automated.
And another example about hierarchy in real data projects is that if you are working with data lakes, like for example, if you are storing data in Azure, you might have like some containers and inside each container, there will be different folders.
And inside those folders, you're going to find different files.
And of course, my friends, we use nested for loops in order to navigate through this hierarchy so that we reach those files and then we load them maybe to somewhere else or we do some transformations.
This is something that you're going to do a lot if you are analyzing those files or you are doing some data engineering about it.
Of course, there are many other examples that you can encounter, but the rule is very simple.
If you have different layers, different levels, and you have like hierarchy, you will end up, my friend, using nested loop in order to navigate through the structure.
So those are some real use cases about the nested loops.
Alright friends, so with that you have learned how to build a nested for loop and with that we have covered everything about the for loops in Python.
Hey friends, so now we're gonna talk about the second type of looping in Python.
We're gonna talk about the while loop.
So what is a while loop?
We use it in order to repeat the same code over and over as long as a condition is true.
So this sounds like the for loop, right?
Now, what are the differences between for loop and while loop?
Here's the deal.
with the for loop we have understood that python gonna go and create internally an iterator and as well a condition that keep checking whether we are at the last item of the iterator and as long we have values from the iterator we keep looping through the for loop and once we don't have any more values we go to the end now there is like two issues with this setup the first one is that we are always pre-defining the sequence
so before we start iterating we know exactly that we have here three iterations so it starts with the one and ends with the three and we know we can iterate three times so we know exactly how many times we can iterate the second issue is that this condition in the middle is as well something predefined from python and we cannot change it so it's always about whether we are at the end of the sequence
and this is exactly why we have the second type of looping we have the while loop so this time we're gonna define our own condition for the iteration and if our condition is true then we're gonna go and do something and go back to the top so that means as long as our condition is true we will keep looping and iterating but the moment our condition is not anymore true it is false then we're gonna exit the loop
The big advantage is that we define our own loop.
So with that we have more freedom and flexibility.
So we don't know upfront how many times it will go.
It is all about our condition.
And this sounds of course risky because we might end up easily in an infinite loop.
So now by looking to those two side by side,
the difference is very simple with the for loop we are iterating over a sequence like a range a list so we are looping over something known but with the while loop we are iterating and looping over a condition that could change anytime
so now let's dig deeper into the while loop now for the while loop we have as well like the for loop different designs and types and i'm gonna put them into two categories the first one we call it the while condition so you define a condition and the loop can keep going
until the condition becomes false so this is the classical type but we have another design we call it the while true so this loop gonna run forever it is infinite that's why we add to it together with a break statement in order to force the loop to stop and of course there is a reason for that so those are the two main categories and we're gonna start with the first one the classical one the while condition
so now we're gonna start with a very famous use case where we build a counter using the while loop we start with the keyword while and then we define a condition like for example i should be smaller than four and once you do that python gonna go and create for you a condition and as usual for each condition we have either true or false now since this is a while loop false means we're gonna go to the end but if it is true we need to do something
something could be like for example print the i now if you do it like this after the action is done it's gonna go back to the top of the loop so it is like two lines similar to the for loop if you go and execute it you will get an error because python don't know what i is so we have always to assign it first to a value before the loop and of course we call this initialization
of the while loop now once you do that it's gonna put it of course before the loop starts because we are outside of the loop so now let's see how python gonna execute it first it's gonna go and execute i is equal to one so it's gonna create a variable and assign for it the value one and then enters the loop
So now it's going to go and check, OK, is I smaller than four?
Well, it is true.
That's why it's going to go and print the I in the output and then go back to the top of the loop and then ask again, is I smaller than four?
Well, nothing changed, my friends.
The I is still equal to one and it's going to fulfill the condition and then going to go print it again and go again to the top of the loop.
and gonna ask again is i still smaller than four well of course so as you can see it gonna keep looping we are stacked in infinite loop because we never changed the value of i and the condition is always true that's why it will never stop until it kills your pc
that's why we have to add a new line inside our while loop in order to give a chance for the i to change like for example we could update it by adding one to the value so that's the value get incremented by each iteration and we have lucky counter so now let's see how python gonna execute it first it's gonna start with the initialization so it's gonna assign one to the i and then it's gonna go and enter the loop it's gonna go and ask
is i smaller than four well it is true that's why it's gonna go to the print and print the current value is one now next python gonna go to the second step and add the value one to the variable so one plus one we will get two and then assign it to the variable i now gonna go to the top but this time we have a new value for the i so is two smaller than four well it is true that's why it's gonna go to the print
print it in the output two and then update the value again we're gonna have now three in the variables and then go back to the top if three smaller than four yes go print it in the output and update it so now our variable is equal to four and then go back to the top now it's gonna ask is four smaller than four well this is false and finally now python gonna break the loop
and go to the end and exit so as you can see python will keep looping and iterating as long as our condition is true until the condition change and turns to false and this is the only way to exit our loop this is how we build a counter in python using the while loop so now if you look again to the code we have here three sections the first one outside of the while loop we have something called initialization where you define the initial value of the loop variable
and then after the while you define your condition and now inside the loop you do the thing that you want but the third component that is very important you have to build an update mechanism so you have to change the value that we are checking in your condition so if you don't do those three steps either the loop will not work or you will be stuck in infinite loop
And by the way, my friends, if you are enjoying this type of free tutorials where I'm sketching the concepts behind the scenes and showing the codes and you would like to see more like this, then support the channel by subscribing, liking, commenting.
This really helps a lot.
So now let's go back.
So now let's build a counter that's going to count from one to five.
let's start first with the variable the initialization so we're gonna assign for it the one and after that we're gonna go and build our while loop and now we're gonna build our condition so as long as the counter is smaller or equal to five we have to iterate and loop so we're gonna say okay print the count
And now the last step, we don't have to forget about making the update mechanism.
So we're just going to go and add one for it.
And actually, that's it.
Let's go and execute it.
Now, if you look to the output, you can see we have a counter that counts from one to five.
so with that we have a nice counter and of course we can control now with those values the whole counter we could make the step of two so now if you go and execute it you will get in the output one three five so we have now bigger steps or we can say we're gonna count until ten not two five so with those values we could control the whole counter and this is what we call counter based loop
okay now let's take things to the next level now look to our loop variable over here this variable is getting the value from our code it is something hard coded and it starts with the one so there is no surprise how things can work over here but we can make things more dynamic where we ask the user for a value and then we check it and based on the user's input we decide whether we continue in the loop or we exit
Like for example, you are building a system that ask a very simple question to the user.
Do you agree?
And the system can expect from the user only yes, nothing else.
So if the user answers like no, maybe, or I will think about it, it will not accept it.
So we accept only yes, only if you agree with the system.
so it sounds a little bit like the system is interrogating a user and it only accepts yes so now how we gonna do it first let's go and build a variable like the answer and at the start it's gonna be empty so now let's go and build a condition like this with the while loop where you're gonna say if the answer from the user is not equal to what i want to hear the yes then
ask again and in order to ask actually we're gonna go and use the function input in order to get a value from the user and you're gonna say do you agree and then we're gonna give the answers yes or no then double point actually
That's it.
That's all about the loop.
We will not make anything incremental.
So all what we are doing is we're going to ask the user for a value.
Then we're going to evaluate it.
If it is not equal to yes, we keep asking.
So the system is going to end up in infinite loop until it hears yes.
Now let's go and maybe print as well outside of the loop something like thank you.
so now let's try this out and execute so now the system is asking very politely do you agree yes or no the user might say you know what i don't agree now the system can ask again do you agree yes or no and maybe the user says you know what maybe i don't know and as you can see the system is keep asking because the condition over here is fulfilled the answer is not equal to yes and we will stay in this loop until we hear yes until the condition is not anymore fulfilled
So now the system is asking for a third time.
Do you agree?
Well, this time, maybe the user is going to be scared a little bit and say yes.
And with that, you will get the thank you.
And that's because we broke the loop.
The condition is not anymore fulfilled.
And we went outside and printed the thank you.
So as you can see, it is very simple.
All what you have to do is to have a looping variable like here, the answer.
Then you build a condition on top of it and then you make sure inside each iteration there is possibility to change the value.
If you don't do that, otherwise you will be stuck in an infinite loop.
All right, so that's all about the while condition, the first type.
Now we're gonna go to the second type, the while true.
This one gonna be the most powerful, but yet the most risky.
So let's dive in.
We're gonna build it like this.
We start with the keyword while, and then after that, we use the value, the boolean value true.
So there is no condition, no counter, no nothing.
It's just the Boolean expression true.
And inside the loop, we're going to do something.
So now in the charts, it's going to be like this.
Python is going to say, OK, while true.
And this time we don't have like two answers.
We are not checking anything.
So we don't have a false and true.
It will be always true.
So there is no escape from this to the end.
now if you just leave it like this and execute it what's gonna happen you will be stuck in infinite loop so that's why we have to build somehow an exit in order to give it a chance to stop the loop and in order to do that we're gonna go and add a second condition using the f statements for example x is equal to the word stop and if this condition is false it's gonna go to the top of the loop
But if it is true, we have to do something.
So think about it.
What we could add here in order to exit a loop?
Well, as we learned, we have the three statements in order to control the loops.
And here we're going to go and use the break.
It is our only chance to stop the loop.
So we're going to go and include it inside the F statements.
If it is true, the value is equal to stop.
Then we're going to go to the break and stop the whole thing.
Now, of course, there are multiple ways and how to get a value for this variable.
But we could ask the user for a value using the input function.
So we ask the user for a value and then we check this value using the F statements.
So now let's see how Python is going to execute it.
Python is going to start by the true and here there is nothing to be decided.
So it's going to go immediately and enter the loop.
So here it's going to go and ask the user for a value.
Let's say our user entered high.
So Python is going to assign high for the X.
and then go to the next step and ask about it so is high equal equal to stop well no so we didn't get a stop from the user that's why python gonna go to the top of the loop and repeat the whole thing again maybe on the way we are printing or doing anything so again python gonna see okay it is true so i can keep going in the loop and then ask the user for another value and here the user gonna give us the value go and in the next step python gonna check it it
go equal to stop well it is not that's why it is false that's why we're gonna stay in the loop and then go back to the top after doing something and here again we have true and then ask the user for again a new value now find
let's say the user says okay i am done and give us the stop so now python gonna go and check did we get stopped from the user well yes finally we got the flag from the user that we are done and with that python gonna go to the break and execute it and this is the point where python gonna say oh we have break we have to exit we have to stop everything immediately and end our loop so as you can see this is another way on how to build a loop
so you build an infinite loop and then you add to it an if statement together with a break because this is your only chance to stop and exit the infinite loop otherwise you're gonna stuck in a black hole so my friends this is very risky but if you do it correctly everything gonna be fine oh i have to fix this first so
something like this okay i know you want to try the infinite loop right but first the disclaimer only run the following statements if your machine can handle it so only if you have strong cpu you have saved your work and you are ready to restart your pc so now let's go and build our infinite loop so we're gonna say while true and we're gonna print something like i am unstoppable
so something like this and again here warning if you execute it your things might crash if you stuck with it just eliminate it using the ctrl c now i'm gonna try this out and i hope i will not break the recording so um we'll go now as you can see it is executing so ctrl c and stop it and now it stops but i still hear the pc was not happy about it
okay so this is how we do the infinite loop i don't know if you are still out there here but i hope you are happy okay now back to our task where we keep asking the user to type yes to agree with us until they do previously we have solved it using the classical while with the condition but this time i'm gonna go and solve it using the while true so we're gonna start with the while and after that we don't need condition we will just say true double points and by the way make sure the first character is capitalized
And then after that, we go immediately and say we have a variable called answer.
So that means it's not like before where we have like initialization at the start.
We don't need that with the while loop because previously we have the answer in that condition.
And if you don't give it like any value, you will get an error.
But here we are not checking anything yet.
So that's why we don't have to define anything outside.
And the same thing, we're gonna ask for an input.
I'm just gonna go and copy this from here.
So with that, we will get a value from our user, but we have to check it, right?
Otherwise it will not work.
So in order to check it, we're gonna use the F statements.
And we're gonna say, if the answer is equal equal to yes,
And then double points.
So now, as you can see, this time we are checking whether the value is yes, not like here.
Right now, if the answer is yes, what can happen?
We don't have to ask anymore.
We got what you want and we break.
So we break the loop and get out.
of course we can put at the end like print so we exit and get out of our while loop and of course at the end completely outside the loop we can say thank you so now let's try this out i will just go and comment this out and let's go and execute it so as you can see we are getting the same question do you agree if you say no you're still gonna get the same question
Maybe I don't know.
So with that, as long as we are not fulfilling this condition, Python will keep asking.
So currently we are inside an infinite loop and we will not exit and break until we give the yes.
So now if I go and give it a yes, it's going to be happy and say thank you.
So as you can see now, the system is behaving exactly like the while condition.
And for this scenario, I'm going to go with the while true because it is easier to read.
So we are building an infinite loop and we keep repeating that until we get the right answer.
OK, so now time to challenge you a little bit.
Now, I want you to extend the current loop with the new requirements.
The first one is to limit the user to three attempts in total.
So if you don't get yes from three attempts, you don't ask anymore.
If the user type yes within the three attempts, then you have to print the following message.
Glad we are on the same page.
But if the user didn't give us a yes within the three attempts, you have to print, three strikes, you are out.
So now pause the video, solve those requirements, and then we're gonna do it together.
Okay, so now how are we gonna solve it?
Now, as you can see, the first requirement that we have to add like only three attempts.
So now currently it is open ended.
We keep repeating the same question until we get what we want.
But now with the new requirements, we have to make it limited.
And with that, we can rethink about maybe we go back to the classical while condition.
I know that we're going to iterate a maximum three times.
And for this, I'm going to say we go back to the normal while condition.
So that means we're going to go and replace the true with the condition.
And we're going to say as long as the attempts less than three times, you have to keep asking the same question.
But of course, we have to do the step before the while where we do the initialization.
So we're going to assign attempts to zero.
So this is the initial value.
This is our condition.
And of course, what is missing, my friends, we have to make the updates.
So for each iteration, we have to increase the attempts.
And usually we're going to put it at the ends before the next iteration.
So we're going to say attempts.
plus equal to one so that I increase the attempts with one.
So with that, we made the three attempts.
Now to the next requirement, we're going to say if the user did answer with yes within the three attempts, we have to print this message.
So again, in our logic, what we are saying, get me a value from the user.
And if the value is yes, then break.
So that means this is a perfect place in order to print this message.
So not only breaking out the loop,
we would like to give this nice message where we can print glad we are on the same page after that we break and then we increase the attempts okay so now let's go and try this out so now first i would like to test whether the attempts are working the new logic that means the system should not ask me more than three times not more than three iterations
now I'm gonna say no then ask me again maybe I don't know and now should exit exactly this is what happens we exit because we are not anymore fulfilling this condition so we are exiting the loop normally without using the break we had three iterations because the attempts were
smaller than three but for the fourth iteration the attempts were equal to three and three is not less than three that's why it is not fulfilling anymore the condition and we exited the while loop normally now let's go and test whether this is gonna work over here if we get yes it's gonna break and give us this message so let's say for the first one it was no
but for the second one we have yes as you can see the system didn't ask us for a third time because we fulfilled this condition inside the while loop the answer from the user is yes and and we fulfilled this condition that's why we entered to this block where python printed the message and then executed the break so that means the while loop stops because of the break not because we are not fulfilling the condition anymore so we didn't exit normally
We exited using the emergency break.
OK, so now let's talk about the last requirement.
Otherwise, we have to print this message.
Three strikes, you are out.
That means if I'm not getting yes, we should print this message.
Well, you might say, you know what?
Let's go and get this one instead of the thank you.
We don't need the thank you anymore.
And if we didn't exit with the break, we will get this message.
OK, so now let's try this out.
I'm going to say no, no and no.
Now as you can see it is working so we are getting okay three strikes you are out.
We didn't get a yes from you but my friends this will not work if we have a yes.
So let me show you what I mean.
Let's say okay first time no but the second time we say yes.
now we have an issue we have the two options together so it is saying glad we are on the same page and this came from here but at the same time it says yeah three strikes you are out well this is not exactly what we want it is either we are happy we are at the same page or you are out not both of them and that's because we have this print completely outside the while loop so after we broke the loop
the next instruction in python gonna be the print so this is gonna be executed whatever happened in our while loop but my friends i would like this to be executed only if we didn't break now think about it what we have used in the for loop in order to print something didn't break well we have to go and use the else statements
And yes, we can use else together with the while, not only for the for loop.
So let's go and add that.
We're going to say else double point and then put this print inside it.
So that means if the loop ended normally without a break, this message can be printed.
But if Python executed the break, then Python will not execute the else.
So either we exit normally and execute the else.
Otherwise, we exit with emergency using the break.
So now let's try this out.
The first one is going to be no.
Then the second one, we're going to say yes.
So now, as you can see, we are getting now only one message.
Glad we are on the same page.
And this came from the break.
That means we fulfilled this condition and Python printed the message, executed the break.
And with that, we exit immediately from the loop without executing the else statements.
And we can do the last test where we have...
never given a yes as you can see now we are getting the three strikes you are out and this came from the else statements so as you can see we can use the other control statements like the break pass continue the else statements in order to build our logic and control exactly how our loop can behave
So now if you compare the two types side by side, you can see with the while condition, we know when the loop should stop.
We have like something limited that's going to end the loop naturally.
But with the while loop, it's going to run forever.
It is infinite until something happens, until an event trigger the stop.
So that means with the while condition, things can end naturally.
But with the while true, you have always to add extra stuff like a condition using the F together with the break.
this is your only possibility to stop the loop and if you are talking about the risk of having infinite loop the while condition it is safer eventually it gonna exit unless you made like a big mistake but with the while true it is more risky because you are defining an infinite loop and you have to be careful designing the if and the break and as we learned the best use cases for the while condition
if you are building like a counter or you are building a limited retries or you are validating the output from the user but on the other side we use the while true if you have an open-end scenario and you are trying to get a trigger from something external like you are trying to connect to database to stream to abis
So at the end, I'm going to say always try to build things using the while condition.
But if you want something more advanced, more flexible, something that is open ended, then you have to use the while true.
But be careful how you build it.
Now we're gonna compare the two types of loops, the for loop and the while loop side by side.
With the for loop, you need a sequence where Python gonna go and create an object called iterator, but with the while loop, you don't need anything like that.
And for the for loop, Python gonna build like predefined condition.
It's gonna ask always, are we done with the sequence?
It's not something that you change, but with the while loop, you are totally flexible.
You can add your own condition for the iterations.
now about when to use what we use the for loop if we know already how many times we want to loop and as we learned we use the for loop in order to go through our data to do something like we loop over the rows the tables the columns the files in order to clean up or prepare the data but now in the other hand with the while loop we use it if we don't know how many times we want to iterate we are waiting for an event a trigger a condition
So we use it if we are building like retries in the login or you are trying to connect yourself to a database or APIs.
Now, the advantage of using the for loop is that it is simple, clear, and it is really hard to make an infinite loop with the for loop.
So it is really safe.
But of course, it is limited.
You have always to predefine how many times you want to iterate.
But in the other hand, with the while loop, it is more advanced, more flexible.
It adds dynamic to your logic, but it is complex and you have a high risk of building an infinite loop.
So at the end, think of the for loop as like you have a playlist.
You know exactly how many songs you have and the while loop you are waiting for a reply.
You keep checking until the condition is met.
So that's all about when to use what.
right friends so with that we have learned everything about the while loop and as well we have learned everything about how to repeat a code in python and all the use cases why we do that and with that we have covered everything as well about the whole control flow so with that you are not anymore writing a code that is a straight line we are making some complex logic you are controlling the flow the logic on how your code is executed
using the conditional statements the if else else if and as well how to repeat a code using for and while and all the different shapes and designs in each type so with that we have covered everything in this section the control flow and next we're going to speak about something very important especially if you are working with data finally we're gonna learn the modern data types the data structures
so if you liked this video and you would like to have more free content like this then support the channel by subscribing liking commenting this really can help the channel to grow and to reach others like you so thank you so much for watching and i will see you in the next video
Similar videos: Python Loops

Как подключиться к PostgreSQL на Python | Создание таблицы, добавление, удаление, вывод данных

Распознавание текста с изображения на Python | EasyOCR vs Tesseract | Компьютерное зрение

Python Full Course for Beginners

Учимся пентесту на практике с нуля | TryHackMe - Wekor | Wordpress | SQL инъекции

Асинхронность, многопоточность, многопроцессность в python | Библиотека asyncio и асинхронный код

