Extras

Tuples

Definition

Tuples are like lists. The difference is that tuples are immutable (this means they cannot be changed once made). Tuples are created with round brackets and lists with square brackets.

Lists are similar to arrays in other languages. A tuple is similar to a record in other languages.

Note

As tuples are immutable (the values stored cannot be changed), you cannot append (add) an item to a tuple like you can with a list.

Easy example

student = ('Sam', 'Taylor', 15, 'Reading', 'Berkshire')
print(student[3])
Show/Hide Output
reading

Note

Notice how we create a tuple with round brackets, but access data in it using square brackets.

Syntax

variableName = (item1, item2, item3, ...)

Examples

Example 1 - Creating a tuple with items in

student = ('Sam', 'Taylor', 15, 'Reading', 'Berkshire')

for item in student:
   print(item)
Show/Hide Output
Sam
Taylor
15
Reading
Berkshire

Example 2 - Finding single items in a tuple

student = ('Sam', 'Taylor', 15, 'Reading', 'Berkshire')

firstName = student[0]
lastName = student[1]
age = student[2]
city = student[3]
county = student[4]

print('First name: ' + firstName)
print('Age: ' + str(age))
Show/Hide Output
First name: Sam
Age: 15

Example 3 - Finding the length of a tuple

student = ('Sam', 'Taylor', 15, 'Reading', 'Berkshire')
print(len(student))
Show/Hide Output
5

Key points

Note

As with lists, tuple references start at zero.

Hint

If you need to change an item in a tuple, you must create a new tuple and the necessary items from the old tuple. This is because tuples are immutable and therefore cannot be changed.

Dictionaries

Python allows you to associate a key with a value. It is like a real dictionary, where each word would be the key and the definition of the word would be the value.

Each key in a Python dictionary must be unique. All keys must be the same data type (for example, all strings or all numbers), but the values can be of any data type.

Easy example

student = {'Firstname': 'Sam', 'Lastname': 'Taylor', 'Age': 15}

print(student['Firstname'])
Show/Hide Output
Sam

Note

This looks up the key 'Firstname' in the dictionary student to find the value 'Sam'.

Syntax

variableName = {key1: value1, key2: value2, key3: value3}

Examples

Example 1 - Creating a dictionary with items in

csDict = {'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
print(csDict)
Show/Hide Output
{'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}

Example 2 - Finding an item in a dictionary

csDict = {'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
print(csDict['binary'])
Show/Hide Output
counting system of 0,1

Note

This will give an error if the key isn’t in the dictionary. Use csDict.get('binary') if you’re not sure whether the key is in the dictionary. This will return None if there is no key. Alternatively, use 'binary' in csDict to check whether the key is in the dictionary.

Example 3 - Updating an item in a dictionary

csDict = {'alu': 'arithmetic logic', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
print(csDict['alu'])
csDict['alu'] = 'Arithmetic Logic Unit'
print(csDict['alu'])
Show/Hide Output
arithmetic logic
Arithmetic Logic Unit

Example 4 - Adding an item in a dictionary

csDict = {'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
csDict['debugger'] = 'Tool to test and debug programs'
print(csDict)
Show/Hide Output
{'debugger': 'Tool to test and debug programs', 'alu': 'arithmetic logic', 'compiler': 'converts instructions into machine code', 'binary': 'counting system of 0,1'}

Example 5 - Deleting an item from a dictionary

csDict = {'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
del csDict['alu']
del csDict['compiler']
print(csDict)
Show/Hide Output
{'binary': 'counting system of 0,1'}

Example 6 - Finding the length of a dictionary

csDict = {'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
print(len(csDict))
Show/Hide Output
3

Example 7 - Find if a dictionary has a certain key

csDict = {'alu': 'arithmetic logic unit', 'binary': 'counting system of 0,1', 'compiler': 'converts instructions into machine code'}
print('binary' in csDict)
print('bin' in csDict)
Show/Hide Output
True
False