Data Types and Variables 4
Data Types and Variables 4
A variable is a space in the computer’s memory allocated to a piece of information. You can think of a
Aim: To learn
variable as a box where this information is stored. The box has a label – the variable name. There are
about different
different types of boxes for the different types of data, whether text, integers or decimals etc. There data types and
are also different size boxes to store different amounts of information. variables.
Note: In some programming languages, you must declare each variable before you use it in your
program (basically say ‘this variable exists and this is what it is for’). In Python, you do not
need to declare variables but you do need to remember what type of data you have assigned
to each. You will create errors in your programs if you mix up your data types.
1 bool (Boolean) • • a A group of unordered items that are all different (each is unique).
4 float (decimal) • • d This has two possible values, True or False (1 or 0).
Naming variables is otherwise down to personal style. Descriptive words are encouraged, for example, number_of_rows or
last_letter.
One convention that is sometimes used is to end the variable name with the type of data that the variable will hold. For
example, first_name_str for a string, or highest_int for the highest integer. This can help you remember what each variable is
being used for. However, this is only useful if the words chosen don’t make it obvious anyway (first_name will clearly hold a
string, so nothing more is required).
For Boolean variables, try and state a fact that can be true or false, such as is_paid, or is_registered.
Suggest variable names for the following pieces of data. There are no right or wrong answers.
CoP046 – Algorithms & Programming: Python ORB Education. Visit www.orbeducation.com for a range of quality teaching resources.
Data Types and Variables (page 2)
a. What do you think you will get for the two lines of output?
________________________________________________
________________________________________________
b. Create the program and find out what actually happens. Save as “08.3 Text Number 1”.
c. Copy the code and paste it into a new session. Adapt it so that it accepts two
numbers from the user through the console and adds these together. Are the
numbers inputted through the console strings or integers? Save as “08.3 Text
Number 2”. We have shown part of our solution on the right.
a. Create the program and test it with and without the int
function on lines 6 and 7 i.e. try:
number1_int = int(number1_str)
number2_int = int(number2_str)
and
number1_int = (number1_str)
number2_int = (number2_str) Note: In this case, we have named the variables with the
extra _str or _int parts so that we remember what type
Save as 08.4 int. of data is being held at different points in the program.
b. Write a similar program that uses the str function to change integers to strings. Display the effect that the function has
using outputs to the console. You can fix the starting numbers in the program. Save as “08.4b str”.
CoP046 – Algorithms & Programming: Python ORB Education. Visit www.orbeducation.com for a range of quality teaching resources.