Monday, 18 September 2017

A First Look at Lists

Previously I've mentioned three different data types: strings (enclosed in quotes, and treated as text, such as words and sentences), integers (whole numbers) and floating point numbers (numbers with a decimal point).

Lists are another data type. Whereas strings have quote marks at each end ("" or ''), lists use square brackets ([]) at each end and commas to separate the elements of the list. Element is the term for each item in the list. For example this script:
#!/usr/bin/python3
foo = ['Hello', "World", 4, 6.333, 5, "This is a list"]
for elem in foo:
    print (elem, type(elem))
produces the output:
  RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04b_listdemo.py
Hello <class 'str'>
World <class 'str'>
4 <class 'int'>
6.333 <class 'float'>
5 <class 'int'>
This is a list <class 'str'>

>>>
 There are a number of points here:
  • Lists, like other data types, are often assigned to variables, in this case foo
  • for loops can go through each element (item) in a list instead of a range of numbers
  • A list can contain different types of data, including strings, floats & integers (here using the type() function we encountered in a previous post). 
I can modify the script to show that lists can even include other lists, or variables containing lists.
#!/usr/bin/python3
zim = ['list', 'within', 'a', 'list']
foo = ['Hello', "World", 4, 6.333, 5, "This is a string", zim]
for elem in foo:
    print (elem, type(elem))
with the output:
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04b_listdemo.py
Hello <class 'str'>
World <class 'str'>
4 <class 'int'>
6.333 <class 'float'>
5 <class 'int'>
This is a string <class 'str'>
['list', 'within', 'a', 'list'] <class 'list'>

>>>
 The len() function returns the length of whatever is in the brackets. So to demonstrate I can add a few lines to the script:
#!/usr/bin/python3
zim = ['list', 'within', 'a', 'list']
foo = ['Hello', "World", 4, 6.333, 5, "This is a string", zim]
for elem in foo:
    print (elem, type(elem))
print ('elements in foo = ', len(foo))
print ('elements in zim = ', len(zim))
with the resulting output:
  RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04b_listdemo.py
Hello <class 'str'>
World <class 'str'>
4 <class 'int'>
6.333 <class 'float'>
5 <class 'int'>
This is a string <class 'str'>
['list', 'within', 'a', 'list'] <class 'list'>
elements in foo =  7
elements in zim =  4

>>>
Although you may have noticed, I'll just point out that in calculating the length of foo, len(foo) does not  include the elements within zim - zim is treated as a single element.

Just as an aside, len() also works on strings, counting then returning the number of characters in the string. For example, on the Python command line:
>>> len("Hello World")
11
>>> example = "This is an example string"
>>> len(example)
25
>>>
 Back to lists. If a list just contains numbers, then you can use a for loop to do maths on them. Here's another script.
#!/usr/bin/python3
# initialising the list variable
x = [45, 32, 46, 21, 40, 39, 28, 55]
total = 0
for num in x:
    total = total + num
print ('List is ', x)
print ('Total for list is: ', total)
avg = total / len(x)
print ('Average for list is ', avg)
which gives the output:
  RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04c_numberlist.py
List is  [45, 32, 46, 21, 40, 39, 28, 55]
Total for list is:  306
Average for list is  38.25

>>>
 As you can see, len() is useful for tasks such as finding averages as you don't need to work out for yourself how many elements you need to divide the total by.

No comments:

Post a Comment