Data types

Every variable has a data type. As the name suggests, this controls the type of data that is stored within the variable. Here are some of the simple data types:

String

A string is a sequence of Unicode characters that may be a combination of letters, numbers, and special symbols. They are enclosed in either single or double quotes. It doesn’t matter which you use but the start and end quote must be the same.

string_with_double_quotes = "I am a string enclosed in double quotes"
string_with_single_quotes = 'I am a string enclosed in single quotes'

You can use the other type of quote within your string.

string_with_double_quotes = "Isn't it nice to use apostrophes?"
string_with_single_quotes = 'They said to me "No apostrophes" in this one'

If you do need to use the same type of quote, then you must escape it with a backslash.

string_with_double_quotes = "You would need to escape \"Quotes\" in this string"
string_with_single_quotes = 'It\'s required to escape apostrophes in this string'

You can join multiple strings together using a process called concatenation. To do this in Python you use the + sign.

string_start = "Hello"
string_end = "World"
complete_string = string_start + " " + string_end

In the example above we are joining three strings together. Two of them a contained in the variables string_start and string_end, but there is also a space stored as a raw string placed between the two variables.


Integer

Integers are whole numbers without a decimal point and can be positive or negative.

number1 = 13
number2 = -203
player_health = 5

Float

Floats or floating point numbers are numbers that contain a decimal point and can be either positive or negative. They can also use scientific notation.

number1 = 7.502
number2 = 9.8e3
player_speed = 10.5

Python can readily convert between the numeric types so you don’t have to worry about mixing them in an equation.

total = 10 / 2.5

You can readily convert between the numeric data types using built in functions. We’ll cover functions later on in the course.

The code:

number1 = 12.0
number2 = int(number1)
number3 = float(number2)
print(number1)
print(number2)
print(number3)

Would give an output:

12.0
12
12.0

Boolean

Boolean data types are either True or False. The capitalisation of True and False is important as these are reserved Python key values.

Just as with the numeric data types you should always wrap a Boolean variable in a str() function if displaying it on the screen or saving it in a text file.

Booleans are really useful to set as flags showing the state of a program or object.

boolean_variable_1 = True
boolean_variable_2 = False
is_player_jumping = True

List

A list can be used to store multiple variables of any type.  This is useful if you want to organise related data.

The list can contain a mix of data types.

You can access an individual value in a list using its index. This is a number that starts at 0 for the first entry and then increments by 1 for each additional value.

You can make an empty list to be filled later. To add an element to an existing array you can use the append function.

animals = ["cat", "dog", "rabbit"]
chosen_animal = animals[1]
shopping_list = []
shopping_list.append("Apples")

Python stores a string as a list. So you can access individual characters of the string in the same way that you would a list.

text_to_display = "Hello World!"
print(text_to_display[4])

This would print “o” as it is the fifth character in the string. Remember indexes start at 0.


Dictionary

A dictionary is like a list but uses a key-value pair for each entry. This allows you to set a unique key for each entry. Dictionaries use curly braces instead of the square brackets used by lists.

You can access an individual value in a dictionary using their key.

You can make an empty dictionary which can be filled later.

ages = {'JDoe': 29, 'MSmith': 41, 'TChan': 30}
chosen_person = ages['MSmith']
exam_grades = {}

To add entries into a blank list you can either set it using their key, or use the update method to pass in a dictionary of values to add. For example:

The code:

exam_grades = {}
print(exam_grades)
exam_grades['JDoe'] = 85
print(exam_grades)
exam_grades.update({'MSmith': 67, 'BMacleod': 81})
print(exam_grades)

Would give an output:

{}
{'JDoe': 85}
{'JDoe': 85, 'MSmith': 67, 'BMacleod': 81}

What data type would you use to store each of the following?

  • Exam mark between 0 – 100
  • A vehicle’s number plate
  • Café menu with prices
  • Book titles at a library
  • Forward velocity of a vehicle in MPH
Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.