If we want to print “Python Lessons” on the screen ten times, we need to use the print()
function ten times.
That’s what we’re going to do with Python loops. There are two different types of loops; while
and for
, I will also touch on the range()
and len()
functions and break
, continue
and in statements that we need in these loops.
while
loop
It is a type of loop that allows us to rerun the code we wrote in Python. The working logic is that the while
loop is repeated every time, if it satisfies the condition, it enters the loop again, if it does not, the loop ends, let’s show it with an example; Let’s print the numbers between 0 and 10 on the screen.
number = 0
while number <= 10:
print(number)
number = number + 1
Let’s examine the codes in order. In the first line, we created a variable called number
and set its value to zero. In the second line, the loop repeats as long as the value of the number variable is less than and equal to 10.
In the third line, we printed the number
variable on the screen, and in the fourth line, we increased the number variable by one.
How the program will work;
First, the value of the number
variable is zero, it will compare it with ten, if it is small and equal, it will enter the loop, first print the zero value on the screen, then increase the number
variable by one, the new value of the number
variable will be one, and it will come to the beginning of the loop again. In the same way, it will compare with the value of ten, print the new value on the screen and increase the value by one, until the value of the number
variable is greater than ten, and it will end the loop.
for
loop
Another loop in Python Loops is the for
loop. Functioning the same as the while
loop, the usage of this loop is slightly different, but the functions are the same. Let’s print the numbers up to 10, which we did with the while
loop, with the for
loop;
for number in range(0, 11):
print(number)
Here, too, the variable is a number
, and the condition is that the number variable is between 0 and 10. Let’s show it with another example.
for letter in "Python":
print(letter)
Now that we’ve seen both types of loops in the Python Loops topic. If you ask which one is better to use, the two do not have an advantage over each other. While the while
loop is better in one application, the for
loop may be more advantageous in another application.
range
function
range(start value, end value, change amount)
-
range(10)
: Specifies numbers in the range from 0 to 9. It defaults to 0 because we do not specify the initial value, and 1 by default because we do not specify the change amount. And another thing to note is that it does not include the end value. -
range(2, 20, 2)
: contains numbers in the form of binary increments of numbers from 2 to 19. -
range(20, 2, -2)
: Includes numbers from 20 to 2 with binary decrement
in
operator
Example:
for number in range(20):
where the number
variable takes all values between 0 and 19.
len
function
Example:
text = "Python"
print(len(text))
or in list:
list =[2, 36, "Python", 2, 5]
print(len(list))
break
statement
username = "baransel"
password = "python"
total_tries = 3
while total_tries > 0:
total_tries -= 1
input_username = input("Enter your username: ")
input_password = input("Enter your password: ")
if input_username == username and input_password == password:
print("You have successfully logged into the system.")
else:
print("User information is incorrect, try again!")
When we enter the username and password correctly, it still asks us for the username and password, but when we enter the information we want correctly, let’s log in and the process ends, let’s use the break
statement for this;
username = "baransel"
password = "python"
total_tries = 3
while total_tries > 0:
total_tries -= 1
input_username = input("Enter your username: ")
input_password = input("Enter your password: ")
if input_username == username and input_password == password:
print("You have successfully logged into the system.")
break
else:
print("User information is incorrect, try again!")
continue
statement
for number in range(20):
if number % 2 == 0:
continue
else:
print(number)
Here, if the number is dividing the variable by 2, the interpreter will skip the conditions in which the number variable is divided by 2, thanks to the continue statement, and come to the beginning of the loop.