Professional Documents
Culture Documents
0. Warm up
In [1]: #Q1
x=3
y=2
#We want to get -> (3,2)
#----------------------------------------------------#
print(f'({x},{y})')
print('(%d,%d)'%(x,y))
print('({0},{1})'.format(x,y))
print('({first},{second})'.format(first=x,second=y))
(3,2)
(3,2)
(3,2)
(3,2)
In [9]: #Q2
#reverse list
data=[1,2,3,4,5,6,7,8,9,10]
#(1) We want to get -> [10,9,8,7,6,5,4,3,2,1]
#(2) We want to get -> [10,8,6,4,2]
#(3) We want to get -> [9,7,5,3,1]
#--------------------------------------------#
#Ans
# [1,2,3,4,5,6,7,8,9,10]
#Index 0 1 2 3 4 5 6 7 8 9
print(data[::-1])
print(data[9::-1])
print(data[-1::-1])
print('-----------------------------')
print(data[::-2])
print(data[9::-2])
print(data[-1::-2])
print('-----------------------------')
print(data[8::-2])
print(data[-2::-2])
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
-----------------------------
[10, 8, 6, 4, 2]
[10, 8, 6, 4, 2]
[10, 8, 6, 4, 2]
-----------------------------
[9, 7, 5, 3, 1]
[9, 7, 5, 3, 1]
[]
<class 'list'>
----------------------------------------------------------------------------------------------------------------
----------------------------------
()
<class 'tuple'>
-------1-------
()
<class 'tuple'>
-------2-------
(1,)
<class 'tuple'>
------2_2------
<class 'int'>
-------3-------
(1, 2, 3)
<class 'tuple'>
-------4-------
(1, 2, 3)
<class 'tuple'>
-------4_2-------
<class 'tuple'>
-------5-------
<class 'tuple'>
-------6-------
('a', 'b')
<class 'tuple'>
1.3 Concatenating
In [12]: x=("a", "b")
y=1,2
print(x+y)
print(x*3)
print(x*2+y*3)
('a', 'b', 1, 2)
In [13]: #mytup=1,2,3,4
#mytup[2]=-1 #TypeError: 'tuple' object does not support item assignment
mytup=1,2,3,4 #original
print(mytup)
#Change data type
mylist=list(mytup)
print(mylist)
#Change things inside
print("----------------")
mylist[2]=-2
print(mylist)
print("----------------")
mylist.append(5)
print(mylist)
print("----------------")
mylist.sort()
print(mylist)
print("----------------")
#Trun back to tuple
tupnew=tuple(mylist) #留意括號的差異
print(tupnew)
(1, 2, 3, 4)
[1, 2, 3, 4]
----------------
[1, 2, -2, 4]
----------------
[1, 2, -2, 4, 5]
----------------
[-2, 1, 2, 4, 5]
----------------
(-2, 1, 2, 4, 5)
True
True
-----
True
True
True
False
In [15]: #packing
family="father", "mother", "brother"
print(family)
print(type(family))
print('-------------------------------')
height=175,160,190
print(height)
print('-------------------------------')
#unpacking(lists, tuples)
f,m,b=height #因height有3個東西,我們插了三個旗子上去
print(f,m,b)
print('-------------------------------')
#改插旗子
f,m,b=b,m,f
print(f,m,b)
<class 'tuple'>
-------------------------------
-------------------------------
-------------------------------
1.7 Sorted
Original data does NOT change
In [16]: mytup=(60,3,7,1,8,20)
print(mytup)
print('--------------------')
print(sorted(mytup)) #小至大
#help(sorted) #We will get a list. #@@
print('--------------------')
print(sorted(mytup,reverse=True)) #大至小
print('--------------------')
print(tuple(reversed(mytup))) #反向印出,留意寫法
print('--------------------')
print(mytup) #以上均不改變原本的資料
print('--------------------')
#想保留改變後的樣子,另存為一個新的變數
t8=sorted(mytup)
print(t8)
print(mytup)
(60, 3, 7, 1, 8, 20)
--------------------
--------------------
[60, 20, 8, 7, 3, 1]
--------------------
(20, 8, 1, 7, 3, 60)
--------------------
(60, 3, 7, 1, 8, 20)
--------------------
(60, 3, 7, 1, 8, 20)
In [17]: #Method1
md=dict()
print(md)
print(type(md))
print("----")
#Method2
md1={}
print(md1)
print(type(md1))
{}
<class 'dict'>
----
{}
<class 'dict'>
--------------------------------------
1000
--------------------------------------
---------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------
dict_values([1, 2, 3, 4, 5])
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
--------------------------------------------------
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
--------------------------------------------------
[('e', 5), ('d', 4), ('c', 3), ('b', 2), ('a', 1)]
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
--------------------------------
--------------------------------
In [26]: help(md3.popitem) #-> Pairs are returned in LIFO (last-in, first-out) order.
#----------------------------------------------------------------#
md3={"a":1, "b":2, "c":3, "d":4, "e":5,} #original
print('----------------------------------------------------------------')
print(md3.popitem()) #pop the last one and show it
print('----------------------------------------------------------------')
print(md3) #Really changed
----------------------------------------------------------------
('e', 5)
----------------------------------------------------------------
2.5 In operator
Check if key is inside the dictionary
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------
{'school': 'NCCU', 'phone': '0937XXXXXX', 'birth': '1225', 'age': '25', 'height': '178'}
True
False
True
False
False
----------------------------------------------------------------------------------------
NCCU
----------------------------------------------------------------------------------------
None
NCCU
In [32]: #如找不到就顯示None
dic.get("weight", "None")
#vs next cell
Out[32]: 'None'
In [33]: #有找到就顯示找到的東西
dic.get("phone", "None")
Out[33]: '0937XXXXXX'
print('----------------------------------------------------------------------------------------')
#To get key and values
#Method1
for i in dic:
print(i, dic[i]) #i = key 不再是index了
print('----------------------------------------------------------------------------------------')
#Method2
print(dic.items()) #Let's check how it looks like
for k,v in dic.items():
print(k,v)
----------------------------------------------------------------------------------------
school
phone
birth
----------------------------------------------------------------------------------------
school NCCU
phone 0937XXXXXX
birth 1225
----------------------------------------------------------------------------------------
school NCCU
phone 0937XXXXXX
birth 1225
print('----------------')
#To sort by key and want reverse
for k,v in sorted(dic.items(), reverse=True):
print(k,v)
print('----------------')
#Even we change the order in print, it still sort by key
for k,v in sorted(dic.items()):
print(v,k)
birth 1225
phone 0937XXXXXX
school NCCU
----------------
school NCCU
phone 0937XXXXXX
birth 1225
----------------
1225 birth
0937XXXXXX phone
NCCU school
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
-------------------------------------------
c 2
a 4
b 22
-------------------------------------------
c 2
a 4
b 22
-------------------------------------------
a 4
b 22
c 2
-------------------------------------------
c 2
b 22
a 4
-------------------------------------------
4 a
22 b
2 c
-------------------------------
-------------------------------
3.1 in operater
In [39]: mystr="Taiwan"
mylist=[1,1,2,3]
print("T" in mystr)
print("an" in mystr)
print(1 in mylist)
print("t" in mystr)
print("T" not in mystr)
print(3 not in mylist)
print(3 in mylist)
True
True
True
False
False
False
True
3.2 +, * operator
Taiwan Taiwan
TaiwanTaiwan
[1, 1, 2, 3, 1, 1, 2, 3]
[1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3]
End of Class