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

Define your own variables

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

In this step, there are two exercises to enable you to practice defining variables within a Python program. We have provided the solutions, so don’t worry if you get stuck!

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

Within the .zip file ‘Supporting files and solutions‘ you will see the folder lab 2 containing two files: exercise1.py and exercise2.py.

For each exercise outlined below, open the corresponding file (1) and write the code in the editor (2), as shown in the example figure below. Write your code in the space indicated by the arrow numbered 2.

Exercise 1

  1. Open exercise1.py and copy-paste line 3 into the editor:
print(f'Hello, my name is {my_name} and I am {age} years old.')

The above print statement will print your name and your age. However, the print uses variables and the print statement will change depending on the values of the variables. The variables used are enclosed within the curly braces ({variable_name}).

Define the two variables needed to make this program work. Define those variables in lines 1 and 2, above the print statement. This is because the computer executes the program line by line, so you need to define the variables before they first appear in the print statement; therefore, define the necessary variables with the appropriate values (your name and age) before the print statement (that is, define two variables, one in line 1 and one in line 2).

Remember that a variable that holds text is called a string variable (for example, your name in this exercise). When defining a string variable, you will need to surround the value by quotes "". For example, if your name is Anna, then a variable called name = "Anna" (note the quotes around Anna). You do not need the quotes for numbers like your age (for example, age = 23).

Running the code: Open a terminal (select ‘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 result:

> python exercise1.py
Hello, my name is YOUR NAME and I am YOUR AGE years old.

Exercise 2

Open exercise2.py and define the following string variables at the top: full_name, address_line, post_code, country and telephone_number

Fill in your details for each of these variables. Then create an f-string called label (similar to the one we provided to you in exercise1.py) that combines all the information together in a way as if it appeared on a parcel that arrived to you, and use the print function to print it out, like in the previous exercise. Run and verify your program.

Remember that strings (i.e. text) will need to be surrounded by quotes "". Even the telephone number, which in this case is a number, you could surround it with quotes because we are not planning to do mathematical operations on it, so even the telephone number could be defined as a string!

Finally, to define an f-string, you need to place the letter f before the quotes f'my text' or f"my text" (in Python, strings can be defined either with single or double quotes).

Hint: You can print to a different line within the print function by using the new line character, n, so for example, print(f"Heynthere") will print the hey and there on different lines. n is a special character used for computers to indicate “new line”. Use n to create new lines in your label.

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 result:

> python exercise2.py
Your Full Name, 
Your Address Line, 
Your Post Code, 
Your Country,
Your Tel Number

Solutions

Once you have attempted the exercises, check the solutions to see how you did.  You can find these in the lab 2 folder of the .zip file ‘Supporting files and solutions‘.

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