Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 29, 2021 10:26 am GMT

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.

Here is an example-

n=0while n<10:    print(n)    n+=1
0123456789

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=ndivisor=mrem=dividend%divisorwhile rem>0:    dividend=divisor    divisor=rem    rem=dividend%divisorgcd=divisorlcm=(n*m)//gcdprint("gcd= ",gcd,"  lcm= ",lcm)

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 trueelse:  #statement executed if condition is false.

The program below will make things clear.

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

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=0for i in range (0,n+1):    count+=iprint(count)count2=0i=0while(i<=n): count2+=i i+=1print(count2)
Please enter a number 10050505050

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

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

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

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

121,231,31,2,341,41,2,41,2,3,451,51,2,51,2,3,51,2,3,4,5

Mind the trailing commas... Answer here

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

11,11,21,2,11,2,21,2,31,2,3,11,2,3,21,2,3,31,2,3,41,2,3,4,11,2,3,4,21,2,3,4,31,2,3,4,41,2,3,4,5

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...


Original Link: https://dev.to/aatmaj/learning-python-basic-course-day-6-the-while-loop-and-more-questions-k23

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To