Professional Documents
Culture Documents
TA1107 - Jupyter Notebook
TA1107 - Jupyter Notebook
TA1107 - Jupyter Notebook
#第一格說明
print('-------第一格說明-------')
#x.items() #->returns a list of tuple
print(md.items()) #-> 印出來長這樣 dict_items([('a', 1)]) ,又 for i in md.items(): 代表 對這個list中的每個elements(即
#第二格答案
print('-------第二格答案-------')
#題目
md={'a':1}
for i in md.values():
print(type(i))
#第二格說明
#print('-------第二格說明-------')
#即獲得dictionary中所有的值
#第三格答案
print('-------第三格答案-------')
#題目
md={'a':1}
for i in md.keys():
print(type(i))
#第三格說明
#print('-------第三格說明-------')
#即獲得dictionary中所有的key
#第四格答案
print('-------第四格答案-------')
#題目
print(type(md))
#第四格說明
#print('-------第四格說明-------')
#md 就是一個dictionary
-------第一格答案-------
<class 'tuple'>
-------第一格說明-------
dict_items([('a', 1)])
-------第二格答案-------
<class 'int'>
-------第三格答案-------
<class 'str'>
-------第四格答案-------
<class 'dict'>
1.Set
Type of collection.
Does not allow repeated item.
Is unorderd.
Similar to list but have a curly brackets{大括號}
Similar to dictionary but don't have key
set()
<class 'set'>
1.2 List <-> Set, String -> List VS String -> Set
<class 'list'>
{1, 2, 3}
<class 'set'>
--------------
<class 'set'>
[1, 2, 3]
<class 'list'>
<class 'list'>
--------------------
<class 'set'>
{1, 2, 3, 4, 5}
{3, 4}
{3, 4}
{3, 4}
-------------------------
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
-------------------------
{1, 2}
{1, 2}
{5, 6}
{5, 6}
{1, 2, 3, 4}
In [14]: s1.add(5)
print(s1)
{1, 2, 3, 4, 5}
In [34]: s1.add('apple')
print(s1)
{1, 2, 3, 4, 5, 'apple'}
{1, 2, 3, 4, 5, 'apple'}
------------------------------------------
In [37]: s1.remove("apple")
print(s1)
In [38]: s1.remove(1)
print(s1)
2. Bool
Have only two Values: True, False
In [39]: print(type(False))
print(type(True))
<class 'bool'>
<class 'bool'>
In [17]: print(2>1)
print(type(2>1))
print('--------------')
print(2==1)
print(type(2==1))
True
<class 'bool'>
--------------
False
<class 'bool'>
3.File
---
-------------
<class 'str'>
In [27]: #help(open)
file=open('Example.txt','r')
print(file) #只顯示讀檔狀態
#dir(file)
Example.txt
This is line 1
This is line 2
This is line 3
<class 'str'>
In [27]: #Method2:
#a better way to read fille
with open('Example.txt', 'r') as file1:
file1content=file1.read()
print(file1content)
This is line 1
This is line 2
This is line 3
This is line 1
This is line 2
This is line 3
This
-------
is
-------
line 1
-------
This
This i
s line
This is
line
3.2.2 x.readline()
In [11]: #Method1
with open('Example.txt', 'r') as file:
print(file.readline())
This is line 1
In [12]: #Method2
file3=open('Example.txt', 'r')
file3.readline()
#VS next cell
This is line 1
3.2.3 x.readlines()
<class 'list'>
This is line 1
This is line 2
In [22]: file4[0]
In [34]: file.close()
In [44]: #case1
# fh=open("TA1110_1111_temp.txt", 'r') #亦可
path="TA1110_1111_temp.txt"
fh=open(path, 'r') #即使不寫'r'也可以,這個r電腦要讀取檔案
print(fh)
#VS next cell
In [53]: #case2
#fh2=open("TA1110_1111_temp.txt", 'r').read()
fh2=open(path).read() #-> return strings 這個read是要把他念出來的感覺
print(fh2) #為何有空行?因最後有換行符號
print(type(fh2))
#VS next cell
Hello world!
Goodbye world!
<class 'str'>
In [48]: #case 3
#fh3=open("TA1110_1111_temp.txt", 'r').readlines()
fh3=open(path).readlines() #-> return list
print(fh3)
print(type(fh3))
print(fh3[0]) #為何有空行?因最後有換行符號
print(fh3[1]) #為何有空行?因最後有換行符號
#VS next cell
<class 'list'>
Hello world!
Goodbye world!
In [56]: #case1
path="TA1110_1111_intro-short.txt"
fh=open(path, 'r') #即使不寫'r'也可以
print(fh)
In [57]: #case2
fh2=open(path).read() #->print strings
print(fh2)
print(type)
#亦可
#with open ('TA1110_1111_intro-short.txt') as c2_1:
#print(c2_1.read())
you know how to program you will figure out what you want
<class 'type'>
In [59]: #case 3
fh3=open(path).readlines() #->print list
print(fh3)
print(type(fh3))
print(fh3[0])
print(fh3[1])
#亦可
#with open ('TA1110_1111_intro-short.txt') as c3_2:
#print(c3_2.readlines())
['Why should you learn to write programs?\n', '\n', 'Writing programs (or programming) is a very creative \n',
'and rewarding activity. You can write programs for \n', 'many reasons, ranging from making your living to solv
ing\n', 'a difficult data analysis problem to having fun to helping\n', 'someone else solve a problem. This boo
k assumes that \n', 'everyone needs to know how to program, and that once \n', 'you know how to program you will
figure out what you want \n', 'to do with your newfound skills. \n']
<class 'list'>
In [ ]: #case3
with open ('TA1110_1111_intro-short.txt') as c3_3:
a=c3_3.readlines()
print(a)
# ['Why should you learn to write programs?\n', '\n', 'Writing programs (or programming) is a very creative
# Index 0 1 2
print('------------------------------------')
print(a[2]) #為何有空行?因最後有換行符號
print('------------------------------------')
print(a[1]) #為何是空行,因為是換行符號
#VS next cell
print('------------------------------------')
['Why should you learn to write programs?\n', '\n', 'Writing programs (or programming) is a very creative \n',
'and rewarding activity. You can write programs for \n', 'many reasons, ranging from making your living to solv
ing\n', 'a difficult data analysis problem to having fun to helping\n', 'someone else solve a problem. This boo
k assumes that \n', 'everyone needs to know how to program, and that once \n', 'you know how to program you will
figure out what you want \n', 'to do with your newfound skills. \n']
------------------------------------
------------------------------------
------------------------------------
Out[79]: '\n'
Hello world!
Goodbye world!
------------------------------------------------------------
Hello world!
Goodbye world!
------------------------------------------------------------
Hello world!
Goodbye world!
------------------------------------------------------------
you know how to program you will figure out what you want
In [47]: #運用字串格式化,增添點字
with open('TA1110_1111_temp.txt') as f1:
i=1
for sen in f1:
print('line %d' %i, sen) #舊式字串格式化
i=i+1
line 2
line 9 you know how to program you will figure out what you want
In [36]: #如果我不要空行
with open('TA1110_1111_intro-short.txt') as f1:
i=1
for sen in f1:
x=f'line {i}: {sen}' #新式字串格式化
print(x.rstrip()) #搭配這個,刪除換行,因為他是字串的尾吧空白
i=i+1
line 2:
line 9: you know how to program you will figure out what you want
End of Class
In [ ]: