Looping in Python
Looping in Python
WHILE LOOP
A while loop in python runs a bunch of code or statements again and again
until the given condition is true when the condition becomes false, the loop
terminates its repetition.
We must use the keyword “while”, along with it we have to put a condition in
parenthesis and after that, a colon is placed. The condition could be either true
or false. Until the condition is true, the loop will keep on executing again and
again. If we use a certain sort of condition in our while loop that, it never
becomes false then the program will keep on running endlessly, until we stop it
by force. So, this kind of mistake in our syntax is known as logical/human error.
To terminate an infinite loop, you can press CTRL+C on your system.
Syntax
while condition:
Body of your While Loop
Example
count = 0
while count < 9:
print("The count is : ", count)
count = count + 1
print("Good bye!")
Output
Example 2
num = 10
while num > 1:
print("Hello")
Example
i = 1
j = 5
while i < 4:
while j < 8:
print(i, ",", j)
j = j + 1
i = i + 1
Output
FOR LOOP
Like all the other functions we have seen so far, for loop is also just a
programming function that iterates a statement or several statements based
on specific boundaries under certain defined conditions, that are the basis of
the loop.
Note that the statement that the loop iterates must be present inside the body
of the loop.
Regarding loops, iteration means going through some code again and again. In
programing it has the same meaning, the only difference is that the iteration
depends upon certain conditions and upon its fulfillment, the iteration stops,
and the compiler moves forward.
The concept of the loop could be easily understood using an example of songs
playlist. When we like a song, we set it on repeat, and then it automatically
starts playing again and again. The same concept is used in programming, we
set a part of code for looping and the same part of the code executes until the
certain condition that we provided is fulfilled. You must be thinking that in
song playlist the song keeps on playing until we stop it, the same scenario can
be made in case of loops, if we put a certain condition that the loop could not
fulfill, then it will continue to iterate endlessly until stopped by force.
An example of where loop could be helpful to us could be in areas where a lot
of data must be printed on the screen and physically writing that many printing
statements could be difficult or in some cases impossible.
Advantages of Loops:
• Complex problems can be simplified using loops
• Less amount of code required for our program
• Lesser code so lesser chance or error
• Saves a lot of time
• Can write code that is practically impossible to be written
• Programs that require too many iterations such as searching and sorting
algorithms can be simplified using loops
Example 1
Print 0 to 10 using for loop.
for i in range(11):
print(i)
Example 2
Print 5 to 20 using for loop.
for x in range(5, 21):
print(x)
Example 3
Calculate sum of numbers from 1 to 10.
total = 0
for x in range(1, 11):
total = total + x
print(total)
Output
Output
Steps in FOR-LOOP
Suppose we want to print numbers from 1 to 20, but by adding +2 every time.
Ex. We need to print -> 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
Example
for i in range(1, 21, 2):
print(i, end=" ")
Negative FOR-LOOP
Print all the numbers from 1 to 10, in reverse order.
for i in range(10, 0, -1):
print(i)
Nested FOR-LOOP
Loops defined within another Loop are called Nested Loops. Nested loops are
used to iterate matrix elements or to perform complex computation.
When an outer loop contains an inner loop in its body it is called Nested
Looping.
for <expression>:
for <expression>:
Body
Example
Print the following pattern
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Solution
for i in range(5):
for j in range(5):
print("*", end=" ")
print()
Example
Print the following pattern:
*
* *
* * *
* * * *
* * * * *
Solution
for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()
d. 4 8 12 16 20 24 28 32 36 40
e. 5 10 15 20 25 30 35 40 45 50
For solutions, check the end of the book.