03.python.04.stringslistsdictionaries
03.python.04.stringslistsdictionaries
strings
definition
index/position
0 1 2 3 4 5 6 7 8 9 10
t h i s i s i t !
seq[1]
seq[-1]
seq[-2]
strings
index/position
0 1 2 3 4 5 6 7 8 9 10
t h i s i s i t !
index/position
0 1 2 3 4 5 6 7 8 9 10
t h i s i s i t !
slice seq[1]
seq[-1]
seq[-2]
seq[0:3]
seq[3:]
seq[:5]
strings
index/position
0 1 2 3 4 5 6 7 8 9 10
t h i s i s i t !
index/position
0 1 2 3 4 5 6 7 8 9 10
t h i s i s i t !
in all calls that return a string result, the result is a new string (because strings are immutable)
string methods
immutable
'This Is A Test'
string methods
immutable
'This Is A Test'
print(seq)
string methods
immutable
'This Is A Test'
print(seq)
'this is a test'
string methods
immutable
'This Is A Test'
strings
operations
• similarity:
• sequences of elements and characters, same operators,
same access type (by index)
• differences:
• strings are sequences of characters (homogeneous),
lists are sequences of any type of elements
• lists are mutable, strings are immutable
sequence operations
strings, lists, tuples, bytes and bytearray
lst.append(el) lst.count(el)
lst.reverse() lst.clear()
lst.index(el[,i[,j]]) lst.copy()
lst.insert(pos, el)
lst = [1, 2, 3]
print(lst)
list methods
immutable
lst = [1, 2, 3]
print(lst)
[1, 2, 3]
list methods
immutable
lst = [1, 2, 3]
print(lst)
[1, 2, 3]
lstdup = lst.copy()
lst.append(6)
print(lst, lstdup)
list methods
immutable
lst = [1, 2, 3]
print(lst)
[1, 2, 3]
lstdup = lst.copy()
lst.append(6)
print(lst, lstdup)
[1, 2, 3, 6] [1, 2, 3]
list methods
immutable
lst = [1, 2, 3]
print(lst)
[1, 2, 3]
lstdupagain = lst
lst.append(6)
print(lst, lstdupagain)
list methods
immutable
lst = [1, 2, 3]
print(lst)
[1, 2, 3]
lstdupagain = lst
lst.append(6)
print(lst, lstdupagain)
[1, 2, 3, 6] [1, 2, 3, 6]
list of items from a string
values = seq.split(",")
print(values)
list of items from a string
values = seq.split(",")
print(values)
['1', ' 3', ' -3', ' 0', ' 4', ' 20', ' -1']
list of items from a string
values_str = seq.split(",")
values_int = []
for val in values_str:
values_int.append(int(val))
print(values_int)
list of items from a string
values_str = seq.split(",")
values_int = []
for val in values_str:
values_int.append(int(val))
print(values_int)
[1, 3, -3, 0, 4, 20, -1]
list of items from a string
values_str = seq.split(",")
values_int = [int(val) for val in values_str]
print(values_int)
list of items from a string
values_str = seq.split(",")
values_int = [int(val) for val in values_str]
print(values_int)
[1, 3, -3, 0, 4, 20, -1]
string <-> lists
print(lst)
string <-> lists
print(lst)
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ',
's', 't', 'r', 'i', 'n', 'g']
string <-> lists
lst = [1, 2, 3, 4]
lststr = [str(x) for x in lst]
print(lststr)
string <-> lists
lst = [1, 2, 3, 4]
lststr = [str(x) for x in lst]
print(lststr)
['1', '2', '3', '4']
string <-> lists
lst = [1, 2, 3, 4]
lststr = [str(x) for x in lst]
print(lststr)
['1', '2', '3', '4']
seq = "".join(lststr)
print(seq)
string <-> lists
lst = [1, 2, 3, 4]
lststr = [str(elem) for elem in lst]
print(lststr)
['1', '2', '3', '4']
seq = "".join(lststr)
print(seq)
'1234'
string <-> lists
lst = [1, 2, 3, 4]
lststr = [str(elem) for elem in lst]
print(lststr)
['1', '2', '3', '4']
seq = "".join(lststr)
print(seq)
'1234' seq = "".join(str(elem) for elem in lst]
dictionaries
dictionary
definition
d.items() d.popitem()
d.copy()
d.update(dx) k in d
for k in d