0% found this document useful (0 votes)
13 views

Data Types and Variables 4

The document introduces data types and variables in programming. It explains that a variable is a space in memory allocated to store a piece of information, like a box with a label. There are different types of variable boxes that can store different data types, like text, integers, or decimals, and boxes can be different sizes to store varying amounts of data. The document also provides tasks to help the reader learn about common data types, naming variables, and converting between data types.

Uploaded by

Zhen Nan Chong
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data Types and Variables 4

The document introduces data types and variables in programming. It explains that a variable is a space in memory allocated to store a piece of information, like a box with a label. There are different types of variable boxes that can store different data types, like text, integers, or decimals, and boxes can be different sizes to store varying amounts of data. The document also provides tasks to help the reader learn about common data types, naming variables, and converting between data types.

Uploaded by

Zhen Nan Chong
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Types and Variables

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.

Task 1 – Data Types


A few of the data types you might use are shown in the table below. Try and match each type to its description. Common sense
should get you through here but if you need help, search for ‘Python data types’.

Variable Type Desciption

1 bool (Boolean) • • a A group of unordered items that are all different (each is unique).

2 list • • b Positive or negative whole numbers.

3 set • • c Positive or negative numbers with decimal points.

4 float (decimal) • • d This has two possible values, True or False (1 or 0).

5 int (integer) • • e Essentially, a piece of text of any kind.

6 str (string) • • f An ordered sequence of items.

Task 2 – Naming Variables


There is really only one common convention in place for the naming of variables in Python. This is to use all lower-case letters
separated by underscores e.g. first_name, last_name etc.

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.

a. A variable holding the age of someone in years ____________________________________________________________


b. A variable holding a street address ______________________________________________________________________
c. A variable holding the value True if someone is a member ___________________________________________________
d. A variable holding a Date of Birth _______________________________________________________________________

CoP046 – Algorithms & Programming: Python  ORB Education. Visit www.orbeducation.com for a range of quality teaching resources.
Data Types and Variables (page 2)

Task 3 – Text or Number?


Look at the program on the right. It takes two numbers and adds them
together, then another two numbers and adds them together.

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.

Task 4 – Data Type Conversions


It is often necessary to convert variables from one type of data to another. For example (as you found in the last task) any data
entered into the console is accepted as a string. These strings may be fine if you are working with names or addresses, but they
are not so good if you are trying to accept numbers for a calculation.

Strings can be converted into integers using the int function.


integer = int(string)
If, for example, you have a variable first_number holding the string “4”, then the code int(first_number) will give you the integer
4. This can then be used in a calculation. The program below demonstrates this.

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

c. The program on the right changes a decimal number (called a floating-


point number) into an integer. Create the program and see what
happens when decimals such as 2.1, 2.5 and 2.9 are assigned in line 1.
Save as “08.4c int”.

CoP046 – Algorithms & Programming: Python  ORB Education. Visit www.orbeducation.com for a range of quality teaching resources.

You might also like