Python Practice Problems

Section 1: Python Basics MCQ

1) Who is the author of python programming language?

  A. Monte Python
  B. Guido Van Rossum
  C. Larry Wall
  D. David H. Hansson

2) Which is not a features of python programming?

  A. ObjectOriented
  B. Interactive
  C. Interpreted
  D. Statically typed

3) Which function is used to retrieve the data type of a variable or object?

  A. type()
  B. dtype()
  C. dir()
  D. help()

4) Which function is used to list attributes of a given object?

  A. type()
  B. dtype()
  C. dir()
  D. help()

 5) Which is not a keyword in python3?

  A. def
  B. while
  C. print
  D. elif  

6) Which keyword is used to get other modules in python code?

 A. import
 B. include
 C. inherit
 D. install  

7) Which of the following will be an invalid python variable names?

A. abc123
B. abc_123
C. _abc123
D. 123abc

8) Which is not the correct data declaration in python?

A. x = -19.8
B. x =  2+3j
C. x, y = 1, 2
D. x, y, z = 1

9) Which of them will be False?

A. 'hello' == "hello"
B.  'a' == 'A'
C.  5 in [1, 2, 3, 4, 5]
D. 'a' not in "AEIOU"

10) Which of the following statement is incorrect?

A. var = "hello";
B. var = "hello" + "world"
C. var = 'I can't learn Python'
D. a = 1; b = 2     

Section 2: Basic Input Output, Variables, Operators And Numbers

11.  Write a program to add two given numbers of int, float or complex types.

>>> practiceQuestion(2, 4)
    6
>>> practiceQuestion(-2, 1.5)
    -0.5
>>> practiceQuestion(1+2j, 3+4j)
    (4+6j)

12.  Write a program to find the area of a square with a given side length.

>>> practiceQuestion(30)
    900

13.  Write a program to find the BMI (Body Mass Index) for a given weight (in kg) and a given height (in meter).

Formula: BMI is weight in kg divided by square height in meter.

>>> practiceQuestion(67, 1.67)
    24.02       

14.  Write a program to convert a given temperature from Celsius To Fahrenheit scale.

Formula: Multiply Celsius by 9, then divide by 5, then add 32 to find the value in Fahrenheit

>>> practiceQuestio(28.0)
    82.40    

15.  Write a program to find the hypotenuse of a right-angled triangle, using Pythagoras Theorem.

Formula: Hypotenuse is the square root of square base plus square height.

>>> practiceQuestion(3, 4)
    5.0    

16.  Write a program to find the area of a semicircle using a given radius value.

Formula: Area of circle is pie time the square of radius.

>>> practiceQuestion(3)
    14.14    

17.  Write a program to find the maximum, minimum and sum of a given list of numbers.

>>> practiceQuestion([1, 6, 3, 4, 5])
    (6, 1, 19)    

18.  Write a program to the ASCII order of a given character.

>>> practiceQuestion('A')
    65
>>> practiceQuestion('a')
    97  

19.  Write a program to find the discriminant of a Quadratic Equation of the form ax^2+bx+c

Formula: Discriminant is b**2-4ac

>>> practiceQuestion(1, 3, 2) # in the order of (a, b, c)
    1
>>> practiceQuestion(1, 4, 4)
    0
>>> practiceQuestion(1, 2, 3)
    -8

20.  Write a program To find the distance between two given points in the (x,y) coordinate format.

Formula: Distance between points on a graph is the square root of, square difference of x co-ordinates, plus square difference of y co-ordinates.

>>> practiceQuestion(1, 1, 4, 5) # in the order x1, y1, x2, y2
    5.0

Section 3: Python Conditions

21.  WAP to check if a given number is both positive and even.

>>> practiceQuestion(20)
    True
>>> practiceQuestion(-2)
    False
>>> practiceQuestion(3)
    False 

22.  WAP to check if a given number is divisible by 7 Or 11.

>>> practiceQuestion(21)
    True
>>> practiceQuestion(22)
    True
>>> practiceQuestion(23)
    False  

23.  WAP to check if a given number can represent a leap year.

>>> practiceQuestion(120)
    True
>>> practiceQuestion(123)
    False
>>> practiceQuestion(200)
    False    

24.  WAP to check if a given number is Prime or not.

>>> practiceQuestion(17)
    True
>>> practiceQuestion(9)
    False
>>> practiceQuestion(-2)
    False    

25.  WAP to check if three given numbers can represent the sides of an equilateral triangle.

>>> practiceQuestion(2, 2, 2)
    True
>>> practiceQuestion(-3, -3, -3)
    False
>>> practiceQuestion(2, 3, 2)
    False    

26.  WAP to check if three given numbers can represent the sides of an isosceles triangle.

>>> practiceQuestion(2, 2, 1)
    False
>>> practiceQuestion(-3, 1, -3)
    False
>>> practiceQuestion(2, 3, 2)
    True    

27.  WAP to check if three given numbers can represent the sides of a scalene triangle.

>>> practiceQuestion(2, 2, 2)
    False
>>> practiceQuestion(3, 4, 5)
    True
>>> practiceQuestion(2, 3, 2)
    False 

28.  WAP to return "Fizz" if a given number is divisible by 3, "Buzz" if the same is divisible by 5, "FizzBuzz" if it is divisible by both 3 and 5, and print the same number if it is not divisible by either 3 or 5.

