"You can put single quotes ' in a string with double quotes on the outside"
"You can put single quotes ' in a string with double quotes on the outside"
Strings : An immutable sequence of unicode characters
"You can put single quotes ' in a string with double quotes on the outside"
"You can put single quotes ' in a string with double quotes on the outside"
'and visa versa (")'
'and visa versa (")'
print(
"""muti-line strings
with three quotes""")
muti-line strings
with three quotes
# 잘 알아둘것, 많이 사용
='KAIST MFE Program'
word# indexing
print(word[0],word[5],word[6],sep="/")
# slicing
# str[a:b:c] = start a, end b, jump c
# a is inclusive, b is exclusive, c can be minus
# default : a =0, b=n+1, c=1 but when c is minus then default : a=n, b=0(inclusive)
print(word[0:5],word[6:9],word[10:])
print(word[:2],word[-2:])
print(word[0:10:2])
print(word[::-1])
K/ /M
KAIST MFE Program
KA am
KITME
margorP EFM TSIAK
='Python strings are sliceable.'
text10] text[
'i'
=len(text)
length text[length]
IndexError: string index out of range
-1] text[length
'.'
='Hello'; b='Python'
aprint(a+b, a*2+b*3, 'H' in a, 'm' not in a)
HelloPython HelloHelloPythonPythonPython True True
'123'+1
TypeError: can only concatenate str (not "int") to str
'123'+str(1)
'1231'
int('123')+1
124
# len, .find, .count, .lower, .upper, .split, .replace, .starswith, .endswith
# .isalpha, .isnumeric, .isalnum
# len is function, others are methods
print(
word,len(word),
'I'),
word.find(' '),
word.count(
word.lower(),
word.upper(),' '),
word.split(' ','-'),
word.replace(='\n'
sep )
KAIST MFE Program
17
2
2
kaist mfe program
KAIST MFE PROGRAM
['KAIST', 'MFE', 'Program']
KAIST-MFE-Program
True
False
False
True
False
True
False
print(
'K'),
word.startswith('M'),
word.endswith(
word.isalpha(),' ','').isalpha(),
word.replace(
word.isnumeric(),'123'.isnumeric(),
word.isalnum(),' ','1').isalnum(),
word.replace(='\n'
sep )
True
False
False
True
False
True
False
True
# string = immutable, not possible to modify
# method or operations on string != modify but make new string
="ACGT"
sequence"A","G") sequence.replace(
'GCGT'
sequence
'ACGT'
id(sequence)
2053186997616
=sequence.replace("A","G")
sequence did(sequence)
2053183781744
# a mutable sequence of objects, important!!
# To create List object, use [] or list()
=[1,2,3,4]
x1=[1,1.0,1+0j,'one',None,True]
x2=[[1,2,3,4],[5,6,7],[8,9]] # nested list
x3# 리스트 안에는 무엇이든 올 수 있음
print(x1,x2,x3,sep='\n')
[1, 2, 3, 4]
[1, 1.0, (1+0j), 'one', None, True]
[[1, 2, 3, 4], [5, 6, 7], [8, 9]]
=list()
e1type(e1)
list
len(e1)
NameError: name 'e1' is not defined
list('cat')
['c', 'a', 't']
=('abc')
a_tuplelist(a_tuple)
['a', 'b', 'c']
# indexing -> original object, slicing -> make sub-list
=[5,2.3,'hello']
myListprint(
0],
myList[-1],
myList[-3]
myList[ )
5 hello 5
=[1,55.5,"Am I in a list?",True,"the end"]
many_typesprint(
2:4],
many_types[2:],
many_types[3],
many_types[:-4:-2],
many_types[-2:-5:-1],
many_types[-3],
many_types[:3:2],
many_types[:-2:2],
many_types[:='\n'
sep )
['Am I in a list?', True]
['Am I in a list?', True, 'the end']
[1, 55.5, 'Am I in a list?']
[55.5, 'Am I in a list?']
[True, 'Am I in a list?', 55.5]
[1, 55.5]
[1, 'Am I in a list?']
[1, 'Am I in a list?']
# Multidimensional lists can alse be indexed and sliced.
=[[1,2,3],[4,5,6],[7,8,9,0]]
x0] x[
[1, 2, 3]
0][1] x[
2
0][0:2] x[
[1, 2]
# List object is an immutable!!
=['red','blue','green','black','white']
colorListprint(id(colorList),colorList)
2665431628288 ['red', 'blue', 'green', 'black', 'white']
2]='yellow'
colorList[print(id(colorList),colorList)
2665431628288 ['red', 'blue', 'yellow', 'black', 'white']
2:4]=['gray','purple']
colorList[ colorList
['red', 'blue', 'gray', 'purple', 'white']
1:4]='r e d'
colorList[ colorList
['red', 'r', ' ', 'e', ' ', 'd', 'white']
1:]=['orange']
colorList[ colorList
['red', 'orange']
=[1,2,3]; L2=[4,5,6]; L3=[7,8,9,0]
L1print(
+L2+L3,
L1
[L1],+[L2]+[L3],
[L1]='\n') sep
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[[1, 2, 3]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 0]]
print(
*3,
L13*L1,
*3,
[L1]+L2]*3,
[L11,2,3] in [L1]+[L2],
[='\n'
sep )
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
True
# len, min, max, del x[slice]
# x.append(value), x.extend(list), x.remove(value), x.count(value),
# x.insert(index, value), x.index(value), x.sort(), x.reverse()
=[0,1,2,3,4,5,6,7,8,9]
xdel x[0] # 많이 사용
x
[1, 2, 3, 4, 5, 6, 7, 8, 9]
del x[:3]
x
[4, 5, 6, 7, 8, 9]
del x[:]
x
[]
=[0,1,2,3,4,5,6,7,8,9]
xlen(x)
10
# in place
x.reverse() x
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# not in place
sorted(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# in place
x.sort() x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
=[5,3,6,7]
pockets=pockets
pockets_copy1)
pockets_copy.append( pockets_copy
[5, 3, 6, 7, 1]
# pockets_copy is just labeling of pockets
pockets
[5, 3, 6, 7, 1]
# [:] makes copy of original list object
# At list [:] is copy, but at Array(numpy) [:] is view
=pockets[:]
pockets_realcopy1)
pockets_realcopy.append( pockets_realcopy
[5, 3, 6, 7, 1, 1]
pockets
[5, 3, 6, 7, 1]
=pockets.copy()
pockets_method1)
pockets_method.append( pockets_method
[5, 3, 6, 7, 1, 1]
pockets
[5, 3, 6, 7, 1]
=(12345,54321,'hello')
t t
(12345, 54321, 'hello')
# if it contains a single variables, should include comma!
=(2)
xtype(x)
int
=(2,)
xtype(x)
tuple
=1,2,3,'hello'
a a
(1, 2, 3, 'hello')
=a
w,x,y,zprint(w,x,y,z)
1 2 3 hello
=a
x,_,y,_print(x,y)
1 3
*y=a
x,print(x,y)
1 [2, 3, 'hello']
# can be indexed or sliced.
=(1,2,3,4,5,6,7,8,9)
xprint(
5],
x[-3],
x[3:7],
x[3:7:2],
x[='\n'
sep )
6
7
(4, 5, 6, 7)
(4, 6)
# Tuples are immutable : impossible to add/remove/replace elements in a tuple
=(12,34.56)
tup10]=100 tup1[
TypeError: 'tuple' object does not support item assignment
=([1,2],[3,4])
tup20][0]=100
tup2[ tup2
([100, 2], [3, 4])
=(1,2,3,4)
tup1='a','b','c'
tup2+tup2 tup1
(1, 2, 3, 4, 'a', 'b', 'c')
*3 tup1
(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
'a' in tup2
True
0:1]+tup2[1:] tup1[
(1, 'b', 'c')
0:1]+tup2[1] tup1[
TypeError: can only concatenate tuple (not "str") to tuple
# concatenation
=tup1[1::2]+(6,)
tup_even tup_even
(2, 4, 6)
# len, max, min, x.index, x.count
=('lama','sheep','lama',48)
animalslen(animals)
4
print(
'lama'),
animals.index('sheep'),
animals.count(='\n') sep
0
1
# to fix tuple, tuple -> list -> tuple
=list(x)
x_list
x_list.reverse()=tuple(x_list)
x x
(9, 8, 7, 6, 5, 4, 3, 2, 1)
# tuple is faster than list!
%timeit x=[1,2,3,4,5]
%timeit x=(1,2,3,4,5)
47.3 ns ± 0.743 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
10.2 ns ± 0.0818 ns per loop (mean ± std. dev. of 7 runs, 100,000,000 loops each)
type({})
dict
# two-value sequenced can convert to dict easy!
=[(1,2),(3,4),(5,6)]
lotdict(lot)
{1: 2, 3: 4, 5: 6}
=['a1','b2','c3']
losdict(los)
{'a': '1', 'b': '2', 'c': '3'}
dict(a=1,b=2,c=3)
{'a': 1, 'b': 2, 'c': 3}
="abc"
adict(a=1,b=2,c=3)
{'a': 1, 'b': 2, 'c': 3}
=dict(Name='Zara', Age=7, Class='First')
dict1 dict1
{'Name': 'Zara', 'Age': 7, 'Class': 'First'}
'Name'] dict1[
'Zara'
'Age']=8
dict1['School']='ABC School'
dict1[ dict1
{'Name': 'Zara', 'Age': 8, 'Class': 'First', 'School': 'ABC School'}
='Town'
a='Downtown'
dict1[a] dict1
{'Name': 'Zara',
'Age': 8,
'Class': 'First',
'School': 'ABC School',
'Town': 'Downtown'}
# Check only key value!!
print(
'Name' in dict1,
'Age' in dict1,
'Country' in dict1,
'Country' not in dict1,
'Zara' in dict1,
='\n'
sep )
True
True
False
True
False
# len, del x[key]
# x.clear(), x.items(), x.keys(), x.values(), x.update(dict2)
print(dict1, len(dict1), sep='\n')
{'Name': 'Zara', 'Age': 8, 'Class': 'First', 'School': 'ABC School', 'Town': 'Downtown'}
5
del dict1['Town']
dict1
{'Name': 'Zara', 'Age': 8, 'Class': 'First', 'School': 'ABC School'}
dict1.clear() dict1
{}
del dict1
dict1
NameError: name 'dict1' is not defined
=dict(a=1,b=2,c=3)
dict1 dict1.keys()
dict_keys(['a', 'b', 'c'])
list(dict1.keys())
['a', 'b', 'c']
dict1.values()
dict_values([1, 2, 3])
list(dict1.values())
[1, 2, 3]
dict1.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
=dict(apples=1,oranges=2,pears=2)
d2=dict(pears=4,grapes=5,lemons=6)
ud
d2.update(ud) d2
{'apples': 1, 'oranges': 2, 'pears': 4, 'grapes': 5, 'lemons': 6}
dict1
{'a': 1, 'b': 2, 'c': 3}
=dict1.copy()
dict2
dict2.clear() dict1
{'a': 1, 'b': 2, 'c': 3}
=dict1
dict2
dict2.clear() dict1
{}
# elements of set should be immutable!
=[[1,2],[3,4]]
list1set(list1)
TypeError: unhashable type: 'list'
=[(1,2),(3,4)]
list2set(list2)
{(1, 2), (3, 4)}
# if elements are not unique, automatically remove duplicates
=[1,2,2,3,4]
list3set(list3)
{1, 2, 3, 4}
=dict(a=1,b=2,c=3)
dict1set(dict1)
{'a', 'b', 'c'}
set(dict1.keys())
{'a', 'b', 'c'}
set(dict1.values())
{1, 2, 3}
set(dict1.items())
{('a', 1), ('b', 2), ('c', 3)}
import copy as cp
=dict(a=1,b=2,c=[1,2,3])
A=A.copy()
B=cp.deepcopy(A)
C
'a']=9
B['c'][0]=9
B[print(A,B,C,sep='\n')
{'a': 1, 'b': 2, 'c': [9, 2, 3]}
{'a': 9, 'b': 2, 'c': [9, 2, 3]}
{'a': 1, 'b': 2, 'c': [1, 2, 3]}