Iterative Programs (For, While, Nested Loop)
The next topic is the For Loop. The for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set, or string) or other iterable objects. This loop allows us to execute a block of code multiple times based on the length of the sequence or specified range. Below are 10 programs that demonstrate different uses of the For Loop in Python. For Loop Programs 1. Print Numbers from 1 to 10 Description: This program uses a for loop to print numbers from 1 to 10. # Program to print numbers from 1 to 10 for i in range(1, 11): print(i) 2. Print Even Numbers within a Range Description: Prints even numbers between 1 and 20 using a for loop with a step of 2. # Program to print even numbers from 1 to 20 for i in range(2, 21, 2): print(i) 3. Calculate the Sum of First N Natural Numbers Description: This program calculates the sum of the first n natural numbers. # Program to calculate the sum of first n natural numbers n = int(input("Enter a number: ")) sum_n = 0 for i in range(1, n + 1): sum_n += i print("Sum of first", n, "natural numbers is:", sum_n) 4. Display Multiplication Table Description: Displays the multiplication table of a number entered by the user. # Program to display the multiplication table num = int(input("Enter a number: ")) for i in range(1, 11): print(f"{num} x {i} = {num * i}") 5. Print Each Character of a String Description: Iterates over each character of a string and prints it on a new line. # Program to print each character of a string text = input("Enter a string: ") for char in text: print(char) 6. Find the Factorial of a Number Description: Computes the factorial of a number using a for loop. # Program to find the factorial of a number num = int(input("Enter a number: ")) factorial = 1 for i in range(1, num + 1): factorial *= i print("Factorial of", num, "is:", factorial) 7. Calculate the Sum of Elements in a List Description: This program calculates the sum of elements in a list using a for loop. # Program to calculate the sum of elements in a list numbers = [10, 20, 30, 40, 50] sum_numbers = 0 for num in numbers: sum_numbers += num print("Sum of list elements:", sum_numbers) 8. Display Only Positive Numbers from a List Description: Filters and displays only the positive numbers from a list of integers. # Program to display only positive numbers from a list numbers = [-10, 15, -30, 45, 0, 50] for num in numbers: if num > 0: print(num) 9. Print Fibonacci Series up to N Terms Description: Generates the Fibonacci series up to n terms. # Program to print Fibonacci series up to n terms n_terms = int(input("Enter the number of terms: ")) a, b = 0, 1 for i in range(n_terms): print(a, end=" ") a, b = b, a + b 10. Count Vowels in a String Description: Counts and displays the number of vowels in a given string. # Program to count vowels in a string text = input("Enter a string: ") vowels = "aeiouAEIOU" count = 0 for char in text: if char in vowels: count += 1 print("Number of vowels:", count) Explanation Each For Loop program showcases different ways to use for loops to iterate over numbers, lists, and strings. The for loop structure is versatile and allows us to perform various operations based on the elements or indexes of the sequence, which makes it essential for performing repeated tasks. The next topic is the While Loop. The while loop in Python repeatedly executes a block of code as long as the condition specified is True. This loop is useful when the number of iterations is not known beforehand, and we want the loop to continue until a certain condition is met. Below are 10 programs that demonstrate different uses of the While Loop in Python. While Loop Programs 1. Print Numbers from 1 to 10 Description: This program uses a while loop to print numbers from 1 to 10. # Program to print numbers from 1 to 10 using while loop i = 1 while i <= 10: print(i) i += 1 2. Sum of Natural Numbers Description: Calculates the sum of first n natural numbers using a while loop. # Program to calculate the sum of first n natural numbers using while loop n = int(input("Enter a number: ")) sum_n = 0 i = 1 while i <= n: sum_n += i i += 1 print("Sum of first", n, "natural numbers is:", sum_n) 3. Print Even Numbers Between 1 and 20 Description: Prints even numbers between 1 and 20 using a while loop. # Program to print even numbers from 1 to 20 using while loop i = 2 while i <= 20: print(i) i += 2 4. Reverse a Number Description: Reverses a number entered by the user using a while loop. # Program to reverse a number using while loop num = int(input("Enter a number: ")) reverse = 0 while num > 0: digit = num % 10 reverse = reverse * 10 + digit num = num // 10 print("Reversed number:", reverse) 5. Count Down from 10 to 1 Description: A countdown from 10 to 1 using a while loop. # Program to count down from 10 to 1 using while loop i = 10 while i > 0: print(i) i -= 1 6. Print Multiplication Table of a Number Description: Prints the multiplication table of a number entered by the user. # Program to print multiplication table using while loop num = int(input("Enter a number: ")) i = 1 while i <= 10: print(f"{num} x {i} = {num * i}") i += 1 7. Check for Prime Number Description: This program checks whether a number is prime using a while loop. # Program to check if a number is prime using while
Iterative Programs (For, While, Nested Loop) Read More »