Loop practice questions in Python - Level 1

WAP stands for Write A Program

  1. WAP to print the first and last character of all the names in the given list
    l = ['Mayank', 'Aniket', 'Saurabh', 'Rahul']
    Expected output:
    M k
    A t
    S h
    R l
    ………………………………
  2. Write a Python program that takes two inputs from the user:
    An integer n representing the number of whole numbers (starting from 0).
    An integer p representing the power to which each whole number should be raised.
    The program should print the powers of the first n whole numbers.

    Example Input:
    Enter the value of n: 5
    Enter the value of p: 2
    Example Output:
    0^2 = 0
    1^2 = 1
    2^2 = 4
    3^2 = 9
    4^2 = 16
    5^2 = 25
    ………………………………

  3. WAP to print the data type of each element in the given tuple.
    t = ('Someone', 34, 'hi', 12.983, 34 +9j)

    Expected Output:
    Someone, Type: <class ‘str’>
    34, Type: <class ‘int’>
    hi, Type: <class ‘str’>
    12.983, Type: <class ‘float’>
    (34+9j), Type: <class ‘complex’>
    ………………………………
  4. WAP to print all the two digit even numbers which are also a multiple of 3.
    Expected Output:

    12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
    ………………………………
  5. WAP to print the sum of individual tuples separately.
    marksList = [(23, 24, 31), (56, 34, 53), (42, 58, 31)]
    Expected Output:
    Sum of (23, 24, 31) = 78
    Sum of (56, 34, 53) = 143
    Sum of (42, 58, 31) = 131
    ………………………………
  6. Write a Python program to print all numbers between 30 and 300 that are multiples of both 2 and 7.
    Expected output:
    42 56 70 84 98 112 126 140 154 168 182 196 210 224 238 252 266 280 294
    ………………………………
  7. t = (23, 45, 67, 90, 12, 10)
    WAP to print even or odd for all the numbers in the given tuple.
    Expected Output:
    23 – Odd
    45 – Odd
    67 – Odd
    90 – Even
    12 – Even
    10 – Even
    ………………………………
  8. Write a Python program that takes a string input from the user and reverses it.
    Note: Don’t use slicing
     Example Input: Enter a string: Python
    Expected Output: Reversed string: nohtyP
    ………………………………
  9. t = (23, 45, 67, 90, 12, 10)
    WAP to print the 2nd digit of all the numbers from this given collection.
    Note: If a number has only one digit, print "No second digit".
    Expected Output:
    23 → 3
    45 → 5
    67 → 7
    90 → 0
    12 → 2
    10 → 0
    ………………………………
  10. WAP to print all the numerical data from the given list.
    l = [2, 5, 'a', 5, 'r', 7, 'm', 't', '4']
    Expected Output:
    2
    5
    5
    7
    4
    ………………………………
  11. WAP in Python to calculate the sum of all numbers from 1 to n.
    Take n as input from the user.
    Example Input: Enter the value of n: 5
    Example Output: Sum of numbers from 1 to 5 = 15
    ………………………………
  12. Write a Python program that takes a number as input from the user and counts the total number of digits in the number.
    Example Input: Enter a number: 78645
    Example Output: Total number of digits: 5
    ………………………………
  13. Write a Python program that takes a number as input from the user and calculates its factorial.
    (Factorial of n is the product of all positive integers from 1 to n, denoted as n!.)
    Example Input: Enter a number: 5
    Example Output: Factorial of 5 is 120
    ………………………………

  14. Print the following pattern using a loop.
    Take the number of rows as input from the user.
    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1
    Example Input 1: Enter the number of rows: 5
    Example Output 1: 
    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1
    Example Input 2: Enter the number of rows: 3
    Example Output 2: 
    3 2 1
    2 1
    1