For, While List (QP)
For, While List (QP)
Notes
For loop in python: For loops in Python iterate over a sequence (such as a list, tuple, string, or range)
and execute a block of code multiple times. They're beneficial for performing repetitive tasks efficiently
and can handle numerical and non-numerical data.
Example :
for i in range(5,10):
print i
output:
Task
Q2. Python program to print all the even numbers within a given range
Q3. Python program to calculate sum of all numbers from 1 to a given number
Q4. Python program to calculate sum of all odd numbers within the given range
Q6. Python program to display all numbers from a list using a for loop
Q8. Write a Python program that calculates the sum of all elements in a given list.
Example: Given the list [1, 2, 3, 4, 5], the program should output 15
Q9. Write a Python program that finds the largest number in a given list
Example: Given the list [10, 20, 4, 45, 99], the program should output 99
Q10. Write a Python program that counts how many times a specific element appears in a list.
Example: Given the list [1, 2, 2, 3, 4, 4, 4, 5] and the target number 4, the program should
output 3
Q11. Write a Python program that prints all the even numbers from a given list
Q12. Write a Python program that creates a new list containing the squares of all elements in a
given list.
Example: Given the list [1, 2, 3, 4], the program should output [1, 4, 9, 16]
Q13. Write a Python program that creates a new list containing only the positive numbers from a
given list.
Example: Given the list [-1, 2, -3, 4, 5, -6], the program should output [2, 4, 5]
Q14. Write a Python program that concatenates all the strings in a given list into a single string.
Example: Given the list ["hello", "world", "python"], the program should output
"helloworldpython"
Q15. Write a Python program that calculates the product of all numbers in a list
Example: Given the list [2, 3, 4], the program should output 24
Q16. Write a Python program that creates a new list containing the lengths of each string in a given list.
Example: Given the list ["apple", "banana", "cherry"], the program should output [5, 6, 6].
Q17. Write a Python program using a while loop to find the sum of all the numbers in a list.
Example: Given the list [1, 2, 3, 4, 5], the program should output 15
Q18. Write a Python program using a while loop to count how many times a specific number
appears in a list. Example: Given the list [1, 2, 3, 4, 2, 2, 5] and the number 2, the
program should output 3
Q19. Write a Python program using a while loop to reverse a list. Example: Given the list [1,
2, 3, 4, 5], the program should output [5, 4, 3, 2, 1]
Q20. Write a Python program using a while loop to find the smallest number in a list. Example:
Given the list [10, 20, 4, 45, 99], the program should output 4
Q21. Write a Python program using a while loop to find the greatest number in a list. Example:
Given the list [10, 20, 4, 45, 99], the program should output 99
Q22. Write a Python program with a list consisting of both positive and negative numbers.
Ensure that the smallest number is found correctly
Q23. Write a Python program that counts how many times a specific element (given by the user)
occurs in a list using a while loop.
Ask the user whether they need to change any element, if the user enter “Y” – then ask for the
index and element to be inserted and change the elements in the list accordingly
Repeat it until the user enters “N” and print the new list