Lists and Tuples In todays lesson we will

  • Slides: 8
Download presentation
Lists and Tuples In today’s lesson we will look at: • what an array

Lists and Tuples In today’s lesson we will look at: • what an array is • why arrays are useful • programming with lists in Python

The Need for Arrays • We’ve looked a how to ask the user questions

The Need for Arrays • We’ve looked a how to ask the user questions and store the answer in a variable, e. g. age = input("How old are you? ") • But what if there’s more than one person? • We could do something like this: age 1 = input("How old is person 1? ") age 2 = input("How old is person 2? ") age 3 = input("How old is person 3? ") • Can you see any problems with that approach?

Arrays We can solve that problem using an array. Arrays: • are collections of

Arrays We can solve that problem using an array. Arrays: • are collections of variables that all have the same name • can store any type of data: numbers, text, true/false – even other arrays! • have a number – called an index – that selects which of the values you want to use For example, you might have set of ages called age, and the first value would be age[0].

How is That Useful? Arrays are useful for: • storing collections of things that

How is That Useful? Arrays are useful for: • storing collections of things that are all stored and processed in the same way – e. g. a program that picks six lottery numbers • when you don't know how many values there are going to be – generalisation! • creating a look-up – e. g. to pick a random day of the week. • Answering GCSE questions on arrays!

Python And Arrays • Bad News: Python has no native (i. e. built-in) support

Python And Arrays • Bad News: Python has no native (i. e. built-in) support for arrays • Good News: Python has things called lists and tuples that are very similar • Here's an example Python program using a list: age = [] for n in range(3): a = int(input("Age: ")) age. append(a) print("The ages entered are: ", age)

Lists in Python • You can create a list with things in it: name

Lists in Python • You can create a list with things in it: name = ["Tom", "Dick", "Harry"] • You can create an 'empty' list: name = [] • You can append (i. e. add) an item to a list: name. append("Rita") • You can look at the value of any item in the list using its index – its position in the list (we start counting from 0): name[0] would give you Tom name[1] would give you Dick, etc.

Lists in Python • You can see how many items are in a list:

Lists in Python • You can see how many items are in a list: age = [51, 13, 46, 4] len(age) would give the answer 4 • You can sort a list: sort(age) would make age [4, 13, 46, 51] • You can find the smallest number in a list: min(age) would give the answer 4 • You can find the largest number in a list: max(age) would give the answer 51

Python Example Creates an empty list Repeat three times age = [] for n

Python Example Creates an empty list Repeat three times age = [] for n in range(3): a = int(input("Age: ")) age. append(a) Ask for an age Add the age to the list print("The ages entered are: ", age) Print the whole list