Python Exercises

Welcome to Python Exercises, this is the part of our Python week where we test what you've learned so far.

Try not to panic though. This isn't a marked piece of work, its just helping you to test your skills with not only Python and its syntax, but with industry specific names.

Enter your answer to each task and then press submit to get your score!

At the end of the quiz you just enter your name, and press submit and then you're done!

Have fun!

Task 1

How would you make the terminal say Hello World?


Task 2

Create a variable called carname and give it a value of Volvo


Task 3

Create a variable named i and assign it a value of 50


Task 4

Fix this variable so it is best practice

2my-1st_name = "john'

Task 5

How would we print this string in all capitals?

"i live in england"

Task 6

How would you access the random library?


Task 7

After you have imported the random library, how would you generate and print a random integer between 1 and 10?


Task 8

How would you make a programme that asks for your name as input?


Task 9

How would you print out this variable?

fav_drink="coffee"

Task 10

How would you convert this to an f string?

print("My favourite drink is {}".format(fav_drink))

Task 11

Without changing this code what would be the best way to add 2 to i?

i = 10

Task 12

What would you expect the terminal output to be for this code snippet?

age = 21

if age > 17:
print("Yes I can serve you")
else:
print("You aren’t old enough")

Task 13

print(10/3)

gives a result of 9

What would we expect the output be for this code?

print(10%3)

Task 14

If you were to run this code, what would the expected output be?

password = "qwerty"

if len(password) < 8:
print("Password is too short")
else:
print(password)

Task 15

If you were to run this code, what would the expected output be?

num = 21


if num % 3 == 0 and num % 7 == 0:
print("Fizz Buzz")

elif num % 7 == 0:
print("Buzz")

elif num % 3 == 0:
print("Fizz")

else:
print(num)

Task 16

What would you add to the bottom of this code to order a large latte?

def coffee_order(size, drink_type):


print(f"Here’s your {size} {drink_type}.")

Task 17

This function isn't working, everything seems to be there but I'm getting this error

Traceback (most recent call last): File "/Users/Desktop/CodeNation/python/Exercises/tasks.py", line 23, cash_machine(1234, 400)

File "/Users/Desktop/CodeNation/python/Exercises/tasks.py", line 15, in cash_machine if entered_pin == actual_pin and amount <= balance:

UnboundLocalError: local variable 'balance' referenced before assignment

What do I need to add to make it work?

actual_pin = 1234
balance = 500

def cash_machine(entered_pin, amount):

________________

if entered_pin == actual_pin and amount <= balance:
balance -= amount
print(f"Dispensing cash of £{amount}. Your new balance is £{balance}.")

elif entered_pin == actual_pin and amount > balance:
print(f"Insufficient funds, you cannot withdraw £{amount}. You only have £{balance} in your account.")
else:
print("Incorrect PIN")


cash_machine(1234, 400)

Task 18

Rewrite this line of code so it works

fav_films == "Ghostbusters" 'Deadpool" "Titanic"]

Task 19

Now print the list into the terminal


Task 20

Now print Ghostbusters into the terminal using the list


Task 21

What method would you use to remove the last item from your list?


Task 22

Using a for loop, how would you write a programme that counts from 1-10?


Task 23

Now what would you add to the code to print 1-10 in the terminal?


Task 24

So taking the previous code into account, how would you count to 10 in increments of 2


Task 25

How would you write a programme to make the numbers count backwards?


Task 26

What would you expect the output of this code to be?

for i in range(5):

print("hello ")

Task 27

A while loop in Python is used for what type of iteration?

Indeterminate
Indefinite
Discriminant
Definite


Task 28

What would I need to add to line 3 to stop this programme running infinitely?

1. n = 5

2. while n > 0:
3.
4. print(n)

Task 29

What is this data type?

True

Task 30

How would you make this code a comment?

This is a comment
00:00