Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Input and output in Python

Learn algorithms, logic, and Python basics. Create simple programs, grasp computer science fundamentals, and see its impact across various fields.

These three short exercises will help you practice your Python programming skills on reading input from a user, declaring variables, and doing basic operations on numbers (addition, subtraction, multiplication etc). Don’t worry if you get stuck – the solutions are provided.

To get started, open Visual Studio Code on your computer. 

Within the .zip file Supporting files and solutions you will see the folder lab 3. Use the exercise1.py, exercise2.py and exercise3.py files provided to you to write the code for each of the exercise below.

Exercise 1

The problem you need to solve

The Central Bank of Python gives you a loan with an interest rate of 4%. Write a Python program to read the loan amount from the user and calculate and print the total interest the customer will pay in a year. (interest = amount * (interest rate / 100)).

Prepare

In Visual Studio Code, open the exercise1.py file. You will see a skeleton code with multiple TODO comments to help you complete this exercise. Each TODO block you will see (they appear as Python comments with # and the keyword TODO) is a small sub-task for you. 

Complete the code

Have a look at all the TODO comments and complete the code required for each.
Running the code: Open a terminal (click “Terminal” → “New Terminal” from the menu bar on the top) and type python exercise1.py in the terminal pane (and hit the ENTER key) to run the program.
Expected output:
❯ python exercise1.py
What is the amount you want to borrow? 1000
The interest you will pay in a year for £1000.0 with an interest rate of 4% is £40.0

Exercise 2

The problem you need to solve

You are asked to create a Python program that reads a temperature in Celsius and converts and print it to Fahrenheit. The formula is F = (C * 1.8) + 32 (where F is Fahrenheit and C is Celsius).

Prepare

In Visual Studio Code, open the exercise2.py file. You will see a skeleton code with multiple TODO comments to help you complete this exercise. Each TODO block you will see (they appear as Python comments with # and the keyword TODO) is a small sub-task for you. 

Complete the code

Have a look at all the TODO comments and complete the code required for each.
Running the code: Open a terminal, if not already opened, (click “Terminal” → “New Terminal” from the menu bar on the top) and type python exercise2.py in the terminal pane (and hit the ENTER key) to run the program.
Expected output:
❯ python exercise2.py
Please provide temperature in Celsius: 32
Temperature in Fahrenheit: 89.6

Exercise 3

The problem you need to solve

Write a Python program that reads two numbers and calculates and prints the result of addition, subtraction, multiplication and division. The input value can be an integer or a float (hint: if integer or float, always convert input to float).

Prepare

In Visual Studio Code, open the exercise3.py file. You will see a skeleton code with multiple TODO comments to help you complete this exercise. Each TODO block you will see (they appear as Python comments with # and the keyword TODO) is a small sub-task for you. 

Complete the code

Have a look at all the TODO comments and complete the code required for each.
Running the code: Open a terminal, if not already opened, (click “Terminal” → “New Terminal” from the menu bar on the top) and type python exercise3.py in the terminal pane (and hit the ENTER key) to run the program.

Expected output:

❯ python exercise3.py
Please provide the first number: 6
Please provide the second number: 2
Addition: 8.0
Subtraction: 4.0
Multiplication: 12.0
Division: 3.0

Solutions

Once you have attempted the exercises, check the solutions to see how you did.  You can find these in the folder lab 3 of the .zip file ‘Supporting files and solutions‘ on page 1.5 Python, Visual Studio Code, and course files.

Please do not look at the solutions before you attempt the exercises.

This article is from the free online

An Introduction to Programming Using Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now