Python for loop:

for loop construct in Python enables you to repeat a piece of code a predetermined number of times. The for loop's syntax is as follows:

for i in range(1, 10):

The list of numbers in the range function ranges from 1 to 10. The code enclosed in the brackets will be run for each number in the list as the for loop iterates over it.

Python for loop example:

You may use this for loop to print the digits 1 through 10. This is what you would type:

for i in range(1, 10):

print("{0}".format(i))

Python while loop:

A while loop in Python runs a block of code as long as a specific condition is met. This is a useful technique for repeating an action until a specific condition is met.

Python while loop example:

Here is an illustration of a Python while loop:

while True:

print("In while loop")

time.sleep(.5)

print("Out while loop")

As long as the condition is met, the while loop will run the code contained within the while block. The variable "time" must have a value that is less than or equal to.5 seconds in this case.

Python for loop and Python while loop examples: 

These are the simple python for loop and Python while loop programs for practice.We have listed six basic python for loop and python while loop programs and their solutions .


Python loop Program 1 :

Ask for two numbers. If the first one is larger than the second, display the second number first and then the first number, otherwise, show the first number first and then the second.

Solution:

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

if num1>num2:
    print(num1,num2)
elif num2>num1:
    print(num2,num1)
else:
    print(num1, num2)
Python for loop | Python while loop
Python loops 



Python loop Program 2 :

Ask the user to enter their name and then display their name three times.

Solution:

name = str(input("Enter your name: "))

for i in range(3):
    print(name)
Python for loop | Python while loop
Python for loop



Python loop Program 3 :

Ask the user to enter their name and display each letter in their name on a separate line.

Solution:

name = str(input("Enter your name: "))

for i in range(len(name)):
    print(name[i])
Python for loop | Python while loop
For loop



Python loop Program 4 :

Ask how many people the user wants to invite to a party. If they enter a number below 10, ask for the names and after each name display “[name] has been invited”. If they enter a number that is 10 or higher, display the message “Too many people”.

Solution:

num = int(input("Enter the number of people you want to invite: "))

if num >= 10:
    print("Too many people")
else:
    for i in range(num):
        name = str(input("Enter the name of "+ str(i+1) +" person: "))
        print(name + " has been invited")
Python for loop | Python while loop
Python Loops 



Python loop Program 5 :

5- Set the total to 0 to start with. While the total is 50 or less, ask the user to input a number. Add that number to the total and print the message “The total is… [total]”. Stop the loop when the total is over 50.

Solution:

total = 0

while total<=50:
    total += int(input("Enter a number: "))

print(total)
Python for loop | Python while loop
Loops



Python loop Program 6:

Ask the user to enter a number. Keep asking until they enter a value over 5 and then display the message “The last number you entered was a [number]” and stop the program.

Solution:

num1 = int(input("Enter a number over 5: "))

while num1<=5:
    num1 = int(input("Enter a number over 5: "))

print("The last number you entered was a " + str(num1))
Python for loop | Python while loop
Python loops program