A very good way to shorten your lines of code in Python is by writing while or for loops.
And this because if you want to print the large amount of numbers that's on your screen, you need to
write so many print statements.
If you want to print the numbers, you need to write the print statements.
Right, so that you can use the while loop.
So let's write I equals zero.
OK, and here's how you write a while.
So.
Right, while I use more than or equal.
Down five, so here we look while I is more or equal to five.
OK, so while this is true, we want to print.
Every night and then in the end, we can simply increment.
I.
I of course, I lost one here in that case, this means that in every law.
So first I will be equal to zero, then it will be printed, it will be incremented by one, OK?
And then the while loop will repeat and this will continue until I becomes equal to five.
Then after it is equal to five, it will become equal to six.
So loss is equal to six.
This condition here will not be met and for that reason will get out of this law.
So let's run our code now and see what happens.
And you can see that we print all the numbers from zero to five, which is exactly what we expected.
You can also go further by displaying not the numbers, but certain amount of character slips, right?
Print I multiplication, same in here, we can write the symbol.
So this means that for every loop we're going to type the symbol I as many times as the value of five
is.
So here's how this system go.
So we can see here that in the final statement we have five, then we have four, three, two, and
then only one.
And this is how that while statement works.
And there, as you can see, some different cases of what you can print or what you can do inside.
Another very useful tool in any programming language in Python as well is the for loop.
OK, so for loop usually loops around sets of numbers.
Let's see.
So for that reason I will create a list here and we're actually going to cover the list a little bit
later as well.
But here I will create a simple list of temperatures so temperatures will be equal to let's write five
different temperatures.
So the first one will be six, seven, then 30.
Then seven two, then four to three, and finally 91.
So this is the least of our temperatures.
Then let's create a law that would prevent every of the temperatures in that list.
So let's write for item in.
Temperatures, so here you can see that item is for each item in the set of the temperatures.
So for each of these values, we're going to do something inside the for loop.
So what I will simply do here is to print the item or the current value that is taken from this set.
So let's see what is going to be the result.
Now, I think you can guess it, but yes, we'll get each of the temperatures on different line.
OK, quite simple.
And imagine if you have to do this with a while loop.
We all need to write while and then for every step you need to increment the different position of the
set and the loop will be way longer, actually double longer.
Comparing to just for loop that we just wrote and for that reason sometimes is quite useful using the
for loop in these cases.
Another quite useful data structure for using in the Falu is the range.
So I will actually change the temperatures here to a range of numbers and you can write.
Numbers.
Equals range then, and this means that here we're creating a range of numbers.
OK, and we can see right here for each number in numbers, print number.
OK, so in that way, we don't need to write every number, but we simply have the set of the numbers
between zero and nine.
OK, and we can print each of them.
So if we simply press run us right or wrong, you can see that we have all the numbers here printed
from zero to nine.
So in the range we can do some actual quite fun stuff.
So the first thing you can do is simply write two numbers here.
So five and ten.
And if you do this, you are simply going to get the numbers between five and 10.
So you can also specify that the lower and the upper level.
Then another thing you can do within the range structure is let's say you want to print some numbers
between then.
And 100, but also you want to print every five numbers, so here I write five.
OK, and this is the increment between each number.
So if I press here, you can see that we are getting excellent numbers between ten and nine to five
or excellent between the 100th and that range, but the difference between them is five.
So we get 10, 15, 20 and so on.
And yeah, you can use the range in your advantage for four loops, which is quite useful then.