SlideShare a Scribd company logo
Python 入門
ABOUT ME
Ѳ 姚韋辰 (黑楓)
Ѳ 北科大資工二
Ѳ 北科程式設計研究社 1st 社長
Ѳ T.T.S. App 開發者
Ѳ 專長
Ѳ Android App 開發
歡迎來加我臉書好友找我聊天~OωO
安裝方法
Windows
前往 python.org/downloads ,下載最新版本的Python 3;並完成安裝。
務必勾選「Add Python 3.x to PATH」
macOS
前往 python.org/downloads ,下載最新版本的Python 3;並完成安裝。
Linux
• Ubuntu 14.04+ 已內建 Python
• 檢查現有版本:python3 --version
• 更新 Python:sudo apt-get upgrade python3
• 無內建的安裝方式:sudo apt-get install python3
開始之前
為什麼要學 Python?
Python 優點
• 易學
• 簡潔且美觀
• 跨平台
• 不須編譯,可直接執行
• 第三方函式庫眾多
• 數字沒有範圍限制 ( 取決於記憶體 )
Python 常見用途
• 機器學習
• 數據處理
• 科學計算
• 自然語言處理
• Web 應用
• 圖像辨識
Python 2 vs Python 3
Why use python 3?
執行 Python
• 使用互動式命令列
• 直接印出結果
• 將程式寫成檔案,再由python執行
• 以 .py 為副檔名
使用 互動式命令列
• OSX:開啟 terminal,輸入 python 或 python3
• Linux:開啟 terminal,輸入 python 或 python3
• Windows:開啟 cmd,輸入 python 或 python3
• 如果顯示 python2,需另外安裝 python3
基本資料型態
• 整數 ex:42、4848、-88
• 浮點數 ( 小數點的數字或指數 ) ex:3.14159、2.0e3
• 字串 ( 一串文字 ) ex:‘I have an apple’
• 布林 ( 是與非 ) ex:True、False
整數 (int) 與 浮點數 (float)
• 不要將 0 放在最前面或分母 ex:05、5/0
• 可容納任何大小的數字,不會產生溢位
• 0b、0o、0x 開頭分別代表二、八、十六進制
字串 (String)
# 單行字串 (Python 將單引號與雙引號一視同仁)
'Bang' # 單引號
“Bang!" # 雙引號
# 多行字串
'''A triple qouted string
can span multiple lines
like this
'''
"""Bang!!!"""
布林 (Boolean)
True #成立
False #不成立
關係運算
== # 相等
!= # 不相等
> # 大於
< # 小於
>= # 大於等於
<= # 小於等於
10 > x > 1
>>> 8 > 5 # 大於
True
>>> 1 <= 5 # 小於等於
True
>>> 5 == 4 # 等於
False
>>> 5 != 5 # 不等於
False
邏輯運算
1. not
2. and
3. or
not False # True
True and False # False
True or False # True
type()
>>> type('14') # 有單引號或者雙引號 => 字串(String)
<class 'str'>
>>> type(14.4) # 有小數點且沒有引號 => 浮點數(float)
<class 'float'>
>>> type(14) # 無小數點及引號 => 整數(int)
<class 'int'>
>>> type(True) # True 與 False,且無引號 => 布林
<class 'bool'>
>>> 2.0e2 # 2.0 * (10 * 10)
200.0
算術運算子
+x ; -y # 正負
abs(x) # 絕對值
x + y ; x – y # 加減
x * y ; x / y # 乘除
x // y # 除法(捨去小數位)
x % y # 餘數
x ** y # 次方
>>> 8 + 5 # 加法
13
>>> 8 - 5 # 減法
3
>>> 8 * 5 # 乘法
40
>>> 8 / 5 # 除法
1.6
>>> 8 // 5 # 除法 ( 無條件捨去小數 )
1
>>> 8 % 5 # 餘數
3
型態轉換
int('500') # 500
str(87) # '87'
float(1) # 1.0
int('ff', 16) # 255
int('z', 36) # 35
字串操作
字串中的數學
'abcd' + 'efg' # 'abcdefg'
'www' * 3 # 'wwwwwwwww'
字串格式化
'{}, {}, {}'.format('a', 'b', 'c’) # 'a, b, c'
'{0}{1}{0}'.format('o', 'w') # 'owo'
'{}{}O'.format('O', 'w') # 'OwO'
字串擷取
str = '你好,世界'
str[1] # '好'
str[4] # '界'
str[1:4] # '好,世'
str[0:5:2] # '你,界'
str[開始 : 結束 : 間隔]
字串分割
str = '你好f**k世界'
str.split('f**k') # ['你好', '世界']
字串長度
str = '你好,世界'
len(str) # 5
字串把玩
str = 'hello, i am Andy Yao'
str.capitalize() # 'Hello, i am andy yao'
str.title() # 'Hello, I Am Andy Yao'
str.upper() # 'HELLO, I AM ANDY YAO'
str.lower() # 'hello, i am andy yao'
註解
電腦會在程式執行的時候,自動忽略掉註解之後的所有文字
通常用於解釋程式碼,以便理解
# 這是註解
""" 這是字串,同時也是註解,使用三個雙引號 ""“
"""
這是字串,同時也是註解,使用三個雙引號,可以跨行註解。
用在字串上可以保留縮排、空格,也可以拿來使用,如輸出 print()
"""
變數
變數是一個容器的稱呼,與其容
器中的內容並無絕對關係。
a = 1
b = 2
c = a / b
# c == 0.5
c = c + 1
# c == 1.5
a, b = b, a
# a == 2 and b == 1
a = 8 # a -> 8
b = 2 # b -> 2
a = b # a -> 2, b -> 2
b = a # a -> 2, b -> 2
>>> a = 8
>>> b = 5
>>> a + b
13
>>> a - b
3
>>> a * b
40
>>> a / b
1.6
輸出
print('Hello world')
# Hello world
print(948794, '狂')
# 948794 狂
輸入
a = input()
# _
s = input('請輸入密碼:')
# 請輸入密碼:_
進階資料型態
• 串列 (List):可容納任意資料型態的清單
• 元組 (Tuple):不可改變的串列
• 字典 (Dictionary):一組 key 對應一組 value 的多組資料
• 集合 (Set):類似數學上的集合
List
List 是有順序的清單,具有
索引特性,長度可以變動,可
容納其他型態
>>> list = [1, 2] # 創建一個 list,給定初始值
>>> list.append(3) # 在 list 末端新增一個 3
>>> list.append('three') # 在 list 末端新增一個 'three'
>>> list # 印出 list 的所有物件
[1, 2, 3, 'three']
>>> list.pop() # 取出 list 末端的物件
'three'
>>> list
[1, 2, 3]
>>> list.remove(2) # 移除特定物件
>>> list
[1, 3]
>>> list.insert(0, 'three') # 在 0 的位置插入 'three'
>>> list
['three', 1]
字典 (dictionary)
dict = {'one': 1, 'two': 2, 'three': 3}
dict # {'one': 1, 'two': 2, 'three': 3}
dict['two'] # 2
dict['two'] = 'II'
dict # {'one': 1, 'two': 'II', 'three': 3}
dict['two'] # 'II'
條件式
if 18.5 <= BMI < 24 :
print('正常’)
縮排建議為四個空格
if 條件:
成立時執行的程式片段
......
elif 條件:
上面的 if 不成立且條件成立時執行的程式片段
......
else:
上面都不成立時執行的程式片段
......
if score >= 90: # 分數大於等於 90
print('A級')
elif score >= 80 and score < 90: # 分數介於 90 ~ 80
print('B級')
elif score >= 70 and score < 80: # 分數介於 80 ~ 70
print('C級')
elif score >= 60 and score < 70: # 分數介於 70 ~ 60
print('D級')
else: # 分數小於 60
print('不及格')
迴圈
• for 迴圈
• while 迴圈
While 迴圈
a = 0
while a < 5 :
print(a, end=' ')
a += 1
# 0 1 2 3 4
For 迴圈
five = [1, 2, 3, 4, 5]
for i in five :
print(i)
# 1
# 2
# 3
# 4
# 5
Range
for i in range(10) :
print(i)
# 0
# 1
# ...
# 9
type(range(1)) # <class 'range'>
list(range(10)) # [0, 1, ..., 9]
list(range(3, 19)) # [3, 4, ..., 18]
list(range(10, -1, -2)) # [10, 8, ..., 0]
函數
def add3(x) :
return x + 3
m = add3(2) # m == 5
def add7(y=0) : # y=0 代表如果沒給定參數時,預設為0
return y + 7
n = add7() # n == 7
p = add7(10) # p == 17
錯誤處理
#捕捉錯誤
try:
score = input('請輸入成績:')
score = int(score)
# 當 try 區塊捕捉到 ValueError 錯誤時,執行此區塊
except ValueError:
print('輸入格式錯誤')
ValueError
number = int(input("請輸入數字:"))
print(number / 2)
# number 應為數字
# 但當傳入非數字的符號時
# 會丟出 ValueError
當型別與值不符時,會丟出 ValueError 的錯誤
例外處理
補充資訊
補充網站
所有錯誤列表
注意須知
1. Python 沒有分號
2. Python 以換行和縮排代替大括弧 “{}”
小試身手
計算 BMI
如 BMI 大於 24 ,顯示過重
低於18.5,顯示過輕
基本解法
height = int(input('請輸入身高(cm):'))
weight = int(input('請輸入體重(kg):'))
height = height / 100
bmi = weight / height**2
print('你的BMI為{}:'.format(bmi, end=''))
if bmi >= 24:
print('好像有點過重,為了健康,最好多運動唷~~')
elif bmi > 18.5:
print('維持的不錯嘛,差我一點')
else:
print('輕到快被風吹走啦,還不多吃一點~!')
重複執行
while(True):
height = int(input('請輸入身高(cm):'))
weight = int(input('請輸入體重(kg):'))
height = height / 100
bmi = weight / height**2
print('你的BMI為{}:'.format(bmi, end=''))
if bmi >= 24:
print('好像有點過重,為了健康,最好多運動唷~~')
elif bmi > 18.5:
print('維持的不錯嘛,差我一點')
else:
print('輕到快被風吹走啦,還不多吃一點~!')
if input('是否繼續(Y/N):').upper() != 'Y':
break
處理錯誤輸入
while(True):
try:
height = int(input('請輸入身高(cm):'))
weight = int(input('請輸入體重(kg):'))
except ValueError:
print("輸入格式錯誤")
continue
height = height / 100
bmi = weight / height**2
print('你的BMI為{}:'.format(bmi, end=''))
if bmi >= 24:
print('好像有點過重,為了健康,最好多運動唷~~')
elif bmi > 18.5:
print('維持的不錯嘛,差我一點')
else:
print('輕到快被風吹走啦,還不多吃一點~!')
if input('是否繼續(Y/N):').upper() != 'Y':
break
工商時間
SITCON 學生計算機年會
TDOHacker 駭客地下城
ACM 競賽培訓

More Related Content

PPTX
Ethical hacking - Footprinting.pptx
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
PPTX
Building an Empire with PowerShell
PDF
MK Keamanan Komputer - Sesi 1 : Introduction
PDF
用 Keras 玩 Machine Learning
PDF
Pandas
PPT
Networking and penetration testing
PDF
Cehv8 - Module 02: footprinting and reconnaissance.
Ethical hacking - Footprinting.pptx
Algorithm And analysis Lecture 03& 04-time complexity.
Building an Empire with PowerShell
MK Keamanan Komputer - Sesi 1 : Introduction
用 Keras 玩 Machine Learning
Pandas
Networking and penetration testing
Cehv8 - Module 02: footprinting and reconnaissance.

What's hot (20)

PDF
Footprinting
PDF
How fun of privilege escalation Red Pill2017
PDF
Intermediate code generation
PPTX
Memory Forensics for IR - Leveraging Volatility to Hunt Advanced Actors
PPTX
NETWORK PENETRATION TESTING
PDF
MK Keamanan Komputer - Sesi 5 : Keamanan Internet
PPTX
Develop a Defect Prevention Strategy—or Else!
PPTX
Pen Testing Explained
PDF
Machine learning in software testing
PPTX
Data Analytics Life Cycle [EMC² - Data Science and Big data analytics]
PPTX
Kheirkhabarov24052017_phdays7
PDF
CNIT 123 8: Desktop and Server OS Vulnerabilities
PPTX
Malware ppt final.pptx
PDF
Code generation in Compiler Design
PDF
手把手教你 R 語言分析實務
PPTX
CYS Project Presentation on using johnny and john the ripper
PPTX
LL(1) parsing
PPTX
Penetration testing reporting and methodology
PPTX
Microsoft Secure Score Demo
PPTX
Data Wrangling
Footprinting
How fun of privilege escalation Red Pill2017
Intermediate code generation
Memory Forensics for IR - Leveraging Volatility to Hunt Advanced Actors
NETWORK PENETRATION TESTING
MK Keamanan Komputer - Sesi 5 : Keamanan Internet
Develop a Defect Prevention Strategy—or Else!
Pen Testing Explained
Machine learning in software testing
Data Analytics Life Cycle [EMC² - Data Science and Big data analytics]
Kheirkhabarov24052017_phdays7
CNIT 123 8: Desktop and Server OS Vulnerabilities
Malware ppt final.pptx
Code generation in Compiler Design
手把手教你 R 語言分析實務
CYS Project Presentation on using johnny and john the ripper
LL(1) parsing
Penetration testing reporting and methodology
Microsoft Secure Score Demo
Data Wrangling
Ad

Similar to Python 入門 (20)

PDF
Python基本資料運算
PPTX
ncuma_型別與迴圈.pptx
PDF
Python 2 - 快速簡介
PDF
Python basic - v01
PDF
Ppt 1-50
PPTX
Y3CDS - Python class 01
PPTX
Python入門:5大概念初心者必備 2021/11/18
PPTX
ncuma_邏輯與迴圈.pptx
PPTX
Python攻略
ODP
Op 20090411
PPTX
python基礎教學
PDF
Python程式設計 - 基本資料運算
PPT
Python Basic
PDF
講義
PDF
Python變數與資料運算
PDF
Python系列2
PDF
PDF
Ppt 26-50
PDF
Programming python - part 1
PDF
型態與運算子
Python基本資料運算
ncuma_型別與迴圈.pptx
Python 2 - 快速簡介
Python basic - v01
Ppt 1-50
Y3CDS - Python class 01
Python入門:5大概念初心者必備 2021/11/18
ncuma_邏輯與迴圈.pptx
Python攻略
Op 20090411
python基礎教學
Python程式設計 - 基本資料運算
Python Basic
講義
Python變數與資料運算
Python系列2
Ppt 26-50
Programming python - part 1
型態與運算子
Ad

Recently uploaded (20)

PPTX
3分钟读懂索尔福德大学毕业证Salford毕业证学历认证
PPTX
《HSK标准教程4下》第15课课件new.pptx HSK chapter 15 pptx
PPTX
3分钟读懂福特汉姆大学毕业证Fordham毕业证学历认证
PDF
黑客技术,安全提分不是梦!我们采用最新的数据破解和隐藏技术,精准定位并修改你的成绩,同时采用深度隐藏技术确保你的操作不被发现。价格实惠,流程快速,事后无痕...
PPTX
3分钟读懂滑铁卢大学毕业证Waterloo毕业证学历认证
PPTX
3分钟读懂纽曼大学毕业证Newman毕业证学历认证
PPTX
3分钟读懂诺里奇艺术大学毕业证NUA毕业证学历认证
PPTX
3分钟读懂拉夫堡大学毕业证LU毕业证学历认证
PDF
黑客出手,分数我有!安全可靠的技术支持,让你的GPA瞬间提升,留学之路更加顺畅!【微信:viphuzhao】
PPTX
3分钟读懂贵湖大学毕业证U of G毕业证学历认证
PPTX
3分钟读懂伦敦商学院毕业证LBS毕业证学历认证
PPTX
ONU and OLT from Baudcom Jenny training PPT
PPTX
3分钟读懂加州大学欧文分校毕业证UCI毕业证学历认证
PPTX
3分钟读懂肯塔基大学毕业证UK毕业证学历认证
PPTX
3分钟读懂圣安德鲁斯大学毕业证StAnd毕业证学历认证
PPTX
3分钟读懂皇家艺术学院毕业证RCA毕业证学历认证
PPTX
3分钟读懂曼彻斯特大学毕业证UoM毕业证学历认证
PPTX
3分钟读懂渥太华大学毕业证UO毕业证学历认证
PPTX
3分钟读懂南威尔士大学毕业证UCB毕业证学历认证
PPTX
3分钟读懂利物浦约翰摩尔大学毕业证LJMU毕业证学历认证
3分钟读懂索尔福德大学毕业证Salford毕业证学历认证
《HSK标准教程4下》第15课课件new.pptx HSK chapter 15 pptx
3分钟读懂福特汉姆大学毕业证Fordham毕业证学历认证
黑客技术,安全提分不是梦!我们采用最新的数据破解和隐藏技术,精准定位并修改你的成绩,同时采用深度隐藏技术确保你的操作不被发现。价格实惠,流程快速,事后无痕...
3分钟读懂滑铁卢大学毕业证Waterloo毕业证学历认证
3分钟读懂纽曼大学毕业证Newman毕业证学历认证
3分钟读懂诺里奇艺术大学毕业证NUA毕业证学历认证
3分钟读懂拉夫堡大学毕业证LU毕业证学历认证
黑客出手,分数我有!安全可靠的技术支持,让你的GPA瞬间提升,留学之路更加顺畅!【微信:viphuzhao】
3分钟读懂贵湖大学毕业证U of G毕业证学历认证
3分钟读懂伦敦商学院毕业证LBS毕业证学历认证
ONU and OLT from Baudcom Jenny training PPT
3分钟读懂加州大学欧文分校毕业证UCI毕业证学历认证
3分钟读懂肯塔基大学毕业证UK毕业证学历认证
3分钟读懂圣安德鲁斯大学毕业证StAnd毕业证学历认证
3分钟读懂皇家艺术学院毕业证RCA毕业证学历认证
3分钟读懂曼彻斯特大学毕业证UoM毕业证学历认证
3分钟读懂渥太华大学毕业证UO毕业证学历认证
3分钟读懂南威尔士大学毕业证UCB毕业证学历认证
3分钟读懂利物浦约翰摩尔大学毕业证LJMU毕业证学历认证

Python 入門