Python Practice Questions

BASIC PROGRAMS

  1. Program to check whether the given number is positive, negative or zero.
  2. Program to find largest number between two numbers.
  3. Program to find largest number between three numbers.
  4. Program to find second largest number between three numbers.
  5. Program to print wishes according to the time given in hours.
  6. Program to check whether given alphapet is vowel or consonant.
  7. Program to check whether given character is alphabet, digit or symbol.
  8. Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:
    Percentage >= 90% : Grade A
    Percentage >= 80% : Grade B
    Percentage >= 70% : Grade C
    Percentage >= 60% : Grade D
    Percentage >= 40% : Grade E
    Percentage < 40% : Grade F
  9. Program to read a single digit number and print in words.
  10. Program to display number of days in a given month.
  11. Program to print natural numbers from 1 to N.
  12. Program to print natural numbers from N to 1.
  13. Program to find sum of N natural numbers.
  14. Program to display multiplication table of a given number.
  15. Program to count number of digits of a given number.
  16. Program to find sum of digits of a given number.
  17. Program to find reverse of a given number.
  18. Program to check whether given number is palindrome or not.
  19. Program to find factorial of given number.
  20. Program to check whether a number is prime or not.

PROGRAMS ON LIST

  1. Program to move first element to last in list.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print resultant list of elements in single-line with space separation.
    Sample Input:
    5
    10
    20
    30
    40
    50
    Sample Output:
    20 30 40 50 10
  2. Program to interchange first and last elements in list.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print resultant list of elements in single-line with space separation.
    Sample Input:
    5
    10
    20
    30
    40
    50
    Sample Output:
    50 20 30 40 10
  3. Program to split the list into two half's and add first part at the end.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print resultant list of elements in single-line with space separation.
    Sample Input:
    5
    10
    20
    30
    40
    50
    Sample Output:
    30 40 50 10 20
  4. Program to find second largest and second smallest number from the list.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print two integers in single-line with space separation: second largest and second smallest.
    Sample Input:
    5
    30
    10
    40
    20
    50
    Sample Output:
    40 20
  5. Program to find difference between sum of even numbers and sum of odd numbers.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print an integer represents difference between sum of even numbers and sum of odd numbers.
    Sample Input:
    5
    30
    10
    40
    20
    50
    Sample Output:
    30
  6. Program to move all zero's to the end of list.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print resultant list of elements in single-line with space separation.
    Sample Input:
    6
    0
    10
    0
    20
    0
    30
    Sample Output:
    10 20 30 0 0 0
  7. Program that calculate the fractions of its elements that are positive, negative and zeros.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Output Format:
    Print fractions of positive, negative and zero in three different lines.
    Sample Input:
    8
    -3
    -2
    -1
    0
    0
    1
    2
    3
    Sample Output:
    0.375
    0.375
    0.25
    Explanation:
    Number of positive elements / total elements = 3/8 = 0.375
    Number of negative elements / total elements = 3/8 = 0.375
    Number of zero elements / total elements = 2/8 = 0.25
  8. Program to insert an element at given position in list.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Next line of input consists of an integer pos, represents index position
    Next line of input consists of an integer newele, represents new elements to be insert.
    Output Format:
    Print resultant list of elements with space separataion in single line.
    Sample Input:
    5
    10
    20
    30
    40
    50
    2
    25
    Sample Output:
    10 20 25 30 40 50
    Explanation:
    n = 5
    li = [10,20,30,40,50]
    pos = 2
    newelement = 25
    New list after insertion is [10,20,25,30,40,50]
  9. Program to delete an element from a list at specified position.
    Input Format:
    First line of input consists an integer N, represents number of elements for list.
    Next N-lines follows an each one with integer store in list.
    Next line of input consists of an integer pos, represents index position
    Output Format:
    Print deleted element at given position or print -1 if given position is out of range.
    Sample Input1:
    5
    10
    20
    30
    40
    50
    2
    Sample Output1:
    30
    Explanation1:
    n = 5
    li = [10,20,30,40,50]
    Position to be delete 2
    30 is element at position 2.
    Sample Input2:
    5
    10
    20
    30
    40
    50
    7
    Sample Output2:
    -1
    Explanation2:
    n = 5
    li = [10,20,30,40,50]
    Position to be delete 7
    Position 7 is out of range, so print -1
  10. Program to reverse of list elements without using list built-in methods.
    Sample Input:
    5
    10
    20
    30
    40
    50
    Sample Output:
    50 40 30 20 10
  11. Program to remove duplicates in list.
    Sample Input:
    6
    1
    2
    3
    2
    4
    2
    Sample Output:
    1 2 3 4
  12. Program to count the frequency of each element of list.
    Sample Input:
    6
    1
    2
    3
    2
    4
    2
    Sample Output:
    1 1
    2 3
    3 1
    4 1
  13. Program to sort the elements of list in descending order.
    Sample Input:
    5
    3
    4
    2
    5
    1
    Sample Output:
    1 2 3 4 5
    Explanation:
    Arrange the list elements in descending order.
  14. Program to find second smallest and second largest numbers from list.
    Sample Input:
    8
    4
    6
    1
    5
    2
    7
    3
    8
    Sample Output:
    2 7
  15. Program to find duplicate element in k-distance. If found print "YES" otherwise "NO".
    Sample Input1:
    8
    1
    2
    3
    4
    1
    2
    3
    4
    3
    Sample Output1:
    NO
    Explanation:
    Each element in the given array li[] appears twice and the distance between every element and its duplicate is 4.
    Sample Input1:
    6
    1
    2
    3
    2
    4
    5
    3
    Sample Output1:
    YES
    Explanation:
    2 is present at index 1 and 3.

PROGRAMS ON STRINGS

  1. Program to count the number alphabets, digits and symbols in a String.
    Sample Input: 8+8+8 is daily time management strategy
    Sample Output: 29 3 7
    Explanation: In the given String, Number of alphabets are 29, digits are 3 and symbols are 7.
  2. Program to swap cases in the given String.
    Sample Input: Hello Govardhan1206
    Sample Output: hELLO gOVARDHAN1206
    Explanation: In the given String, if a character is in uppercase convert it into lowercase and vice-versa.
  3. Program to find the length of the longest word in a string.
    Sample Input: Hello Govardhan How are you?
    Sample Output: 9
    Explanation: In the given String, substring Govardhan is longest string its length is 9.
  4. Program to find duplicate words in a string.
    Sample Input:
    where there is a will there is a way
    Sample Output:
    there
    is
    a
    Explanation: Duplicate words in the specified string: there, is and a
  5. Program to find number of words having non-repeating characters.
    Sample Input:
    where there is a will there is a way
    Sample Output:
    5