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:
produces the output:#!/usr/bin/python3foo = ['Hello', "World", 4, 6.333, 5, "This is a list"]for elem in foo:
print (elem, type(elem))
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04b_listdemo.pyThere are a number of points here:
Hello <class 'str'>
World <class 'str'>
4 <class 'int'>
6.333 <class 'float'>
5 <class 'int'>
This is a list <class 'str'>
>>>
- 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).
with the output:#!/usr/bin/python3zim = ['list', 'within', 'a', 'list']
foo = ['Hello', "World", 4, 6.333, 5, "This is a string", zim]for elem in foo:
print (elem, type(elem))
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04b_listdemo.pyThe len() function returns the length of whatever is in the brackets. So to demonstrate I can add a few lines to the script:
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'>
>>>
with the resulting output:#!/usr/bin/python3zim = ['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))
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04b_listdemo.pyAlthough 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.
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
>>>
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")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.
11
>>> example = "This is an example string"
>>> len(example)
25
>>>
which gives the output:#!/usr/bin/python3# initialising the list variable
x = [45, 32, 46, 21, 40, 39, 28, 55]
total = 0for num in x:
total = total + numprint ('List is ', x)
print ('Total for list is: ', total)
avg = total / len(x)
print ('Average for list is ', avg)
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test04c_numberlist.pyAs 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.
List is [45, 32, 46, 21, 40, 39, 28, 55]
Total for list is: 306
Average for list is 38.25
>>>
No comments:
Post a Comment