The 7th lesson of Python tutorial Lists - BoardCode
The 7th lesson of Python tutorial Lists
Hi, welcome.
This is the lesson number 7 of Python tutorial.
We already talked about strings and strings methods
In this lesson we'll talk about the second type of sequences which is lists. Lists are iterable and mutable objects can contain any type of objects.


Creating lists.
To creat a list simplely we use []. Inside, we write its items separated with camma.

>>> []
[]

>>> len([])
0

>>> [" "]
[' ']

>>> len([" "])
1

>>> myList=['toast',[1,2,3],123,True,("a","b","c"),{1:None,2:None}]

>>> len(myList)
6

You could also creat a list with the built-in function list() like this:
>>> list((1,2,"Python"))
[1, 2, 'Python']

>>> list(("toast"))
['t', 'o', 'a', 's', 't']


Operations on lists:
Operations on lists are similar with strings'
concatination: joins two lists or more togother.
>>> []+["Hello"]+["world"]
['Hello', 'world']

>>> [1]+[" "]+["Python"]
[1, ' ', 'Python']

>>> ["Hi"]+"everyone"
Traceback (most recent call last): File "", line 1, in ["Hi"]+"everyone" TypeError: can only concatenate list (not "str") to list

>>> ["Hi"]+1
Traceback (most recent call last):
File "", line 1, in

["Hi"]+1
TypeError: can only concatenate list (not "int") to list

repition: repeats list's items a specified number of times.
>>> ["Hi"]*5
['Hi', 'Hi', 'Hi', 'Hi', 'Hi']

>>> ["Hi"]*(-5)
[]

>>> ["Hi"]*5+["everyone"]
['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'everyone']

>>> ["Hi"]*5+["everyone"]*2
['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'everyone', 'everyone']

>>> ["Hi"]*5+["everyone"]+2
Traceback (most recent call last):
File "", line 1, in
["Hi"]*5+["everyone"]+2
TypeError: can only concatenate list (not "int") to list

membership: tests if an object is an item of a list or not. Testing will be with in and not in operators.
>>> myList=['toast',[1,2,3],123,True,("a","b","c"),{1:None,2:None}]
>>> "toast" in myList
True
>>> ["toast"] in myList
False

>>> [1,2,3] in myList
True
>>> 'cack' not myList
SyntaxError: invalid syntax

>>> 'cack' not in myList
True

>>> ("a","b","c") in myList
True


Access to  list's items:
The same as strings, we use [] to access list's items.
>>> myList=['toast',[1,2,3],123,True,("a","b","c"),{1:None,2:None}]
>>> myList[0]
'toast'

>>> myList[1]
[1, 2, 3]

>>> myList[2]
123

>>> myList[9]
Traceback (most recent call last): File "", line 1, in myList[9] IndexError: list index out of range

>>> myList[-1]
{1: None, 2: None}

>>> myList[5]
{1: None, 2: None}

>>> myList[5]==myList[-1]
True

>>> myList[-2]
('a', 'b', 'c')


Slicing:
Slicing is used to access to a sublist of another list.

Attention!
If i=j, list[i] and list[j] return the same item or the item with the index j comes before the i's in the order, slicing returns an empty list []

list[i:j]: access to the sublist that begins from the item with index i(included) and ends at the item with index j(not included).
>>> myList[1:6]
[[1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[1:5]
[[1, 2, 3], 123, True, ('a', 'b', 'c')]

>>> myList[1:4]
[[1, 2, 3], 123, True]

>>> myList[0:6]
['toast', [1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}

>>> myList[1:2]
[[1, 2, 3]]

>>> myList[0:1]
['toast']

>>> myList[1:3]
[[1, 2, 3], 123]

>>> myList[2:5]
[123, True, ('a', 'b', 'c')]

>>> myList[4:3]
[]

>>> myList[-1:-3]
[]

list[:j]: access to the sublist that begins from the beginning and ends at the item with index j( as usual not included)
>>> myList=['toast',[1,2,3],123,True,("a","b","c"),{1:None,2:None}]
>>> myList[:1]
['toast']

>>> myList[:2]
['toast', [1, 2, 3]]

>>> myList[:3]
['toast', [1, 2, 3], 123]

>>> myList[:4]
['toast', [1, 2, 3], 123, True]

>>> myList[:5]
['toast', [1, 2, 3], 123, True, ('a', 'b', 'c')]
>>> myList[:6]

['toast', [1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[:-1]
['toast', [1, 2, 3], 123, True, ('a', 'b', 'c')]

>>> myList[:-2]
['toast', [1, 2, 3], 123, True]

>>> myList[:-3]
['toast', [1, 2, 3], 123]

>>> myList[:-4]
['toast', [1, 2, 3]]

>>> myList[:-5]
['toast']

list[i:]: begins from the item with the index i(included) and ends at the end.
>>> myList[1:]
[[1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[2:]
[123, True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[3:]
[True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[4:]

[('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[5:]
[{1: None, 2: None}]

>>> myList[-6:]
['toast', [1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}]


list[:]: access to the whole list.
>>> myList[:]
['toast', [1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[0:6]
['toast', [1, 2, 3], 123, True, ('a', 'b', 'c'), {1: None, 2: None}]

>>> myList[:]==myList[0:6]
True



Note:
Untill now, we recognized some built-in functions in Python like len(), the new one in this lesson is list(). It takes any iterable object as parameter and generats a list fron its items.
Note:
 **If were no given parameter list() returns an empty list.
**If the parameter were a dictionary list() returns a list from its keys. We'll discuss dictionaries later.
>>> list("Python")
['P', 'y', 't', 'h', 'o', 'n']

>>> list(["lion","tiger","elephent"],[1,2,3])
Traceback (most recent call last):
File "", line 1, in


>>>list((1,2,"Python"))
[1, 2, 'Python']


No comments:

Post a Comment

Pages