Worksheet 5: Understanding and Applying the For
Loop
Section A: Basic Questions
Print Numbers
- Write a
for
loop to print numbers from 1 to 20.
- Write a
Sum of Even Numbers
- Write a
for
loop to calculate the sum of all even numbers from 1 to 50.
- Write a
Display Characters
- Write a
for
loop to print each character in the string"Programming"
.
- Write a
Reverse a String
- Write a
for
loop to print the characters of the string"Python"
in reverse order.
- Write a
Section B: Intermediate Questions
Factorial Calculation
- Write a
for
loop to calculate the factorial of a number entered by the user.
- Write a
Multiplication Table
- Write a
for
loop to print the multiplication table of 7 (from 1 to 10).
- Write a
Count Vowels in a String
- Write a
for
loop to count the number of vowels in the string"education"
.
- Write a
Squares of Numbers
- Write a
for
loop to print the square of each number from 1 to 10.
- Write a
Section C: Advanced Questions
Sum of Elements in a List
- Given a list
[12, 7, 19, 24, 5, 31]
, write afor
loop to calculate the sum of all the elements.
- Given a list
Find Maximum and Minimum
- Write a
for
loop to find the maximum and minimum values in the list[15, 22, 9, 31, 8, 16]
.
- Write a
Print a Triangle Pattern
- Write a nested
for
loop to print the following pattern:11 21 2 31 2 3 41 2 3 4 5
Find Prime Numbers
- Write a
for
loop to find all the prime numbers between 1 and 50.
- Write a
Section D: Application-Based Questions
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
.
- Write a
Count Occurrences of a Character
- Write a
for
loop to count how many times the character'a'
appears in the string"banana apple mango"
.
- Write a
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).
- Write a
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.
0 Comments
Please do note create link post in comment section