Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 8: Loops in Python

0 Replies, 678 Views

[Image: images?q=tbn:ANd9GcTmzA9Nu4_GpHpUbXTwYC3...c&usqp=CAU]
Loops:
Getting in deeper with python is a lot of fun. Now we are going to introduce a new concept in Python that is Loops. If you want to repeat the statement or a block of statements in python we use loops. There are two types of loops i.e for loops and while loops. But wait why we have two types of loops when both do the same task. So the answer for your question is for loop is used when the number of iteration is known and while loop is used when number of repetition is unknown. For Example, if you are given to print all numbers from 0 to 30 you have to use for loop as the repetition is known in this example but when we are asking user to enter password we are not sure that in how many attempts he/she will enter the correct password so here we will use while loop. So now get into code and see how these examples are used in code…


Code:
# Program to demonstrate for loop

for i in range(0,30):
  print (i)


First understand the syntax of loop statement ‘i’ is counter variable. And range is from (0,30) 0 is the initial number and final number is 30 when it becomes equal to 30 the loop terminates so 30 will never be printed.

Now let’s code while loop..

Code:
#Program to demonstrate while loop
answer = ''

while answer != 'George Washington':
  answer = input("Who is the first president of the United states? ")
 
print("Correct answer")

Idea Key point: when there is no counter variable this is known as infinite loop because this loop will never terminate untill you input the correct answer.
Let’s see some other programs


Code:
# Program to print times table of a number
table = int(input("Enter a number: \n"))
print("\nHere is your times table of " + str(table) + "\n")
for i in range(1, 11):
  print(table*i)

Now let’s convert this code into a ‘while’ loop. While loops do not have built-in counters we will define counter for it

Code:
# Program to print times table of
# number in while loop
i = 1 # Defining counter
table = int(input("Enter a number: "))
print("\nHere is your times table of " + str(table) + "\n")
while(i <= 10):
  print(table*i)
  i += 1  # short for i = i + 1

Exclamation Task to do: print all the multiples of a given number under range 80


Idea In next threads we will cover nested loops and some basic patterns.


("Allah Hafiz" and "Good Bye" Till the next post )

"Keep Learning and Keep exploring"


 






Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,231 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,614 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,647 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,827 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,593 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,776 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,805 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,808 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,803 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)