Comments

53 Python Exercises and Questions for Beginners

In this post, I’m going to list a bunch of Python exercises and questions for beginners. If you’re starting out with Python, this post is a good way to test your knowledge and learn new things. You may also want to check out my Python Tutorial for Beginners on YouTube and Python 3 Cheat Sheet.

NOTE: This post is a work in progress and I’ll regularly add new questions to the list. So, be sure to come back for new coding exercises.  If you enjoy this post, please spread the love by share it with others.

Let’s get started!

Questions

Basics

  1. What is an expression?
  2. What is a syntax error?
  3. What is PEP8?
  4. What does a linter do?
  5. What is the result of this expression: “*” * 10
  6. What is CPython?
  7. How is CPython different from Jython?
  8. How is CPython different from IronPython?

Primitive Types

  1. What is a variable?
  2. What are the primitive built-in types in Python?
  3. When should we use “”” (tripe quotes) to define strings?
  4. Assuming (name = “John Smith”), what does name[1] return?
  5. What about name[-2]?
  6. What about name[1:-1]?
  7. How to get the length of name?
  8. What are the escape sequences in Python?
  9. What is the result of f“{2+2}+{10%3}”?
  10. Given (name = “john smith”), what will name.title() return?
  11. What does name.strip() do?
  12. What will name.find(“Smith”) return?
  13. What will be the value of name after we call name.replace(“j”, “k”)?
  14. How can we check to see if name contains “John”?
  15. What are the 3 types of numbers in Python?

Control Flow

  1. What is the difference between 10 / 3 and 10 // 3?
  2. What is the result of 10 ** 3?
  3. Given (x = 1), what will be the value of after we run (x += 2)?
  4. How can we round a number?
  5. What is the result of float(1)?
  6. What is the result of bool(“False”)?
  7. What are the falsy values in Python?
  8. What is the result of 10 == “10”?
  9. What is the result of “bag” > “apple”?
  10. What is the result of not(True or False)?
  11. Under what circumstances does the expression 18 <= age < 65 evaluate to True?
  12. What does range(1, 10, 2) return?
  13. Name 3 iterable objects in Python.

Functions

  1. What is the difference between a parameter and an argument?
  2. All functions in Python by default return …?
  3. What are keyword arguments and when should we use them?
  4. How can we make a parameter of a function optional?
  5. What happens when we prefix a parameter with an asterisk (*)?
  6. What about two asterisks (**)?
  7. What is scope?
  8. What is the difference between local and global variables?
  9. Why is using the global statement a bad practice?

Coding Exercises

  1. Write a function that returns the maximum of two numbers.
  2. Write a function called fizz_buzz that takes a number.
    1. If the number is divisible by 3, it should return “Fizz”.
    2. If it is divisible by 5, it should return “Buzz”.
    3. If it is divisible by both 3 and 5, it should return “FizzBuzz”.
    4. Otherwise, it should return the same number.
  3. Write a function for checking the speed of drivers. This function should have one parameter: speed.
    1. If speed is less than 70, it should print “Ok”.
    2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: “Points: 2”.
    3. If the driver gets more than 12 points, the function should print: “License suspended”
  4. Write a function called showNumbers that takes a parameter called limit. It should print all the numbers between 0 and limit with a label to identify the even and odd numbers. For example, if the limit is 3, it should print:
    • 0 EVEN
    • 1 ODD
    • 2 EVEN
    • 3 ODD
  5. Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
  6. Write a function called show_stars(rows)If rows is 5, it should print the following:
    • *
    • **
    • ***
    • ****
    • *****
  7. Write a function that prints all the prime numbers between 0 and limit where limit is a parameter.

 

Want to learn all the Python skills you need? Check out my Complete Python Course for Beginners.

Hi! My name is Mosh Hamedani. I’m a software engineer with two decades of experience and I’ve taught over three million people how to code or how to become professional software engineers through my YouTube channel and online courses. It’s my mission to make software engineering accessible to everyone.
Tags: ,

