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.
You could also creat a list with the built-in functionlist ()
like this:
Operations on lists:
Operations on lists are similar with strings'
concatination: joins two lists or more togother.
repition: repeats list's items a specified number of times.
membership: tests if an object is an item of a list or not. Testing will be within and not in operators.
Access to list's items:
The same as strings, we use [] to access list's items.
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).
list[:j]: access to the sublist that begins from the beginning and ends at the item with index j( as usual not included)
list[i:]: begins from the item with the index i(included) and ends at the end.
list[:]: access to the whole list.
Note:
Untill now, we recognized some built-in functions in Python likelen (), 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 parameterlist () returns an empty list.
**If the parameter were a dictionarylist () returns a list from its keys. We'll discuss dictionaries later.
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
[]
>>> 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((1,2,"Python"))
[1, 2, 'Python']
>>> list(("toast"))
['t', 'o', 'a', 's', 't']
[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
['Hello', 'world']
>>> [1]+[" "]+["Python"]
[1, ' ', 'Python']
>>> ["Hi"]+"everyone"
>>> ["Hi"]+1
File "
["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
['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
File "
["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
>>> 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
>>> "toast"
True
>>> ["toast"]
False
>>> [1,2,3]
True
>>> 'cack'
True
>>> ("a","b","c")
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')
>>> myList[0]
'toast'
>>> myList[1]
[1, 2, 3]
>>> myList[2]
123
>>> myList[9]
{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.
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]
[]
[[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']
>>> 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}]
[[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
['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
Note:
**If were no given parameter
**If the parameter were a dictionary
>>> 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']
['P', 'y', 't', 'h', 'o', 'n']
>>> list(["lion","tiger","elephent"],[1,2,3])
File "
>>>list((1,2,"Python"))
[1, 2, 'Python']
No comments:
Post a Comment