📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #6 – Циклы и операторы в них (for, while)

Школа itProger / Программирование15:49

Transcription

Hello!

In this lesson, we will learn how to work with loops and loop operators in Python. Before we begin, I would like to recommend the website itproger.com. On this site, you will find code, homework assignments, and a lot of other useful information. The link to this lesson on the site will be in the description of this video.

To start, let's define what loops are. Loops are special constructs that allow us to execute code multiple times in a row, and we can specify the number of executions ourselves. In Python, there are two types of loops: the for loop and the while loop. We will study these loops now.

First, let's get acquainted with the for loop. To write it, we need to use the keyword "for." Next, we need to create a variable, which we can name anything. Let's just call it "i." This variable will be used inside the loop. Then, we simply specify a range. To indicate the range, we can use a method in Python called "range." Here, let's say our range will go up to six numbers.

Now, I want to do something inside this loop. For starters, let's just print this variable each time. If we run this now, we will see that the value of the variable is printed each time. You can notice that the initial value of the variable starts at zero and goes up to five. This means that the number itself is not included, and the count goes from zero to this number, not including it.

Additionally, everything you write inside the loop must be indented. The concept is the same as with conditional statements. If you need to write multiple lines of code, each line must be indented by the same amount.

Besides specifying a loop that goes from zero to a certain number, we can also say that our loop should start from one and go up to six, not including it. In this case, we notice that zero is gone, and we have five iterations, resulting in the numbers 1, 2, 3, 4, and 5 being printed.

You can also pass a third parameter to this function, which indicates how much we will increase our variable. By default, this variable increases by one. So we get the output 1, 2, 3, 4, and so on. However, if we specify the number two, then initially we will have one, then three, then five, and nothing more will be printed.

Thus, to create a loop, we can use the range function and the for loop. Here, we can specify the starting value, the ending value, and a specific step if we want the value to start from zero. We can simply use one parameter to indicate an ending value.

In general, this is how we create the loop we need.

Through the for loop, we can also iterate over strings. The fact is that any string is a list made up of many characters, and we can iterate over each character.

To start, let's create a string. Let's say it will be "Hello world" or something like that. Then we create the loop in the same way, but instead of the range function, we specify the string we will iterate over.

If I simply print the variable "i" inside the loop, we will get the result where each character from the string is printed. Even the space character is printed.

We can actually work with each character as we wish. For example, I can multiply each character by three, and in that case, we see that each character is printed three times. However, this isn't very practical.

Let's develop a small program where I will search for a specific character in the string. How can we do this? We can write a condition directly in the loop. In this condition, we will say that if our variable is equal to the exclamation mark, for example, that is the character I want to search for in the string.

If any character equals the exclamation mark, we will print "Yes" to indicate that such a character was found.

Let's run the program again, and we notice that nothing was printed on the screen, which means that such a character simply does not exist in the string.

However, if I add this character to the string, we see "Yes" printed, confirming that this character is indeed present.

We could also search for another character, like "L." In this case, it prints "Yes" three times because there are indeed three such characters.

Of course, we don't have to just print everything because that's not very interesting. Let's create a variable called "count," starting at zero. Each time we find a specific character, we will increase this variable by one. After the loop ends, we will print the number of elements found.

Let's write that we will print our variable "count." If we run this, we see that the number of these characters is three.

If I try to find another character, like "W," we see that there are zero occurrences. Why wasn't the letter "W" found? Because we are searching for it in lowercase, while it is in uppercase in the string. For Python, these are considered two different letters, which is why no such character was found in the string.

In addition to the for loop, Python also has the while loop. The essence of this loop is that it also allows us to execute certain code multiple times, and we can determine the number of executions ourselves.

The difference between the for loop and the while loop lies only in the format of writing. In the for loop, we always use the keyword "in" to indicate what we are iterating over, whether it's a range or a string.

In the while loop, we can simply specify a condition. Let's create a simple example of this loop.

I will create a variable, let's call it "i," and initially set it to five. Then we create the loop using the keyword "while," and here we just need to specify one condition. We will say that the loop will continue as long as "i" is less than 15.

Inside the loop, I will print the variable and add two to it each time. If I run this now, we see that the initial value of the variable was five, and this value was printed. Then we increased the variable by two, and again printed it.

So we see the outputs 5, 7, 9, 11, and 13. The number 15 is not printed because it does not meet the condition.

I can change the condition to allow "i" to be less than or equal to 15, and if I run this now, we see that 15 is printed because the condition allows it.

While loops are convenient because you can simply specify a condition, and as long as that condition is true, the loop will continue to run.

For example, I can create a variable called "hasCar," set it to True, and I can specify that as long as this variable is True, the code will execute.

However, I do not recommend running such a loop because it will be an infinite loop, which can freeze your computer since it will never stop. This loop is infinite because the condition will always be true, and we never change the value of this variable.

Instead, let's do something different. We will get some input from the user and check it immediately. If they type the keyword "stop," we will set the variable "hasCar" to False.

Now let's run this. We are waiting for input. If I enter anything but the word "stop," we will continue waiting for input.

Let's make it clearer by printing a message. I will write "Enter Data" to indicate what to do.

Now we can run this again. We can enter data, and it will continue until I type the keyword "stop." Once I type "stop," the variable will be set to False, and we will exit the loop.

Thus, the while loop is convenient when we need to specify a condition and then execute a loop. The for loop is useful when we need to iterate over something, whether it's a range, a string, or a list. We will study lists a bit later.

Now, let's explore some operators that can be used in loops. We will study these operators in the for loop, but they can also be used in the while loop.

First, let's create a loop that goes from zero to ten, not including it. It might be better to go up to eleven. Each time, I will print the value of the variable.

If we run this, we get the expected result from zero to ten, not including ten.

Now, let's look at the "break" operator. With this operator, we can exit the loop. For example, I want to exit the loop as soon as we reach the number five.

To do this, I will write a small condition that says if "i" is equal to five, we will use the "break" operator. If I run this, we will see that the numbers 1, 2, 3, and 4 are printed, but five is not printed because we checked the condition and exited the loop when we reached five.

In addition to "break," there is also the "continue" operator. Unlike "break," it does not exit the loop; it simply skips one iteration.

Let's write that. I want to check if the number is even. If it is, I will use the "continue" operator. If I run this now, we will see only the odd numbers printed.

This happens because when we find an even number, we skip that iteration. If we hit "continue," the rest of the code is not executed, and we go back to the start of the loop for the next iteration.

Finally, let's develop a small program to find a character in a specific string. We have done this before, but now let's do it correctly.

First, we create a loop to iterate over a string, like "Hello." We will write a condition that if we find the character "L," we will set the variable "found" to True. If we do not find that character, we will set it to False.

We could create this variable at the top. I don't want to assign it any value initially, so I can set it to NaN, which means "nothing."

Now, at the end, we will print this variable to see what we got. Logically, it should print True, but it prints False because when the loop ends, we set it to False.

To fix this, we need to add a "break" operator. This way, as soon as we find the character, we exit the loop, and the "else" condition will not execute.

If I run this now, we see that it prints True. If I search for a non-existent character, like "R," it will print False.

Now this loop correctly finds a specific character in a string.

Well, our lesson has come to an end. In this lesson, we learned how to work with loops. We studied the for and while loops, and we also explored operators that can be used within them, such as "continue" and "break."

That's all from me. I hope you enjoyed the lesson. If so, don't forget to subscribe to the channel and join our social media. All the links will be in the description of this video.

That's all for now. See you soon! Goodbye!