Tuples, Lists and Dictionaries

Tuples, List and Dictionaries

You need to know three types of creating collections of data:

  • Tuples
  • Lists
  • Dictionaries

Tuples

A tuple

  • is a collection of data, surrounded by ()
  • cannot be changed whilst the program is running
  • consider it as a group of constant items defined by the programmer
  • often used for menu items that are not going to change
a_tuple = (54, 99, 3.142)

Lists

A list

  • is a collection of data, surrounded by []
  • can be changed whilst the program is running
  • the most common way collections of data are stored
  • can store strings, characters, numbers (not recommended)
  • other programming languages use the term 'array'
a_list = ['red', 'green', 'blue']

Dictionaries

A dictionary

  • is a collection of data, surrounded by {}
  • can be changed whilst the program is running
  • each item has an index or key to help identify each piece of data
a_dictionary = {
  foreground: 'red',
  background: 'green', 
  text: 'blue',
  }

Example I

Output:
>> 1
fruit_tuple = (
  'apple',
  'banana',
  'strawberry',
  'orange',
)
print(fruit_tuple.index('banana'))

Remember tuples/lists start at zero

Display the position of an item in a list/tuple:

Example II

Output:
>> strawberry
fruit_tuple = (
  'apple',
  'banana',
  'strawberry',
  'orange',
)
print(fruit_tuple[2])

Display a item from a tuple/list:

Example III

>> ['Albert','Bart','Charles','David','Edward']
>> Albert Bart Charles David Edward
names_list = [
  'Albert',
  'Bart',
  'Charles',
  'David',
  'Edward',
]
print(names_list)
print(*names_list)

Create a list (plus extra!):

Example IV

>> Albert Bart Charles David Edward
>> Albert Bart David Edward
names_list = [
  'Albert',
  'Bart',
  'Charles',
  'David',
  'Edward',
]
print(*names_list)
del names_list[2]
print(*names_list)

Delete an item from a list:

Example V

>> Albert Bart Charles David Edward
>> Albert Bart Charles David Edward Frank
names_list = [
  'Albert',
  'Bart',
  'Charles',
  'David',
  'Edward',
]
print(*names_list)
names_list.append('Frank')
print(*names_list)

Add an item to the end of a list:

Example VI

>> Edward Albert Charles David Bart
>> Albert Bart Charles David Edward
>> Edward Albert Charles David Bart
names_list = [
  'Edward',
  'Albert',
  'Charles',
  'David',
  'Bart',
]
print(*names_list)
print(sorted(names_list))
print(*names_list)

Display a sorted tuple/list:

Example VII

>> Edward Albert Charles David Bart
>> Albert Bart Charles David Edward
names_list = [
  'Edward',
  'Albert',
  'Charles',
  'David',
  'Bart',
]
print(*names_list)
names_list.sort()
print(*names_list)

Sort a list:

You cannot sort a tuple. Tuples cannot be changed by the program.

Example VIII

>> {1: 'red', 2: 'green', 3: 'blue'}
colours = {
  1:'red',
  2:'green',
  3:'blue',
}
print(colours)

Create and display a dictionary:

Example IX

>> {1: 'red', 2: 'green', 3: 'blue'}
>> {1: 'red', 2: 'purple', 3: 'blue'}
colours = {
  1:'red',
  2:'green',
  3:'blue',
}
print(colours)
colours[2] = 'purple'
print(colours)

Change a value in a dictionary:

Tasks I

  1. Display a random integer between 1 and 100 inclusive.
  1. Display a random fruit from a list of five fruits.
  1. Randomly choose either heads or tails (‘h’ or ‘t’). Ask the user to make their choice. If their choice is the same as the randomly selected value, display the message ‘You win’, otherwise display ‘Bad luck’. At the end, tell the user if the computer selected heads or tails.

Tasks II

  1. Randomly pick a whole number between 1 and 10. Ask the user to enter a number and keep entering numbers until they enter the number that was randomly picked.
  1. Update program 5 so that it tells the user if they are too high or too low before they pick again.
  1. Randomly choose a number between 1 and 5. Ask the user to pick a number. If they guess correctly, display the message ‘Well done’, otherwise tell them if they are too high or too low and ask them to pick a second number. If they guess correctly on their second guess, display ‘Correct’, otherwise display ‘You lose’.

Tasks III

  1. Make a maths quiz that asks five questions by randomly generating two whole numbers to make the question (e.g. [num1] + [num2]). Ask the user to enter the answer. If they get it right add a point to their score. At the end of the quiz, tell them how many they got out of five.
  1. Display five colours and ask the user to pick one. If they pick the same as the program has chosen, say ‘Well done’, otherwise display a witty answer which involves the correct colour, e.g. ‘I bet you are GREEN with envy’ or ‘You are probably feeling BLUE right now’. Ask them to guess again: if they have still not got it right, keep giving them the same clue and ask the user to enter a colour until they guess it correctly.

J Tuples Lists Dictionaries

By David James

J Tuples Lists Dictionaries

Python - Tuples Lists Dictionaries

  • 439