# 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

- What is an expression?
- What is a syntax error?
- What is PEP8?
- What does a linter do?
- What is the result of this expression: “*” * 10
- What is CPython?
- How is CPython different from Jython?
- How is CPython different from IronPython?

### Primitive Types

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

### Control Flow

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

### Functions

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

## Coding Exercises

- Write a function that returns the maximum of two numbers.
- Write a function called
**fizz_buzz**that takes a number.- If the number is divisible by 3, it should return “Fizz”.
- If it is divisible by 5, it should return “Buzz”.
- If it is divisible by both 3 and 5, it should return “FizzBuzz”.
- Otherwise, it should return the same number.

- 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”.
- If the driver gets more than 12 points, the function should print: “License suspended”

- 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

- 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. - Write a function called
**show_stars(rows)****.**If**rows**is 5, it should print the following:- *
- **
- ***
- ****
- *****

- 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.

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

[…] 53 Python Exercises and Questions for Beginners […]

[…] Python Exercises for Beginners: https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/ […]

[…] Python exercises for beginners: https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/ […]

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

This is the condition .no answer sheets provided by examiner u know!

Do you want the answers to the Coding Exercises?

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

Here’s the link to the course: https://codewithmosh.com/p/python-programming-course-beginners

please provide the answer sheet

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

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

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!

Sorry but de indentation disappears when I send the comment

did you get the answer

after a month not sure have u solved.

def three_five_sum(limit):

result = 0

for number in range(limit):

if number % 3 == 0:

result += number

elif number % 5 == 0:

result += number

return result

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()

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?")

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”