#20 Python Tutorial for Beginners | While Loop in Python

 

#20 Python Tutorial for Beginners | While Loop in Python

Loops in Python

In programming, loops are used to repeat a set of statements multiple times. Instead of manually copying and pasting the same code, we can use loops to automate the process.

There are two types of loops in Python: while loop and for loop. In this blog, we will focus on the while loop.

While Loop

The while loop is used to repeat a set of statements as long as a specific condition is true. The basic structure of a while loop is as follows:

while condition: # code to be repeated

Here, the condition is checked before each iteration of the loop. If the condition is true, the code inside the loop is executed. This process continues until the condition becomes false.

Let's understand this with an example. Suppose we want to print the statement "That is good" multiple times. We can use a while loop to do that.

i = 1while i <= 5: print("That is good") i = i + 1

In this example, we initialize a variable i with the value 1. The condition i <= 5 is checked before each iteration. If the condition is true, the statement "That is good" is printed and the value of i is incremented by 1. This process continues until i becomes greater than 5.

We can also use a while loop to print the value of i along with the statement "That is good". Here's how:

i = 1while i <= 5: print("That is good", i) i = i + 1

In this modified example, the value of i is printed along with the statement "That is good".

We can also use a while loop to print the statement "That is good" in reverse order. Here's how:

i = 5while i >= 1: print("That is good") i = i - 1

In this example, we initialize i with the value 5 and decrement its value by 1 in each iteration. The loop continues until i becomes less than 1.

Similarly, we can use multiple while loops inside each other to perform complex tasks.

i = 1while i <= 5: print("Telescope") j = 1 while j <= 4: print("Rocks") j = j + 1 i = i + 1

In this example, the statement "Telescope" is printed 5 times, and inside each iteration of the outer while loop, the statement "Rocks" is printed 4 times.

Understanding Nested While Loops

Let's take a look at an example of a nested while loop and how it works:

line = "rocks"i = 1while i <= 4:    j = 1    while j <= i:        print(line, end=", ")        j += 1    i += 1

This code will output:

rocks, rocks, rocks, rocks, rocks, rocks, rocks, rocks, rocks, rocks, 

Let's break down how this code works:

  • We have an outer loop that runs from i = 1 to i = 4.
  • Inside the outer loop, we have an inner loop that runs from j = 1 to j = i.
  • Inside the inner loop, we print the value of line, which is "rocks".
  • We increment j by 1 after each iteration of the inner loop.
  • After the inner loop completes, we increment i by 1 and start the next iteration of the outer loop.

By nesting these while loops, we can control the number of times the inner loop runs based on the value of the outer loop variable. In this example, the number of "rocks" printed in each line increases by 1 with each iteration of the outer loop.

It's important to note that the inner loop must complete all its iterations before the outer loop moves on to the next iteration. This is similar to how each day has 24 hours, and the hours are counted from 0 to 23 before moving on to the next day.

Understanding nested loops can be challenging at first, but with practice, it becomes easier to grasp. In the next video, we'll explore examples of nested loops to print patterns like stars. If you're enjoying this series, let me know in the comments. Thanks for reading !

https://www.highcpmrevenuegate.com/apkswh79?key=b531254fe2b7e0765e5a0ebbe0dae43f

How would you rate this .?

Comments

Popular posts from this blog

#22 Python Tutorial for Beginners | Break Continue Pass in Python

Python Tutorial for Beginners

#19 Python Tutorial for Beginners | If Elif Else Statement in Python