Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

give me 100 problem solving questions about for loop in python with expected answers

  1. User

    give me 100 problem solving questions about for loop in python with expected answers

  2. Assistant

    Sure! Here are 100 problem-solving questions about for loops in Python along with their expected answers:

    1. Write a for loop to print all the numbers from 1 to 10. Expected answer: 1 2 3 4 5 6 7 8 9 10

    2. Write a for loop to find the sum of all the even numbers between 1 and 20. Expected answer: 110 (2+4+6+8+10+12+14+16+18+20)

    3. Write a for loop to calculate the factorial of a given number. Expected answer: (for input 5) 120 (54321)

    4. Write a for loop to print all the characters of a given string. Expected answer: (for input "Hello") H e l l o

    5. Write a for loop to count the number of vowels in a given string. Expected answer: (for input "Hello") 2

    6. Write a for loop to find the largest number in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 9

    7. Write a for loop to calculate the sum of all the elements in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 27

    8. Write a for loop to print the multiplication table of a given number. Expected answer: (for input 7) 7x1=7 7x2=14 7x3=21 ... 7x10=70

    9. Write a for loop to find the average of all the numbers in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 4.5

    10. Write a for loop to check if a number is prime. Expected answer: (for input 7) True

    11. Write a for loop to reverse a given string. Expected answer: (for input "Hello") olleH

    12. Write a for loop to print the square of all the numbers from 1 to 5. Expected answer: 1 4 9 16 25

    13. Write a for loop to find the second largest number in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 7

    14. Write a for loop to remove all the duplicates from a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2, 5, 7]) [1, 5, 7, 3, 9, 2]

    15. Write a for loop to check if a given string is a palindrome. Expected answer: (for input "madam") True

    16. Write a for loop to find the sum of the squares of all the even numbers between 1 and 10. Expected answer: 220 (4+16+36+64+100)

    17. Write a for loop to find the minimum number in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 1

    18. Write a for loop to print the Fibonacci series up to a given number. Expected answer: (for input 10) 0 1 1 2 3 5 8

    19. Write a for loop to count the number of words in a given sentence. Expected answer: (for input "Hello, how are you?") 4

    20. Write a for loop to check if a given list is sorted in ascending order. Expected answer: (for input [1, 2, 3, 4, 5]) True

    21. Write a for loop to find the product of all the elements in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 1890

    22. Write a for loop to print all the uppercase letters. Expected answer: A B C D ... X Y Z

    23. Write a for loop to find the length of the hypotenuse of a right-angled triangle given the lengths of the other two sides. Expected answer: (for input 3 and 4) 5

    24. Write a for loop to convert all the elements in a given list to uppercase. Expected answer: (for input ["hello", "world"]) ["HELLO", "WORLD"]

    25. Write a for loop to find the average of all the even numbers between 1 and 50. Expected answer: 25

    26. Write a for loop to check if a given list is a palindrome. Expected answer: (for input [1, 2, 3, 2, 1]) True

    27. Write a for loop to print the first N even numbers. Expected answer: (for input 5) 2 4 6 8 10

    28. Write a for loop to print all the prime numbers between 1 and 100. Expected answer: 2 3 5 7 11 13 ... 97

    29. Write a for loop to find the third largest number in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 5

    30. Write a for loop to remove all the vowels from a given string. Expected answer: (for input "Hello") H ll

    31. Write a for loop to check if a given list contains a duplicate element. Expected answer: (for input [1, 5, 7, 3, 9, 2, 5]) True

    32. Write a for loop to print all the prime numbers between 1 and N. Expected answer: (for input 20) 2 3 5 7 11 13 17 19

    33. Write a for loop to find the sum of the cubes of all the odd numbers between 1 and 10. Expected answer: 728 (1+27+125+343+729)

    34. Write a for loop to find the number of times a given element appears in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2, 5]) 2

    35. Write a for loop to check if a given number is a perfect square. Expected answer: (for input 25) True

    36. Write a for loop to print the following pattern: * **




    Expected answer: (nested for loop) * **




    1. Write a for loop to print the multiplication table up to a given number. Expected answer: (for input 5) 1x1=1 1x2=2 1x3=3 ... 5x5=25

    2. Write a for loop to find the median of a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 4.0

    3. Write a for loop to print the digits of a given number. Expected answer: (for input 12345) 1 2 3 4 5

    4. Write a for loop to find the sum of the digits of a given number. Expected answer: (for input 12345) 15

    5. Write a for loop to check if a given list is sorted in descending order. Expected answer: (for input [5, 4, 3, 2, 1]) True

    6. Write a for loop to check if a given number is a palindrome. Expected answer: (for input 12321) True

    7. Write a for loop to find the second smallest number in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) 2

    8. Write a for loop to remove all the spaces from a given string. Expected answer: (for input "Hello, how are you?") Hello,howareyou?

    9. Write a for loop to check if a given list is in strictly increasing order. Expected answer: (for input [1, 2, 3, 4, 5]) True

    10. Write a for loop to find the sum of all the odd numbers between 1 and 50. Expected answer: 625

    11. Write a for loop to find the number of lowercase letters in a given string. Expected answer: (for input "Hello, how are you?") 12

    12. Write a for loop to reverse the order of elements in a given list. Expected answer: (for input [1, 5, 7, 3, 9, 2]) [2, 9, 3, 7, 5, 1]

    13. Write a for loop to print the following pattern:




      ** * Expected answer: (nested for loop)




    ** *

    1. Write a for loop to find the factorial of all the numbers from 1 to 10. Expected answer: 1 2 6 24 120 720 5040 40320 362880 3628800

    2. Write a for loop to find the sum of the digits of all the numbers from 1 to 50. Expected answer: 289

    3. Write a for loop to check if a given list contains an odd number. Expected answer: (for input [1, 5, 7, 3, 9, 2, 6]) True

    4. Write a for loop to print the first N prime numbers. Expected answer: (for input 5) 2 3 5 7 11

    5. Write a for loop to find the common elements in two given lists. Expected answer: (for input [1, 2, 3, 4] and [3, 4, 5, 6]) [3, 4]

    6. Write a for loop to find the number of digits in a given number. Expected answer: (for input 12345) 5

    7. Write a for loop to check if a given string is an anagram of another given string. Expected answer: (for input "listen" and "silent") True

    8. Write a for loop to find the sum of all the elements on the diagonals of a square matrix. Expected answer: (for input [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 25

    9. Write a for loop to find the maximum number in each row of a given matrix. Expected answer: (for input [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [3, 6, 9]

    10. Write a for loop to find the length of the longest word in a given sentence. Expected answer: (for input "Hello, how are you?") 5

    11. Write a for loop to remove all the special characters from a given string. Expected answer: (for input "^#@Hello, how are you?%^#") Hello, how are you?

    12. Write a for loop to check if a given number is a perfect cube. Expected answer: (for input 8) False

    13. Write a for loop to find the sum of all the elements in each column of a given matrix. Expected answer: (for input [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [12, 15, 18]

    14. Write a for loop to find the maximum number in each column of a given matrix. Expected answer: (for input [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [7, 8, 9]

    15. Write a for loop to check if a given number is a power of 2. Expected answer: (for input 16) True

    16. Write a for loop to find the sum of products of corresponding elements of two given lists. Expected answer: (for input [1, 2, 3] and [4, 5, 6]) 32

    17. Write a for loop to check if a given number is an Armstrong number. Expected answer: (for input 153) True

    18. Write a for loop to find the common elements in N given lists. Expected answer: (for input [1, 2, 3, 4], [3, 4, 5, 6], and [2, 3, 4, 5]) [3, 4]

    19. Write a for loop to check if a given string is a pangram. Expected answer: (for input "The quick brown fox jumps over the lazy dog") True

    20. Write a for loop to find all the prime factors of a given number. Expected answer: (for input 24) [2, 2, 2, 3]

    21. Write a for loop to print the following pattern:






      ** * Expected answer: (nested for loop)






    ** *

    1. Write a for loop to find the number of perfect squares between two given numbers. Expected answer: (for input 1 and 10) 3

    2. Write a for loop to check if a given list is a subset of another given list. Expected answer: (for input [2, 4, 6] and [1, 2, 3, 4, 5, 6]) True

    3. Write a for loop to find the sum of all the elements in each row of a given matrix. Expected answer: (for input [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [6, 15, 24]

    4. Write a for loop to rotate the elements of a given list by N positions. Expected answer: (for input [1, 2, 3, 4, 5] and N=2) [4, 5, 1, 2, 3]

    5. Write a for loop to check if a given number is a perfect number. Expected answer: (for input 28) True

    6. Write a for loop to find the common elements in N given lists. Expected answer: (for input [1, 2, 3, 4], [3, 4, 5, 6], and [2, 3, 4, 5]) [3, 4]

    7. Write a for loop to find the sum of digits of a factorial of a given number. Expected answer: (for input 5) 3

    8. Write a for loop to check if a given number is a strong number. Expected answer: (for input 145) True

    9. Write a for loop to find the number of trailing zeros in the factorial of a given number. Expected answer: (for input 10) 2

    10. Write a for loop to check if a given number is a narcissistic number. Expected answer: (for input 153) True

    11. Write a for loop to find all the prime numbers between two given numbers. Expected answer: (for input 1 and 10) [2, 3, 5, 7]

    12. Write a for loop to find the sum of all the prime numbers between two given numbers. Expected answer: (for input 1 and 10) 17

    13. Write a for loop to check if a given list is a superset of another given list. Expected answer: (for input [1, 2, 3, 4, 5, 6] and [2, 4, 6]) True

    14. Write a for loop to find the sum of the squares of all the prime numbers between 1 and 10. Expected answer: 91 (4+9+25+49)

    15. Write a for loop to print the ASCII value of all the characters in a given string. Expected answer: (for input "Hello") 72 101 108 108 111

    16. Write a for loop to check if a given number is a Smith number. Expected answer: (for input 22) True

    17. Write a for loop to find the sum of digits of a given number until the result becomes a single-digit number. Expected answer: (for input 987) 3

    18. Write a for loop to find the least common multiple (LCM) of two given numbers. Expected answer: (for input 4 and 6) 12

    19. Write a for loop to check if a given list is a union of two given lists. Expected answer: (for input [1, 2, 3, 4, 5, 6] and [2, 4, 6]) True

    20. Write a for loop to find the GCD (Greatest Common Divisor) of two given numbers. Expected answer: (for input 36 and 48) 12

    21. Write a for loop to check if a given number is a Hamming number. Expected answer: (for input 16) True

    22. Write a for loop to find all the Amicable pairs within a given range. Expected answer: (for input 1 and 100) [(220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), (66928, 66992), (67095, 71145), (69615, 87633), (79750, 88730)]

    23. Write a for loop to find all the Pythagorean triplets within a given range. Expected answer: (for input 1 and 30) [(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (10, 24, 26), (12, 16, 20), (15, 20, 25), (20, 21, 29)]

    24. Write a for loop to find the sum of digits of a given number until the result becomes a perfect number. Expected answer: (for input 47) 28

    25. Write a for loop to find all the abundant numbers within a given range. Expected answer: (for input 1 and 100) [12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60