String
store char
data in sequence
char
: 1byte
1byte
= 8bit
= 256
converts bit
to char
or vice-versa according to rules (UTF-8, ASCII, etc)
int
= 4byte
= $-2^{31}\sim2^{31} - 1$
Slicing a[::]
1
2
3
4
5
6
| a = [1,2,3,4,5]
s = "hello jason"
print(a[::-1])
print(a[::3])
print(s[::-1])
|
[5, 4, 3, 2, 1]
[1, 4]
nosaj olleh
a.upper()
a.lower()
1
2
3
| a = 'xYzDfSFs'
print(a.upper())
print(a.lower())
|
XYZDFSFS
xyzdfsfs
a.capitalize()
: capitalize first char
1
2
| a = 'xYzDfSFs'
a.capitalize()
|
'Xyzdfsfs'
a.count('xyz')
: # of ‘abc’ in a
1
2
| a = 'xyzdfsfasfxyzxyz'
a.count('xyz')
|
3
a.find('xyz')
: return offset of ‘xyz’
1
2
| a = 'aaaxyzdfsfasfxyzxyz'
a.find('xyz')
|
3
a.startswith('xyz')
a.endswith('xyz')
: whether a starts/ends with ‘xyz’
1
2
3
| a = 'xyzdfsfasfxyzxyz'
print(a.startswith('xyz'))
print(a.endswith('xyz'))
|
True
True
r""
: raw string
1
2
3
4
| sentence = 'hello it\'s me'
raw_sentence = r'hello it\'s me'
print(i)
print(raw_sentence)
|
hello it's me
hello it\'s me
a.strip()
: remove space front/back
1
2
| a = " hello "
a.strip()
|
'hello'
copy.deepcopy()
1
2
3
4
5
6
| import copy
a = [[1,2,3],[4,5,6]]
b = copy.deepcopy(a)
b[0][0] = 100
print(a)
print(b)
|
[[1, 2, 3], [4, 5, 6]]
[[100, 2, 3], [4, 5, 6]]
Lab 1
1
| !wget https://raw.githubusercontent.com/TeamLab/introduction_to_python_TEAMLAB_MOOC/master/code/6/yesterday.txt
|
--2022-01-18 13:08:43-- https://raw.githubusercontent.com/TeamLab/introduction_to_python_TEAMLAB_MOOC/master/code/6/yesterday.txt
Resolving raw.githubusercontent.com... 185.199.108.133, 185.199.109.133, 185.199.111.133, ...
Connecting to raw.githubusercontent.com|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 638 [text/plain]
Saving to: 'yesterday.txt'
yesterday.txt 100%[===================>] 638 --.-KB/s in 0s
2022-01-18 13:08:44 (24.3 MB/s) - 'yesterday.txt' saved [638/638]
1
2
3
4
5
6
7
8
9
10
11
12
13
| lyrics = ""
with open('yesterday.txt', 'r') as f:
while True:
line = f.readline()
if not line:
break
lyrics += line.strip() + "\n"
print(lyrics)
print(lyrics.count('yesterday'))
print(lyrics.count('YESTERDAY'))
|
Yesterday, all my troubles seemed so far away
Now it looks as though they're here to stay
oh, I believe in yesterday
Suddenly, I'm not half the man I used to be
There's a shadow hanging over me
Oh, yesterday came suddenly.
Why she had to go?
I don't know, she wouldn't say
I said something wrong
Now I long for yesterday.
Yesterday love was such an easy game to play
Now I need a place to hide away
Oh, I believe in yesterday.
Why she had to go?
I don't know, she wouldn't say
I said something wrong
Now I long for yesterday.
Yesterday love was such an easy game to play
Now I need a place to hide away
Oh, I believe in yesterday...
6
0
Advanced Function Concepts
Call by Value
1
2
3
4
5
6
7
| def change(x):
x += 1
return x
x = 10
change(x)
print(x) # unchanged
|
10
Call by Object Reference
Address of object
is passed
1
2
3
4
5
6
7
| def modify(arr): # address of object s is passed as parameter
arr.append(1)
arr = [1,2,3]
s = [0]
modify(s)
print(s)
|
[0, 1]
Scoping
Use global
keyword to use global variable in function
1
2
3
4
5
6
7
8
| def tester():
global x # declare that we're using global variable x
x = 10 # access global variable
print(x)
x = 20
tester()
print(x) # changed!
|
10
10
Function type hints
1
2
3
4
5
| def test(x: int) -> str:
return str(x + 10)
print(test(10))
print(type(test(10)))
|
20
<class 'str'>
docstring
-> try out docstring extension in vscode!
1
2
3
4
5
6
7
8
9
10
11
12
| def add(a: int, b: int) -> int:
'''
return
- the sum of two floats
parameters
- a: int
- b: int
'''
return a + b
print(add(3,10))
|
13