58 responses to “53 Python Exercises and Questions for Beginners”

  1. Manish says:

    Instead of Python 3 Cheat Sheet its spelled out Python 3 Chat Sheet in the hyperlink on3rd line.

  2. Hey Mosh. Could you please provide the answers to the practical questions?

  3. Nishath says:

    How should I enroll with this online course and get started to learn python..

  4. please provide the answer sheet

  5. Shailesh says:

    hey mosh your tutorials are awesome…. Can you please provide tutorials on Django ?

  6. Michael Caefm says:

    I want to check my answers. Please provide the answer sheet!

  7. Julián Varela says:

    I’m breaking my head with this exercise:
    5. Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.

    I’ve wrote this code:

    def sum_of_multiples(limit):
    result = 0
    limit += 1
    for x in [3, 5]:
    for y in range(1, limit):
    if x*y <= limit:
    result += x*y
    return result

    The problem is that I can't figure out how to eliminate de duplicates of the multiples of 3 and 5, like 15 or 30. Please help!

    • Julián Varela says:

      Sorry but de indentation disappears when I send the comment

    • Dapo says:

      def sum_multiples(limit=20):
      l = []
      limit+=1
      for i in xrange(limit):
      if(i%3 == 0 or i%5 == 0):
      l.append(i)
      print(l)

      sum_multiples()

      • Cornelius Du Preez says:

        Mmm… I like your approach to this problem.

        to return the sum of the multiples you still need to sum them.

        may be replace ‘print(l)’ with ‘print(sum(l))’

    • Koshy M.G. says:

      I just got the value with this logic. I used “not in” to check whether if the value already exists in the list or not:
      I’m a very beginner with just 2-3 days of experience in coding. So plz don’t mind if this seems a lame logic. 🙂 But plz do let me know if there’s any way to enhance this. That would be really helpful for me to learn.
      (Adding periods before each line just to keep the indentation in place if that might help.)

      def sum_of_multiples(limit):
      ….count = 0
      ….sum = 0
      ….multiples = []

      ….while count <= limit:
      ……..if count % 3 == 0 and count not in multiples:
      …………multiples.append(count)
      …………print(count)
      ……..elif count % 5 == 0 and count not in multiples:
      …………multiples.append(count)
      …………print(count)
      ……..count+=1
      ….for each in multiples:
      ……..sum +=each
      ….print("\nSum is " + str(sum))

      limit = int(input("Enter the limit: "))
      sum_of_multiples(limit)

      • Alessandro says:

        This is how i get there….

        def multi_sum(limit):
        _numbers=[0]
        _multisum=[]
        _while numbers[-1] != limit:
        __numbers.append(numbers[-1] +1)
        _for num in numbers:
        __if num % 3 == 0 or num % 5 == 0:
        ___multisum.append(num)
        _return multisum[1:]

        print (multi_sum(25))

        …did u meant something like this?

      • ricardo says:

        limit = int(input(‘limit: ‘))
        lst3 =[x*3 for x in range(1, limit//3+1)]
        lst5 = [x*5 for x in range(1, limit//5+1)]
        lst = [*lst3, *lst5]
        print(set(lst))

        i liked this. just learning anyways

    • Rehmana says:

      This is my logic might not be very efficient but it works

      limit = int (input(‘Enter the limit: ‘))
      m_three=1
      m_five = 1
      result = 0
      for i in range(1, limit):
      m_three = i * 3
      m_five = i * 5
      if m_five <= limit and m_three limit and m_three limit and m_three >limit:
      break
      print(result)

    • Przemek says:

      def multiple(r):
      x=0
      for x in range(r):
      if x%3 == 0 and x%5 == 0:
      print(x)
      x+=1

  8. Mecandris says:

    Here is the code:
    2. FizzBuzz
    def FizzBuzz(number):
    if number % 3 == number % 5:
    return ‘FizzBuzz’
    elif number % 5 ==0:
    return ‘Buzz’
    elif number % 3 == 0:
    return ‘Fizz’
    else:
    return number

    number = int(input(‘>’))
    drink = FizzBuzz(number)
    print(drink)

    3. Speed, License, Points
    def lic_suspension (speed):
    suspension_pt = 0
    while suspension_pt != 12:
    if speed = 12:
    return ‘License Suspended’, suspension_pt
    return “You’re overspeeding and license may get revoked”, suspension_pt

    speed = int(input(‘<'))
    vel, msg = lic_suspension(speed)
    print(vel)
    print(f"You've incurred {msg} points")

    4. ODD EVEN
    def showNumbers(limit):
    coll_1 = {}
    for item in range(limit+1):
    if int(item) % 2 == 0:
    num = item
    type = "EVEN"
    coll_1.update({num:type})
    # num = "EVEN"
    else:
    num = item
    type = "ODD"
    coll_1.update({num:type})
    return coll_1

    limit = int(input("<"))
    type = showNumbers(limit)
    print(type)

    5. Sum of Multiples of 3 and 5
    def multiples (limit):
    sum = 0
    if limit <= 2:
    print("Sum is zero. \nPlease enter a non zero number, preferrably greater than 3")
    return sum
    else:
    for item in range(limit + 1):
    if int(item) % 3 == 0 or int(item) % 5 == 0:
    sum += int(item)
    return sum

    limit = int(input('<'))
    total = multiples(limit)
    print(f"Total sum of multiples of 3 and 5 till {limit} is {total}")

    6. Stargazing
    def show_start(limit):
    for item in range(limit+1):
    gaze = ""
    for totem in range(item):
    gaze += 'x'
    print(gaze)

    star = int(input("<"))
    limit = show_start(star)
    print("Isn't it a wonder?")

  9. severi says:

    def buzzfizz(number):
    if number == 0:
    return number
    elif number / 3 == (round(number / 3)) and number / 5 == (round(number / 5)):
    print(“Buzzfizz”)
    elif number / 3 == (round(number / 3)):
    print(“Buzz”)
    elif number / 5 == (round(number / 5)):
    print(“Fizz”)
    else:
    return number

    print(buzzfizz(15))

    # why does this return answer and “none”

    • Koshy M.G. says:

      Not sure. But for me this logic worked:
      (Adding periods before each line just to keep the indentation in place if that might help.)

      def fizz_buzz(num):
      ….if num%3 == 0 and num% 5 != 0:
      ……..print(“Fizz”)
      ….elif num % 3 != 0 and num % 5 == 0:
      ……..print(“Buzz”)
      ….elif num%3 == 0 and num% 5 == 0:
      ……..print(“FizzBuzz”)
      ….else:
      ……..print(num)

      num = int(input(“Enter any number: “))
      fizz_buzz(num)

  10. 2.

    for number in range (1,101):
    if number %3==0 and number %5==0:
    print(‘Buzzfizz’)
    elif number %3==0:
    print(‘Buzz’)
    elif number%5==0:
    print(‘Fuzz’)
    else:
    print(number)

  11. Koshy M.G. says:

    I’m not sure what logic to use for Exercise 7 to fetch prime numbers. Can someone give me a hint plz.

  12. Abdelrahman Sabry says:

    We’ll here are some my answers 🙂

    2.
    fizz_buzz = int(input(“Output: “))
    if fizz_buzz % 3 == 0 and fizz_buzz % 5 != 0:
    print(“Fizz”)
    print(“You chose”, fizz_buzz)
    elif fizz_buzz % 5 == 0 and fizz_buzz % 3 != 0:
    print(“Buzz”)
    print(“You chose”, fizz_buzz)
    elif fizz_buzz % (5 and 3) == 0:
    print(“FizzBuzz”)
    print(“You chose”, fizz_buzz)
    else:
    print(fizz_buzz)
    print(“You chose”, fizz_buzz)

    5.
    y = int(input(“”))
    t = [3,5]
    for i in list(range (1,y+1)):
    if i % 3 == 0 or i % 5 == 0:
    total = list(filter(lambda x:x % 3 == 0 or x % 5 == 0,range(1,y+1)))
    print(total,”\nSum is”,sum(total))

    7.
    y = int(input(“”))
    for i in range(1,y+1):
    r = list(filter(lambda x:i % x== 0, range(1,y+1)))
    if len(r) != 2:
    print(i)
    else:
    print(i,”PRIME”)

    I tried to make my codes as simple as possible, hope you like it!

  13. Hoang Khai says:

    def limit(lim):
    x=0
    for i in range(0,lim+1):
    if i%3==0 or i%5==0:
    x+=i
    return x

    limit(20)
    print(limit(20))

  14. Troy says:

    Thanks for this! 🙂

  15. akshita garg says:

    # 3
    def func(speed):
    … if(speed12):
    print(“license suspended”)

    func(30)
    ok
    func(80)
    point= 2.
    func(100)
    point= 6.0
    func(200)
    point= 26.0
    license suspended

    • akshita garg says:

      def func(speed):
      … if(speed 12):
      … print(“license suspended”)

      >>> func(30)
      ok
      >>> func(80)
      point= 2.0
      >>> func(100)
      point= 6.0
      >>> func(200)
      point= 26.0
      license suspended
      >>> func(135)
      point= 13.0
      license suspended

  16. def shownumber(limit) :
    for i in range(limit+1) :
    if( i % 2!=0) :
    print( i , ” :odd”)
    else :
    print(i , ” :even”)

    >>> shownumber(6)
    0 :even
    1 :odd
    2 :even
    3 :odd
    4 :even
    5 :odd
    6 :even

  17. # 3
    def func(speed) :
    if(speed 12 ) :
    print( ” license suspended”)

    >>> func(30)
    ok
    >>> func(80)
    point= 2.0
    >>> func(100)
    point= 6.0
    >>> func(200)
    point= 26.0
    license suspended
    >>> func(135)
    point= 13.0
    license suspended

  18. Rohit Kasumbiwal says:

    2. Write a function called Fizz_Buzz that takes a number :-

    print(“———–> FIZZ BUZZ GAME <———–")

    number = input("Enter The Number: ")
    number = float(number)
    if number % 15 == 0:
    print("Fizz Buzz")
    elif number % 3 == 0:
    print("Fizz")
    elif number % 5 == 0:
    print("Buzz")
    else:
    print(number)

  19. Rohit Kasumbiwal says:

    4. Write a function called showNumbers that takes a parameter called limit. It should print all the numbers between 0 and limit with a label to identify the even and odd numbers. For example, if the limit is 3, it should print:
    0 EVEN
    1 ODD
    2 EVEN
    3 ODD

    answer:-

    print(“———–> EVEN ODD GAME {numbers} is Even”)
    else:
    print(f”-> {numbers} is odd”)

    • Rohit Kasumbiwal says:

      print(“———–> EVEN ODD GAME {numbers} is Even”)
      else:
      print(f”-> {numbers} is odd”)

      answer is this

  20. adil says:

    # Question 6

    Limit = int(input(‘enter your number’)
    l = ‘ ‘

    for x in range(limit + 1):
    x = x + 1
    l = l + ‘*’
    print(x, ‘—-‘ ,l)

  21. adilchorfi says:

    speed =int(input(‘checking the speed of the driver: ‘))

    def urSpeed(speed):
    if speed speed >70:
    print (‘point’, int((speed -70)/5) ,’is gone’)

    if speed >=130:
    print(‘Licence suspended’)

    print(urSpeed(speed))

  22. adilchorfi says:

    limit = int(input(‘give the limit’))

    for x in range (0,(limit +1),1):

    if x % 2==0 or x==0:
    print(‘even’,x)
    else:
    print(‘odd’,x)

  23. Ndifon Luke says:

    HELLO SIR. Please do you also post questions on c# .net? This could go a long way to help jump start beginners. Thanks in advance..

  24. Muzaffar amin says:

    I did the Coding Exercises too successfully !
    Question 1:
    class Program:

    def method(self):
    number1 = int(input(print(“Enter first number = “)))
    number2 = int(input(print(“Enter Second number = “)))
    if number1 > number2:
    return number1
    else:
    return number2

    def main():
    call = Program()
    print(call.method())

    if __name__ == ‘__main__’:
    main()

  25. Muzaffar amin says:

    Coding Exercises
    Question : 2

    class Progam:

    def logic(self):

    number = int(input(print(“Enter the number =”)))
    if (number % 3) == 0:
    return “Fizz”
    elif (number % 5) == 0:
    return “Buzz”
    elif (number % 3) or (5 != 0):
    return “FizzBuzz”
    else:
    return number

    def main():

    call = Progam()
    print(call.logic())

    if __name__ == ‘__main__’:
    main()

  26. achal says:

    a=input(“enter the range”)
    s=0
    s1=0
    for i in range(1,a+1):
    if(i%3==0):
    c=i
    s=s+c
    print c,
    elif(i%5==0):
    b=i
    s1=s1+b
    print b,
    m=s+s1
    print ‘sum is \n’,m

  27. Milind Chilakwad says:

    def FizzBuzz(number):
    if number % 3 == number % 5:
    return “FizzBuzz”
    elif number % 5 ==0:
    return “Buzz”
    elif number % 3 == 0:
    return “Fizz”
    else:
    return number

    number = int(input(“> “))
    drink = FizzBuzz(number)
    print(drink)

  28. Rama Krishna says:

    Sir can you send the python course material link?
    I am new to python

  29. Divyashree says:

    Please let me know where i can the answers for the questions 🙂

  30. Mulualem Ayele Alemayehu says:

    i can’t download your sample data

  31. Usama says:

    Write a function for checking the speed of drivers. This function should have one parameter: speed.
    If speed is less than 70, it should print “Ok”.
    Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: “Points: 2”.

  32. Felix Park says:

    Hey Mosh. Its a cool tutorial but I can not find the excel file for the exercise. Is it supposed to be in the dropbox link? It doesn’t work, dropbox returns an error message.

  33. Saahil says:

    I am looking for advance course in Python

  34. verochen0920@gmail.com says:

    Where is the answer for all the questions?

Leave a Reply

Connect with Me
  • Categories
  • Popular Posts