Python Programming Practice-For loops

Created with Sketch.

Python Programming Practice: For Loops

Introduction

For loops are a fundamental control flow structure in Python, allowing you to iterate over sequences such as lists, tuples, strings, or other iterable objects. In this blog post, we will explore several Python programming exercises involving for loops. Each exercise comes with a description, Python code solution, and output, providing hands-on practice to enhance your understanding of for loops.

Exercise 1: Print Numbers in a Given Range

Description: Write a Python program using a for loop to print all the numbers between 1 and a user-defined upper limit.

Solution:

def print_numbers_in_range(upper_limit):
    for num in range(1, upper_limit + 1):
        print(num, end=' ')

# Example Usage
upper_limit = int(input("Enter the upper limit: "))
print_numbers_in_range(upper_limit)

Output:

 
Enter the upper limit: 8
1 2 3 4 5 6 7 8

Exercise 2: Calculate the Sum of Numbers in a List

Description: Write a Python program to calculate the sum of all numbers in a given list.

Solution:

def calculate_sum(numbers):
    total_sum = 0
    for num in numbers:
        total_sum += num
    return total_sum

# Example Usage
numbers_list = [2, 4, 6, 8, 10]
result = calculate_sum(numbers_list)
print(f"The sum of numbers in the list is: {result}")

Output:

 
The sum of numbers in the list is: 30

Exercise 3: Print Multiplication Table

Description: Write a Python program to print the multiplication table for a given number.

Solution:

def print_multiplication_table(number, limit):
    for i in range(1, limit + 1):
        print(f"{number} x {i} = {number * i}")

# Example Usage
num_to_generate_table_for = int(input("Enter the number: "))
table_limit = int(input("Enter the limit: "))
print_multiplication_table(num_to_generate_table_for, table_limit)

Output:

 
Enter the number: 5
Enter the limit: 8
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40

Exercise 4: Identify Even and Odd Numbers

Description: Write a Python program to categorize numbers in a list as even or odd.

Solution:

def categorize_numbers(numbers):
    even_numbers = []
    odd_numbers = []
    for num in numbers:
        if num % 2 == 0:
            even_numbers.append(num)
        else:
            odd_numbers.append(num)
    return even_numbers, odd_numbers

# Example Usage
numbers_to_categorize = [1, 2, 3, 4, 5, 6, 7, 8, 9]
evens, odds = categorize_numbers(numbers_to_categorize)
print(f"Even numbers: {evens}")
print(f"Odd numbers: {odds}")

Output:

 
Even numbers: [2, 4, 6, 8]
Odd numbers: [1, 3, 5, 7, 9]

Exercise 5: Find the Factorial of a Number

Description: Write a Python program to calculate the factorial of a given number.

Solution:

def calculate_factorial(number):
    factorial = 1
    for i in range(1, number + 1):
        factorial *= i
    return factorial

# Example Usage
num_to_calculate_factorial = int(input("Enter the number: "))
result_factorial = calculate_factorial(num_to_calculate_factorial)
print(f"The factorial of {num_to_calculate_factorial} is: {result_factorial}")

Output:

 
Enter the number: 5
The factorial of 5 is: 120

Conclusion

These Python programming exercises provide valuable practice for working with for loops and handling different scenarios. By completing these exercises, you enhance your ability to use for loops for iteration, calculations, and categorization. Continue exploring and practicing with for loops to build a solid foundation in Python programming. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *