SlideShare a Scribd company logo
1 of 39
在 Python 中,萬物皆物件!
變數
• 放左邊是變數, 放右邊是型態
• Ex: y=5
• Golden = ‘teacher’…
• numbers = []
Data type
• 數值型態(Numeric type)
int, long, float, bool, complex
• 字串型態(String type)
str
• 容器型態(Container type)
list, set, dict, tuple
數值型態
• type(1)
• type(1L)
• type(11111111111111111111111111111)
• type(3.14)
數值型態
• type(True)
• type(3 + 4j)
• 2 ** 100
字串型態
• Python 中的字串不可變(Immutable)
> name = 'Justin'
>>> len(name)
6
>>> for ch in name:
... print ch
J
u
s
t
i
n
>>> 'Just' in name
True
>>> name + name
'JustinJustin'
>>> name * 3
'JustinJustinJustin'
>>>
字串型態
• Python 中的字串不可變(Immutable)
• 使用 ‘文字’ 或 ”文字”
字串型態
• Python 中的字串不可變(Immutable)
• 使用 ‘文字’ 或 ”文字”
• 可以使用 [] 來取得字串中的某個字元,
從 0 開始
>>> lang = 'Python'
>>> lang[0]
'P'
>>> lang[-1]
'n'
>>>
[] 也可以用來切割字串,例如:
>>> lang[1:5]
'ytho'
>>> lang[0:]
'Python'
>>> lang[:6]
'Python‘
>>> lang[0:6:2]
'Pto'
>>> lang[::-1]
'nohtyP'
>>>
如何反轉字串
字串格式化
>>> '{0} is {1}'.format('Justin', 'caterpillar')
'Justin is caterpillar'
>>> '{real} is {nick}'.format(real = 'Justin', nick =
'caterpillar')
'Justin is caterpillar'
>>> '{0} is {nick}'.format('Justin', nick = 'caterpillar')
'Justin is caterpillar'
容器型態
• list 型態
>>> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> ', '.join(['justin', 'caterpillar', 'openhome'])
'justin, caterpillar, openhome‘
>>> list('justin')
['j', 'u', 's', 't', 'i', 'n']
容器型態
• list 型態
• dict 型態
>>> passwords = {'Justin' : 123456, 'caterpillar' : 933933}
>>> passwords['Justin']
123456
>>> passwords['Hamimi'] = 970221 # 增加一對鍵值
>>> passwords
{'caterpillar': 933933, 'Hamimi': 970221, 'Justin': 123456}
容器型態
• list 型態
• dict 型態
• set 型態
>>> admins = {'Justin', 'caterpillar'} # 建立 set
>>> users = {'momor', 'hamini', 'Justin'}
>>> 'Justin' in admins
True
>>> admins & users
{'Justin'}
>>> admins | users
{'hamini', 'caterpillar', 'Justin', 'momor'}
>>> admins - users
{'caterpillar'}
容器型態
• list 型態
• dict 型態
• set 型態
• tuple 型態
if、for 與 while
If/else
Name = [L.Mei, L.Ming, L.What]
if L.Mei in Name:
print ('Hello, ‘ + Name[0])
else:
print ('Hello, Guest‘)
#這樣寫也可以,但不建議,沒有邏輯
from sys import argv
print 'Hello, ' + (Name[1] if L.Mei in Name else 'Guest')
有何不同,差異在哪,和兩者會顯示甚麼?
while (number == 0):
num = input('輸入四個數字:')
if (isallnumber(num) == False) :
print ('***錯誤:您輸入的不是數字,請重新輸入')
exit
elif (hasSameDigit(num) == True):
print ('***錯誤: 請輸入不同數字,請重新輸入')
elif (len(num) != 4) :
print ('***錯誤: 請輸入四個數字,請重新輸入')
exit
else:
number +=1
while (number == 0):
num = input('輸入四個數字:')
if (isallnumber(num) == False) :
print ('***錯誤:您輸入的不是數字,請重新輸入')
exit
if (hasSameDigit(num) == True):
print ('***錯誤: 請輸入不同數字,請重新輸入')
elif (len(num) != 4) :
print ('***錯誤: 請輸入四個數字,請重新輸入')
exit
else:
number +=1
for
輸入
計算
結果儲存
打開
視窗
For x in ‘spam’:
print x * 2
numbers = [10, 20, 30]
squares = []
for number in numbers:
squares.append(number ** 2)
print squares
#這樣寫也可以,但不建議,沒有邏輯
numbers = [10, 20, 30]
print [number ** 2 for number in numbers]
numbers = [11, 2, 45, 1, 6, 3, 7, 8, 9]
odd_numbers = []
for number in numbers:
if number % 2 != 0:
odd_numbers.append(number)
print odd_numbers
#這樣寫也可以,但不建議,沒有邏輯
numbers = [11, 2, 45, 1, 6, 3, 7, 8, 9]
print [number for number in numbers if number % 2 != 0]
巢層for迴圈
Items = [‘aaa’, 111, (4,5), 2.01]
tests = [(4,5), 3.14]
For key in tests:
For item in items:
if item == key:
print key, “was found”
else:
print key, “was not found”
lts = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for lt in lts:
for ele in lt:
print ele
for lt in lts:
print lt
for lt in lts:
for ele in lt:
print lt
for lt in lts:
for ele in lt:
print lts
while
Example
x = ‘handsome’
While x:
print x
x = x[1:]
num = []
number = 0
num = input('輸入四個數字:')
while (number == 0):
if (isallnumber(num) == False) :
print ('***錯誤:您輸入的不是數字,請重新輸入')
exit
elif (hasSameDigit(num) == True):
print ('***錯誤: 請輸入不同數字,請重新輸入')
elif (len(num) != 4) :
print ('***錯誤: 請輸入四個數字,請重新輸入')
exit
else:
number +=1
num = []
number = 0
while (number == 0):
num = input('輸入四個數字:')
if (isallnumber(num) == False) :
print ('***錯誤:您輸入的不是數字,請重新輸入')
exit
elif (hasSameDigit(num) == True):
print ('***錯誤: 請輸入不同數字,請重新輸入')
elif (len(num) != 4) :
print ('***錯誤: 請輸入四個數字,請重新輸入')
exit
else:
number +=1
break/continue/pass
• break:跳出所在迴圈
• continue:跳至最近所在迴圈頂端
• pass:什麼事也不做,只是空佔敘述
x = 10
While x >= 1:
x = x-1
if x % 2 != 0:
continue
print x
x = 10
While x >= 1:
x = x-1
if x % 2 != 0:
pass
print x
x = 10
While x >= 1:
x = x-1
if x % 2 != 0:
break
print x
python基礎教學

More Related Content

Viewers also liked

久保研究室とは
久保研究室とは久保研究室とは
久保研究室とはMikio Kubo
 
SMX London 2012 presentation - Understanding Google Penalties
SMX London 2012 presentation - Understanding Google PenaltiesSMX London 2012 presentation - Understanding Google Penalties
SMX London 2012 presentation - Understanding Google PenaltiesSimon Penson
 
ピカチュウによるPythonオブジェクト入門2
ピカチュウによるPythonオブジェクト入門2ピカチュウによるPythonオブジェクト入門2
ピカチュウによるPythonオブジェクト入門2Mikio Kubo
 
Wacode5thでのpython講義資料
Wacode5thでのpython講義資料Wacode5thでのpython講義資料
Wacode5thでのpython講義資料丈 宮本
 
Node boxで始めるジェネラティブ・アート#pyconapac
Node boxで始めるジェネラティブ・アート#pyconapacNode boxで始めるジェネラティブ・アート#pyconapac
Node boxで始めるジェネラティブ・アート#pyconapacshnmorimoto
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎juzihua1102
 
Android x 網路爬蟲
Android x 網路爬蟲Android x 網路爬蟲
Android x 網路爬蟲Engine Bai
 
160428 東工大「ロボット技術」授業資料
160428 東工大「ロボット技術」授業資料160428 東工大「ロボット技術」授業資料
160428 東工大「ロボット技術」授業資料openrtm
 
Python入門 コードリーディング - PyConJP2016
Python入門 コードリーディング - PyConJP2016Python入門 コードリーディング - PyConJP2016
Python入門 コードリーディング - PyConJP2016Shinya Okano
 
客戶數據分析四大難題一次解決: IBM 數據分析解決方案
客戶數據分析四大難題一次解決:  IBM 數據分析解決方案客戶數據分析四大難題一次解決:  IBM 數據分析解決方案
客戶數據分析四大難題一次解決: IBM 數據分析解決方案Randy Lin
 
巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據
巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據
巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據Yuan CHAO
 
PythonでDeepLearningを始めるよ
PythonでDeepLearningを始めるよPythonでDeepLearningを始めるよ
PythonでDeepLearningを始めるよTanaka Yuichi
 
Slideshare簡介
Slideshare簡介Slideshare簡介
Slideshare簡介Sports Kuo
 
優化宅的日常-數據分析篇
優化宅的日常-數據分析篇優化宅的日常-數據分析篇
優化宅的日常-數據分析篇Wanju Wang
 
知識を紡ぐための言語処理と、 そのための言語資源
知識を紡ぐための言語処理と、そのための言語資源知識を紡ぐための言語処理と、そのための言語資源
知識を紡ぐための言語処理と、 そのための言語資源Koji Matsuda
 
Pythonによる機械学習入門〜基礎からDeep Learningまで〜
Pythonによる機械学習入門〜基礎からDeep Learningまで〜Pythonによる機械学習入門〜基礎からDeep Learningまで〜
Pythonによる機械学習入門〜基礎からDeep Learningまで〜Yasutomo Kawanishi
 
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014Toomore
 

Viewers also liked (20)

Why python
Why pythonWhy python
Why python
 
久保研究室とは
久保研究室とは久保研究室とは
久保研究室とは
 
SMX London 2012 presentation - Understanding Google Penalties
SMX London 2012 presentation - Understanding Google PenaltiesSMX London 2012 presentation - Understanding Google Penalties
SMX London 2012 presentation - Understanding Google Penalties
 
ピカチュウによるPythonオブジェクト入門2
ピカチュウによるPythonオブジェクト入門2ピカチュウによるPythonオブジェクト入門2
ピカチュウによるPythonオブジェクト入門2
 
Hl20160929
Hl20160929Hl20160929
Hl20160929
 
Wacode5thでのpython講義資料
Wacode5thでのpython講義資料Wacode5thでのpython講義資料
Wacode5thでのpython講義資料
 
Node boxで始めるジェネラティブ・アート#pyconapac
Node boxで始めるジェネラティブ・アート#pyconapacNode boxで始めるジェネラティブ・アート#pyconapac
Node boxで始めるジェネラティブ・アート#pyconapac
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
Gurobi python
Gurobi pythonGurobi python
Gurobi python
 
Android x 網路爬蟲
Android x 網路爬蟲Android x 網路爬蟲
Android x 網路爬蟲
 
160428 東工大「ロボット技術」授業資料
160428 東工大「ロボット技術」授業資料160428 東工大「ロボット技術」授業資料
160428 東工大「ロボット技術」授業資料
 
Python入門 コードリーディング - PyConJP2016
Python入門 コードリーディング - PyConJP2016Python入門 コードリーディング - PyConJP2016
Python入門 コードリーディング - PyConJP2016
 
客戶數據分析四大難題一次解決: IBM 數據分析解決方案
客戶數據分析四大難題一次解決:  IBM 數據分析解決方案客戶數據分析四大難題一次解決:  IBM 數據分析解決方案
客戶數據分析四大難題一次解決: IBM 數據分析解決方案
 
巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據
巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據
巨量資料分析輕鬆上手_教您玩大強子對撞機公開數據
 
PythonでDeepLearningを始めるよ
PythonでDeepLearningを始めるよPythonでDeepLearningを始めるよ
PythonでDeepLearningを始めるよ
 
Slideshare簡介
Slideshare簡介Slideshare簡介
Slideshare簡介
 
優化宅的日常-數據分析篇
優化宅的日常-數據分析篇優化宅的日常-數據分析篇
優化宅的日常-數據分析篇
 
知識を紡ぐための言語処理と、 そのための言語資源
知識を紡ぐための言語処理と、そのための言語資源知識を紡ぐための言語処理と、そのための言語資源
知識を紡ぐための言語処理と、 そのための言語資源
 
Pythonによる機械学習入門〜基礎からDeep Learningまで〜
Pythonによる機械学習入門〜基礎からDeep Learningまで〜Pythonによる機械学習入門〜基礎からDeep Learningまで〜
Pythonによる機械学習入門〜基礎からDeep Learningまで〜
 
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
 

Similar to python基礎教學

Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Derek Lee
 
Python 入門
Python 入門 Python 入門
Python 入門 Andy Yao
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Derek Lee
 
C程式-陣列與指標
C程式-陣列與指標C程式-陣列與指標
C程式-陣列與指標艾鍗科技
 

Similar to python基礎教學 (7)

Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備
 
Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用 Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用
 
Python 入門
Python 入門 Python 入門
Python 入門
 
Ch 6
Ch 6Ch 6
Ch 6
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
 
C程式-陣列與指標
C程式-陣列與指標C程式-陣列與指標
C程式-陣列與指標
 
第5章数组
第5章数组第5章数组
第5章数组
 

python基礎教學

  • 2. 變數 • 放左邊是變數, 放右邊是型態 • Ex: y=5 • Golden = ‘teacher’… • numbers = []
  • 3. Data type • 數值型態(Numeric type) int, long, float, bool, complex • 字串型態(String type) str • 容器型態(Container type) list, set, dict, tuple
  • 4. 數值型態 • type(1) • type(1L) • type(11111111111111111111111111111) • type(3.14)
  • 7. > name = 'Justin' >>> len(name) 6 >>> for ch in name: ... print ch J u s t i n >>> 'Just' in name True >>> name + name 'JustinJustin' >>> name * 3 'JustinJustinJustin' >>>
  • 9. 字串型態 • Python 中的字串不可變(Immutable) • 使用 ‘文字’ 或 ”文字” • 可以使用 [] 來取得字串中的某個字元, 從 0 開始
  • 10. >>> lang = 'Python' >>> lang[0] 'P' >>> lang[-1] 'n' >>> [] 也可以用來切割字串,例如: >>> lang[1:5] 'ytho' >>> lang[0:] 'Python' >>> lang[:6] 'Python‘ >>> lang[0:6:2] 'Pto'
  • 12. 字串格式化 >>> '{0} is {1}'.format('Justin', 'caterpillar') 'Justin is caterpillar' >>> '{real} is {nick}'.format(real = 'Justin', nick = 'caterpillar') 'Justin is caterpillar' >>> '{0} is {nick}'.format('Justin', nick = 'caterpillar') 'Justin is caterpillar'
  • 14. >>> [0] * 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>> ', '.join(['justin', 'caterpillar', 'openhome']) 'justin, caterpillar, openhome‘ >>> list('justin') ['j', 'u', 's', 't', 'i', 'n']
  • 16. >>> passwords = {'Justin' : 123456, 'caterpillar' : 933933} >>> passwords['Justin'] 123456 >>> passwords['Hamimi'] = 970221 # 增加一對鍵值 >>> passwords {'caterpillar': 933933, 'Hamimi': 970221, 'Justin': 123456}
  • 17. 容器型態 • list 型態 • dict 型態 • set 型態
  • 18. >>> admins = {'Justin', 'caterpillar'} # 建立 set >>> users = {'momor', 'hamini', 'Justin'} >>> 'Justin' in admins True >>> admins & users {'Justin'} >>> admins | users {'hamini', 'caterpillar', 'Justin', 'momor'} >>> admins - users {'caterpillar'}
  • 19. 容器型態 • list 型態 • dict 型態 • set 型態 • tuple 型態
  • 22. Name = [L.Mei, L.Ming, L.What] if L.Mei in Name: print ('Hello, ‘ + Name[0]) else: print ('Hello, Guest‘) #這樣寫也可以,但不建議,沒有邏輯 from sys import argv print 'Hello, ' + (Name[1] if L.Mei in Name else 'Guest')
  • 24. while (number == 0): num = input('輸入四個數字:') if (isallnumber(num) == False) : print ('***錯誤:您輸入的不是數字,請重新輸入') exit elif (hasSameDigit(num) == True): print ('***錯誤: 請輸入不同數字,請重新輸入') elif (len(num) != 4) : print ('***錯誤: 請輸入四個數字,請重新輸入') exit else: number +=1
  • 25. while (number == 0): num = input('輸入四個數字:') if (isallnumber(num) == False) : print ('***錯誤:您輸入的不是數字,請重新輸入') exit if (hasSameDigit(num) == True): print ('***錯誤: 請輸入不同數字,請重新輸入') elif (len(num) != 4) : print ('***錯誤: 請輸入四個數字,請重新輸入') exit else: number +=1
  • 27. For x in ‘spam’: print x * 2
  • 28. numbers = [10, 20, 30] squares = [] for number in numbers: squares.append(number ** 2) print squares #這樣寫也可以,但不建議,沒有邏輯 numbers = [10, 20, 30] print [number ** 2 for number in numbers]
  • 29. numbers = [11, 2, 45, 1, 6, 3, 7, 8, 9] odd_numbers = [] for number in numbers: if number % 2 != 0: odd_numbers.append(number) print odd_numbers #這樣寫也可以,但不建議,沒有邏輯 numbers = [11, 2, 45, 1, 6, 3, 7, 8, 9] print [number for number in numbers if number % 2 != 0]
  • 30. 巢層for迴圈 Items = [‘aaa’, 111, (4,5), 2.01] tests = [(4,5), 3.14] For key in tests: For item in items: if item == key: print key, “was found” else: print key, “was not found”
  • 31. lts = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for lt in lts: for ele in lt: print ele for lt in lts: print lt for lt in lts: for ele in lt: print lt for lt in lts: for ele in lt: print lts
  • 32. while
  • 33. Example x = ‘handsome’ While x: print x x = x[1:]
  • 34.
  • 35. num = [] number = 0 num = input('輸入四個數字:') while (number == 0): if (isallnumber(num) == False) : print ('***錯誤:您輸入的不是數字,請重新輸入') exit elif (hasSameDigit(num) == True): print ('***錯誤: 請輸入不同數字,請重新輸入') elif (len(num) != 4) : print ('***錯誤: 請輸入四個數字,請重新輸入') exit else: number +=1
  • 36. num = [] number = 0 while (number == 0): num = input('輸入四個數字:') if (isallnumber(num) == False) : print ('***錯誤:您輸入的不是數字,請重新輸入') exit elif (hasSameDigit(num) == True): print ('***錯誤: 請輸入不同數字,請重新輸入') elif (len(num) != 4) : print ('***錯誤: 請輸入四個數字,請重新輸入') exit else: number +=1
  • 38. x = 10 While x >= 1: x = x-1 if x % 2 != 0: continue print x x = 10 While x >= 1: x = x-1 if x % 2 != 0: pass print x x = 10 While x >= 1: x = x-1 if x % 2 != 0: break print x