>>> practiceQuestion(6)
    Fizz
>>> practiceQuestion(15)
    FizzBuzz
>>> practiceQuestion(7)
    7    

29.  For a Quadratic Equation in the Form of Ax2 + Bx + C, the Discriminant, D is B2-4ac. Write A Function that returns the following output depending on the discriminant.

D > 02 real roots

D = 01 real root

D < 02 complex roots

>>> practiceQuestion (1, 2, 3)
    'This equation has 2 complex roots'
>>> practiceQuestion(1, 3, 2)
    'This equation has 2 real roots'
>>> practiceQuestion(1, 4, 4)
    'This equation has 1 real root'    

30.  Write a function that can convert a given Military time To Regular time.

>>> practiceQuestion('1619')
    '4:19 pm'
>>> practiceQuestion('1200')
    '12:00 pm'
>>> practiceQuestion('1020')
    '10:20 am'

Section 4: Loops

31.  Write a program to filter in all the vowels from a given string.

>>> practiceQuestion('Apple')
    'Ae' 

32.  Write a program to filter out all the odd numbers from a given list of numbers.

>>> practiceQuestion([234,52,345,34,53])
    [234, 52, 34]    

33.  Write a program to print all the index for odd numbers in a given list of numbers.

>>> practiceQuestion([234,52,345,34,53])
    [2, 4]    

34.  Write a program to delete all the vowels from a given string.

>>> practiceQuestion("Apple")
    ppl    

35.  Write a program to check if a given string has at least one, same pair of vowels at consecutive indexes.

>>> practiceQuestion('school')
    True
>>> practiceQuestion('Apple')
    False    

36.  Write a program to filter in all the country names beginning with a vowel from the given list of countries.

>>> practiceQuestion(['India', 'Nepal', 'China', 'Egypt', 'Sri Lanka']
    ['India', 'Egypt']  

37.  Write a program to get the sum of first digits from a given list of numbers.

>>> practiceQuestion([1, 20, 300, 4005])
    10    

38.  Write a program to check if a given password has at least one letter and at least one digit with a minimum length of 6 characters.

>>> practiceQuestion('abc12ed')
    True
>>> practiceQuestion('abcd')
    False
>>> practiceQuestion('1234')
    False    

39.  Write a program to get the list of numbers between two given numbers. Include the boundary values as well.

>>> practiceQuestion(2, 7)
    [2, 3, 4, 5, 6, 7]    

40.  Write a program to print "Hello" until a key 'Y' is pressed as input.

>>> Enter choice: Y
    hello
>>> Enter choice: N    

Section 5: List

41.  Write a program to add a given value at the beginning of a given list.

>>> Enter list: [1, 2, 3]
>>> Enter value: 100
    [100, 1, 2, 3]

42.  Write a program to delete the first and the last values from a given list.

>>> practiceQuestion([])
    []
>>> practiceQuestion([1])
    []
>>> practiceQuestion([1, 2, 3, 4])
    [2, 3]    

43.  Write a program to add the first and the last values from a given list.

>>> practiceQuestion([])
    0
>>> practiceQuestion([17])
    17
>>> practiceQuestion([1, 2, 3, 4])
    5    

44.  Write a program to delete all the odd numbers from a list of numbers.

>>> practiceQuestion([2, 45, 56, 67, 69, 12, 13, 15])
    [2, 56, 12]    

45.  Write a program to get the common values from two given lists. Publish the output list in sorted order.

>>> practiceQuestion([1, 2, 7, 3], [4, 3, 7])
    [3, 7]    

46.  Write a program to join the list of words as a sentence shown below. Publish the sentence in capitalize format.

>>> practiceQuestion(['i', 'SOLVED', 'PYthon', 'practice', 'QUEStions'])
    I solved python practice questions    

47.  Write a program to sort and reverse a given list.

>>> practiceQuestion([34, 12, 45])
    [45, 34, 12]    

48.  Write a program to get the sum of a list of numbers.

>>> Enter a list: [1, 2, 3, 4, 5]
    15

49.  Write a program to get the maximum and minimum values from a list of numbers.

>>> practiceQuestion(Enter a list: [32,52,35,234,5,2345,234,5234,2])
    5234 2    

50.  Write a program to find the uncommon values from two given lists.

>>> practiceQuestion([1, 2, 3, 4], [3, 4, 6, 5])
    [1, 2, 5, 6]    

Section 6: Dictionary

51.  Write a program to convert dna sample made with A, T, C, G into rna sample made with U, A, G, C respectively.

>>> practiceQuestion("ATTCG")
     UAAGC

52.  Write a program to find the count of each vowel in a given word.

>>> practiceQuestion("Australia")
    {'a':3, 'e':0, 'i':1, 'o':0, 'u':1}

53.  Write a program to compress the zero values in a given sparse vector and maintain a dictionary of only the non-zero keys and their respective values.

>>> practiceQuestion([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2])
    {4:1, 10:2}

54.  Write a program to reverse a given dictionary without any information loss from the duplicates.

>>> practiceQuestion({'a':1, 'b':2, 'c':3, 'd':2})
    {1:['a'], 2:['b', 'd'], 3:['c']}

55.  Write a program to find the year in which the player scored maximum goals in the below year:goals dictionary.

>>> practiceQuestion({2015:8, 2016:12, 2017:22, 2018:14, 2019:12})
    2017


Django training in Bangalore