Lesson-4: Python While Loop
Python While Loop
Cycles are called consecutive operations. When we want to repeat a process 10 times, we put it in a loop. If we want a process, we can also loop it forever.
While Loop
The while loop executes the codes contained in it continuously repeatedly while a condition is being met.
While (condition):
line of code
line of code
line of code
As long as the condition written next to the While command is true, the lines of code in it are constantly repeated in a loop (loop). After the condition, a colon (:) must be placed. Lines of Code must be initialized from the next tab.
EXAMPLE – 1:
a=1
While (a<10):
print a
a=a+1
In the above example, variable a was initially assigned a value of 1. a loop is opened as long as variable a is less than 10, and the codes in this loop will work repeatedly as long as variable a is less than 10.
In the loop, variable a will be printed on the screen and its value will be increased by +1. Then, when the loop repeats, the new value of a will be printed on the screen, and the value of a will be increased again by+1. variable a will exit the loop when it becomes 10.
In the above example, numbers from 1 to 10 are printed on the screen respectively.
EXAMPLE – 2:
While 7==7 :
a= input ("Enter a data :")
print ("DATA=",a)
In the above example, the while loop will work as long as the condition is met. 7==7 commands in the loop will always be repeated because it will generate a value of True. This is an infinite loop.
In the loop, the user is asked to enter data and assigned to variable A. Immediately after that, the data in the variable A is printed on the screen. After that, the loop returns to the beginning and asks for data entry again.
EXAMPLE – 3:
findnum=55
while (true):
a= int (input ("Enter a number:" ) )
if findnum<a:
print ("Enter A smaller number ...")
elif findnum>a:
print (" Enter a larger number...")
else:
print (" CONGRATULATIONS, YOU FOUND THE NUMBER...")
break
Above are a number of puzzle game codes. Initially, the number variable is given a value of 55, and the user is asked to find this value. The while loop will return as long as it is true. This means that this cycle is an infinite cycle.
In the loop, we ask the user to guess a number. If (if), we determine whether this entered Number is greater than or less than number=55, and we print the necessary warnings on the screen. If it correctly guesses the number, it enters the else structure and exits the loop with the “break” command after the “congratulations” message.
Python Break Command:
It breaks the cycle it’s in, so it comes out. While is a command that we use a lot in loops. In particular, we also use infinite loops where you need to exit the loop to ensure control.
EXAMPLE -4:
x=1
While x<=100:
if x%2 == 1 :
print (x, " odd number.")
else:
print (x, " even number.")
x=x+1
print ("Process Finished")
In the example above, there is a loop block in while, as long as the variable X is less than 100 and equal to 100. Codes in the loop do not work as soon as the x variable is 101, and the loop ends. If in the loop, the value remaining from the Section X in 2 has a condition equal to one. If this condition is True, the number is an odd number, if not (else) the number is an even number.
The above codes write numbers from 1 to 100 on their sides whether they are odd or even.
EXAMPLE -5:
# Sum of odd numbers from 1 to 100
x=0
result=0
while x <= 100:
x = x+1
if x%2 == 1:
result= result + x
else:
continue
print (" Total= ", result)
At first, x and result variables are assigned zero. The while loop will return as long as the x variable is equal to 100 or less than 100. when entering the loop, the value of the variable x is increased by one. then, if (if) x is 1, which remains from the part of x with two, it is understood that the number is odd, and the following Operation must be performed:
result = result + x
Here, the value in the variable x is added to the result variable. Since only odd numbers will be collected at the end of the cycle, the sum of odd numbers from one to one hundred is calculated. as soon as the x variable is 101, it exits the loop and prints the total value to the screen using the print command.