Lập trình Python
Lập trình Python
2
Python Data Type – NUMERIC (1)
3
Python Data Type – NUMERIC (2)
● Not to declare datatype; just assign values in a variable
● Use type() to see what type of numerical value is it holding
>>> a = 10
>>> type(a)
<class 'int'>
>>> b = 1.234
>>> type(b)
<class 'float'>
>>> c = 5 + 6j
>>> type(c)
<class 'complex'>
>>> d = 1.123456789123456
4
Python Data Type – STRING (1)
5
Python Data Type – STRING (2)
6
Python Data Type – STRING (3)
>>> a = "79"
>>> type(a)
<class 'str'>
>>> b = int(a)
>>> type(b)
<class 'int'>
>>> f = float('1.5e-3')
>>> f
0.0015
>>> print(f)
0.0015
>>> eval('79-39')
40
7
Python Data Type – STRING (4)
>>> a = "Part 1" >>> s[2:-1]
>>> b = "and part 2" 'cd'
>>> a + ' ' + b >>> s
'Part 1 and part 2' 'abcde'
>>> s = a * 2 >>> s[:3]
>>> print(s) 'abc'
Part 1Part 1 >>> s[2:5]
>>> s="abcde" 'cde'
>>> s[0] >>> s[:3]
'a' 'abc'
>>> s[1:4] >>> s[2:]
'bcd' 'cde'
>>> s[-1] >>> s[1:4:2]
'e' 'bd' 8
Python Data Type – STRING (5)
>>> print(s)
abcde
>>> len(s)
5
>>> 'p' in s
False
>>> 'c' in s
True
>>> 'cd' in s
True
>>> s[1] = 'B'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = 'A' + s[1:]
>>> print(s)
Abcde 9
Python Data Type – STRING (6)
>>> s = 'a string, with stuff'
>>> s.count('st')
2
>>> s.find('stu')
15
>>> s.split(' ')
['a', 'string,', 'with', 'stuff']
>>> three = '3'
>>> three.isdigit()
True
>>> supper = s.upper()
>>> supper
'A STRING, WITH STUFF'
>>> s.rjust(30)
' a string, with stuff'
>>> "newlines\n\n\n".strip()
'newlines' 10
Python Data Type – LIST (1)
13
Python Data Type – LIST (4)
>>> r = [1, 2.0, 3, 5]
>>> r.append('thing')
>>> r
[1, 2.0, 3, 5, 'thing']
>>> r.append(['another','list'])
>>> r
[1, 2.0, 3, 5, 'thing', ['another', 'list']]
>>> r = [1, 2.0, 3, 5]
>>> r.extend(['item', 'another'])
>>> r
[1, 2.0, 3, 5, 'item', 'another']
>>> k = r.pop()
>>> k
'another'
>>> r
[1, 2.0, 3, 5, 'item']
14
Python Data Type – LIST (5)
>>> r = [2, 5, -1, 0, 20]
>>> r.sort()
>>> r
[-1, 0, 2, 5, 20]
>>> w = ['apa', '1', '2', '1234']
>>> w.sort()
>>> w
['1', '1234', '2', 'apa']
>>> w.reverse()
>>> w
['apa', '2', '1234', '1']
>>> v = w[:]
>>> v.reverse()
>>> v
['1', '1234', '2', 'apa']
>>> w
['apa', '2', '1234', '1'] 15
Python Data Type – LIST (6)
>>> s = 'biovitrum'
>>> w = list(s)
>>> w
['b', 'i', 'o', 'v', 'i', 't', 'r', 'u', 'm']
>>> w.reverse()
>>> w
['m', 'u', 'r', 't', 'i', 'v', 'o', 'i', 'b']
>>> r =''.join(w)
>>> r
'murtivoib'
>>> d = '-'.join(w)
>>> d
'm-u-r-t-i-v-o-i-b'
>>> s = 'a few words'
>>> w = s.split()
>>> w
['a', 'few', 'words'] 16
Python Data Type – TUPLES (1)
17
Python Data Type – TUPLES (2)
>>> t = (1, 3, 2)
>>> t[1]
3
>>> (a, b, c) = t
>>> a
1
>>> a, b, c
(1, 3, 2)
>>> a, b = b, a
>>> a, b
(3, 1)
>>> r = list(t)
>>> r
[1, 3, 2]
>>> tuple(r)
(1, 3, 2)
18
Python Data Type – DICTIONARY (1)
19
Python Data Type – DICTIONARY (2)
>>> h = {'key':12,'nyckel':'word'}
>>> h['key']
12
>>> 'nyckel' in h
True
>>> h['Per'] = 'Kraulis'
>>> h
{'key': 12, 'nyckel': 'word', 'Per': 'Kraulis'}
>>> h['nyckel'] = 'Kraulis'
>>> h
{'key': 12, 'nyckel': 'Kraulis', 'Per': 'Kraulis'}
>>> del h['nyckel']
>>> h
{'key': 12, 'Per': 'Kraulis'} 20
Python Data Type – DICTIONARY (3)
>>> h = {'key':12,'nyckel':'word'}
>>> 'Per' in h
False
>>> h['Per']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Per'
>>> h.get('Per','unknown')
'unknown'
>>> h.get('key','unknown')
12
>>> h.keys()
dict_keys(['key', 'nyckel'])
>>> h.values()
dict_values([12, 'word'])
>>> len(h)
2 21
Python Data Type – DICTIONARY (4)
>>> h = {'key':12,'nyckel':'word'}
>>> g = h.copy()
>>> del h['key']
>>> h
{'nyckel': 'word'}
>>> g
{'key': 12, 'nyckel': 'word'}
>>> h['Per'] = 'Johansson'
>>> h
{'nyckel': 'word', 'Per': 'Johansson'}
>>> h.update(g)
>>> h
{'nyckel': 'word', 'Per': 'Johansson', 'key': 12}
22
Questions and Answers
23