Worksheet 5 : Understanding and Applying the For Loop

 Worksheet 5: Understanding and Applying the For Loop



Section A: Basic Questions

  1. Print Numbers

    • Write a for loop to print numbers from 1 to 20.
  2. Sum of Even Numbers

    • Write a for loop to calculate the sum of all even numbers from 1 to 50.
  3. Display Characters

    • Write a for loop to print each character in the string "Programming".
  4. Reverse a String

    • Write a for loop to print the characters of the string "Python" in reverse order.

Section B: Intermediate Questions

  1. Factorial Calculation

    • Write a for loop to calculate the factorial of a number entered by the user.
  2. Multiplication Table

    • Write a for loop to print the multiplication table of 7 (from 1 to 10).
  3. Count Vowels in a String

    • Write a for loop to count the number of vowels in the string "education".
  4. Squares of Numbers

    • Write a for loop to print the square of each number from 1 to 10.

Section C: Advanced Questions

  1. Sum of Elements in a List

    • Given a list [12, 7, 19, 24, 5, 31], write a for loop to calculate the sum of all the elements.

  2. Find Maximum and Minimum

    • Write a for loop to find the maximum and minimum values in the list [15, 22, 9, 31, 8, 16].

  3. Print a Triangle Pattern

    • Write a nested for loop to print the following pattern:
      1
      1 2
      1 2 3
      1 2 3 4
      1 2 3 4 5

  4. Find Prime Numbers

    • Write a for loop to find all the prime numbers between 1 and 50.

Section D: Application-Based Questions

  1. Celsius to Fahrenheit Conversion

    • Write a for loop to convert a list of temperatures in Celsius [0, 10, 20, 30, 40] to Fahrenheit using the formula: F = (C * 9/5) + 32.

  2. Count Occurrences of a Character

    • Write a for loop to count how many times the character 'a' appears in the string "banana apple mango".

  3. Identify Palindrome Strings

    • Write a for loop to check if the string "level" is a palindrome (a string that reads the same backward as forward).


Instructions:

  • Complete each question by writing the appropriate Python code.
  • Test your code to ensure it works as expected.
  • For the advanced and application-based questions, try to optimize your code for clarity and efficiency.

Note:

  • This worksheet is designed to reinforce your understanding of the for loop, so make sure to solve the problems step by step and understand how the loop operates in each case.

Post a Comment

0 Comments