DEV Community

Cover image for Learning Python-Basic course: Day 6, The While Loop and more questions🤓!
Aatmaj
Aatmaj

Posted on

Learning Python-Basic course: Day 6, The While Loop and more questions🤓!

🤟WELCOME 👍 Today is the day six of our course! 🔥 The syllabus for today is learning about the while loop and solving some exciting questions like 👉 GCD & LCM 💎 and the Collatz Conjecture😃


The While Loop-
The While Loop in Python shares a similer syntax like others. If the conditions are true, the loop is executed, else skipped over.

while conditions :
 #statements executed if the conditions are true.
Enter fullscreen mode Exit fullscreen mode

Here is an example-

n=0
while n<10:
    print(n)
    n+=1
Enter fullscreen mode Exit fullscreen mode
0
1
2
3
4
5
6
7
8
9
Enter fullscreen mode Exit fullscreen mode

Unlike C, there is no do-while in Python. Neither is switch case.

Here is a sample program to find the GCD and LCM of two numbers--

n=int(input("Please enter first number "))
m=int(input("Please enter second number "))
dividend=n
divisor=m

rem=dividend%divisor
while rem>0:
    dividend=divisor
    divisor=rem
    rem=dividend%divisor
gcd=divisor
lcm=(n*m)//gcd
print("gcd= ",gcd,"  lcm= ",lcm)

Enter fullscreen mode Exit fullscreen mode

In Python, one more thing is added known as while - else. With the else statement, we can run a block of code when the condition is false. This block is run only once. Syntax-

while condition:
  #statement executed if condition is true
else:
  #statement executed if condition is false.
Enter fullscreen mode Exit fullscreen mode

The program below will make things clear.

i = 1 
while i < 10:
  print(i)
  i += 1
else:
  print("i is not less than 10 anymore")
Enter fullscreen mode Exit fullscreen mode

Here is a sample program in which the sum of numbers is shown by both methods, for loop and while loop-

n=int(input("Please enter a number "))
count=0
for i in range (0,n+1):
    count+=i
print(count)
count2=0
i=0
while(i<=n):
 count2+=i
 i+=1
print(count2)
Enter fullscreen mode Exit fullscreen mode
Please enter a number 100
5050
5050
Enter fullscreen mode Exit fullscreen mode

Here is another sample program of the Collatz conjuncture

a=int(input("Please enter a number "))
while(a!=1):
    if(a%2==0):
        a=a/2
    else:
        a=3*a+1
Enter fullscreen mode Exit fullscreen mode

One thing to note here is the indentation. If we put else in same indent level as while, it will become a while-else type of statement and will give wrong results. Hence, always be careful in matching the else statements and the corresponding indents. It is useful many times to leave space equal to four spaces or one tab in between indents to avoid confusion and make the code clear.

Infinite Loops-We can create infinite loops in Python using the while loop as shown

while True:
 #statements
Enter fullscreen mode Exit fullscreen mode

BOOM!!!💥 Be careful to terminate the program well while using the While loop.
We can transform the Collatz code into a infinite loop and give a breaking condition as shown-

a=int(input("Please enter a number "))
while(True):
    if(a%2==0):
        a=a/2
    else:
        a=3*a+1
    if(a==1):
        break
Enter fullscreen mode Exit fullscreen mode

Exercise-
1) Write a program to reverse a number (any-digit). Answer here

2) Write a program to find the factors of a number using prime factorization.
Answer here

3)Modify the prime number program we did in day 4 to replace for loop by while loop. Answer here

More questions on the 'For' loop-
1) Write a program to output the following

1
2
1,2
3
1,3
1,2,3
4
1,4
1,2,4
1,2,3,4
5
1,5
1,2,5
1,2,3,5
1,2,3,4,5
Enter fullscreen mode Exit fullscreen mode

Mind the trailing commas... Answer here

2) CODING CHALLENGE ⚔️
swap two letters in the above solution to give this output-

1
1,1
1,2
1,2,1
1,2,2
1,2,3
1,2,3,1
1,2,3,2
1,2,3,3
1,2,3,4
1,2,3,4,1
1,2,3,4,2
1,2,3,4,3
1,2,3,4,4
1,2,3,4,5
Enter fullscreen mode Exit fullscreen mode

Answer here


✌️So friends that's all for now. 😊 Hope you all are having fun.😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me. 😉
Thank you all👍


🤫psst... follow me on dev.to and GitHub for updates...

Discussion (0)