Python Practice Questions - For Loops, While Loops & Functions
1. Print All Odd Numbers from 1 to 50:
Use a for loop to print only odd numbers between 1 and 50.
2. Print Each Character in a String:
Ask the user to input a string and print each character using a for loop.
3. Sum of Even Numbers from 1 to 100:
Use a for loop to calculate and print the sum of all even numbers between 1 and 100.
4. Print Multiples of 3 from 1 to 30:
Use a for loop to print all multiples of 3 between 1 and 30.
5. Iterate Over a List of Names:
Given a list of names, print a welcome message for each name using a loop.
6. Count Vowels in a String:
Ask the user for a string and count how many vowels it has using a for loop.
7. Add Numbers Until User Enters 0:
Keep asking the user to enter numbers and sum them until they enter 0. Then print the total sum.
8. Password Retry Loop:
Ask the user to enter a password. Keep asking until they type the correct password (e.g.,
'code123').
9. Print Numbers in Reverse from User Input:
Ask the user for a number n and print numbers from n to 1 using a while loop.
10. Find the First Multiple of 7 Greater Than 50:
Use a while loop to find and print the first multiple of 7 greater than 50.
11. Square Root Guessing (Fixed Iteration):
Guess the square root of a number using trial and error for numbers 1 to n using while.
12. Count Digits in a Number:
Use a while loop to count how many digits are in a number entered by the user.
13. Function to Return the Square of a Number:
Write a function square(num) that returns the square of a number.
14. Function to Check if a Number is Prime:
Write a function is_prime(n) that checks whether a number is prime.
15. Function to Count Words in a Sentence:
Write a function that takes a string and returns how many words it contains.
16. Function to Convert Celsius to Fahrenheit:
Write a function to_fahrenheit(c) that converts Celsius to Fahrenheit.
17. Function to Find Sum of a List:
Write a function that takes a list of numbers and returns their sum.
18. Function to Print a Triangle Pattern:
Write a function that takes a number n and prints a triangle pattern of * of height n.