0% found this document useful (0 votes)
13 views84 pages

ToPython Ori

Uploaded by

211 24林家任
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views84 pages

ToPython Ori

Uploaded by

211 24林家任
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

to

from C++
b98902029/r02944039 / 劉雨鑫

1
2
3
4
你是否想知道
眾多程式設計師口中優雅的語言

志玲姐姐

5
6
第一印象
Python 才有的東西

7
直譯 : 不用經過編譯就能執行
 .py 程式碼檔 (source file) 就是 執行檔 (executable file)

不過系統要先安裝好 python 環境

8
直譯 : 不用經過編譯就能執行
互動式命令列

[Link] 9
int 就支援大數

big_number.py 10
易讀多行字串 multi-line string
前後三個 單引號 或 雙引號

multiline_string.py 11
更易懂的條件式
兩種都可以喔

conditional_statement.py 12
即時反應錯誤
沒有給初始值
就拿來運算

把 str 當 int 用

error_report.py 13
支援中文變數

背 14
一行 http server
python -m [Link]

15
Python 簡介
歷史、哲學、應用

Chapter02_introduction
16
簡介
python 是體型一般較龐大的無毒蛇類
1989 年的聖誕節期間,吉多·范羅蘇姆為了
在阿姆斯特丹打發時間,決心開發一個新的腳
本解釋程式, python 就此誕生
目前版本 3.4.1 / 2.7.8
Python 的官方直譯器是 Cpython ,該直譯
器用 C 語言編寫,是一個由社群驅動的自由
軟體,目前由 Python 軟體基金會管理

吉多·范羅蘇姆
17
設計哲學

「優雅」、「明確」、「簡單」

高階語言(比起 Java 、 C++ 、 C )


18
TIOBE 程式語言排名

19
來源: [Link]
TIOBE 程式語言排名

20
應用範圍
以下這些都在內部大量地使用 Python

21
你學 python 可以…
開發任何程式
快速寫出範本程式;如果速度要快,再用 C 寫
強大的計算機
寫出跟虛擬碼 (pseudo code) 很像的 code
HP code wars
變成大蟒蛇
And so on…
22
計算機 vs 手算數學
好用 vs 考試用

23
環境安裝、設定
for windows7

Chapter03_setup
24
環境安裝、設定:概要

1.安裝 python 本體
2.安裝 notepad++
萬用編輯器
3.設定 notepad++
4.設定環境變數 PATH 命令列

25
1.安裝 python
[Link] 下載安裝檔
例: [Link]
下一步安裝法 :D

26
2. 安裝 notepad++ (optional)
[Link]
例: Notepad++ Installer 

依然下一步安裝法 :3

27
2+. 設定 notepad++ (recommend)
解決編碼問題,支援所有語言文字

28
2+. 設定 notepad++ (optional)
<space> 、 <tab> 是 python code 重要的一部

29
4. 設定環境變數 PATH (part1)
從 cmd 進入 Python 前,要先讓系統「認得」” python” 在哪,因此要設定
「 PATH 」

30
4. 設定環境變數 PATH (part2)
PATH 的值 (value) 設定為 python 安裝資料夾,目前是 C:\Python33

記得跟你資料
夾名稱一樣

31
HelloWorld
開始  cmd  python  print(“HelloWorld”)

32
互動式命令列
cmd 輸入 python 或 py ,進入 Python 的「互動式命令列」

離開互動式命令列:輸入 exit() 或 ctrl+Z

互動式命令列 就是 一個良好的 python 測試環境


也可以當作強大的計算機

33
執行程式:拖進 cmd
真正要寫程式,還是會將程式碼存成 .py 檔,再一口氣執行
(系統要先安裝 python ,才能直接執行 .py )

為了在程式遇到錯誤跳出時,仍可以看到完整的錯誤訊息
我們可以用拖曳的方式,在 cmd 裡執行 .py 檔
(建議路徑不要包括英文以外的字元)

34
Practice (optional)
1. 試著找一篇新聞儲存下來
2. 找 的 萬位數是多少
3. 算出
在 時的值

[Link] 35
I/O
input() / print()

36
基本輸出: print()

[Link]
37
小知識: help()
直接在 互動式命令列 查詢函式的用法

38
Python vs C :輸入的不同
In C :
scanf(“%d”,&a); scanf(“%f”,&a); scanf(“%s”,&a); 代表什麼?
a 是 int / float / string 的行為會一樣嗎?

其實 C 有幫我們把 輸入的 string 轉成 變數的型態

In Python :
python 的輸入 input()
input() 是一個 函式,有 回傳值 (就是 輸入的 string )
沒有幫我們把轉成任何 變數型態

39
基本輸入: input()
input() 以 行 為單位
就算 空白行 也是 行 使用者輸入的

[Link]
40
int
+
-
124 1246
*
整數 /

Chapter05_int
41
Python class ‘int’
int

__add__
+
__sub__
-
__mul__
*
__div__
__floordiv__ /

__mod__ //
__pow__ %
**
42
新增整數變數、四則運算
Elementary arithmetic

Elementary_arithmetic.py
43
小知識: python 的註解
單行註解: # 之後到換行為止都是註解(所以最多註解單行)
多行註解:利用多行字串 ””” … ”””

[Link]
44
指定運算子 =

Assignment_operator.py 45
變數交換
變數 A 要跟變數 B 交換, C++ 怎麼做?
In Python : A, B = B, A

[Link] 46
小知識:型態承受力

語言 python C/C++ C/C++


整 型態 int long long int

記憶體大小 不固定 8 byte 4 byte



數值大小 不固定

47
練習:銀行計息
deposit

[Link] 48
string
字串

‘H’ ‘a’ ‘t’ ‘s’ ‘u’ ‘n’ ‘e’ ‘M’ ‘I’ ‘k’ ‘u’
Chapter06_string
49
Python class ‘str’
str

__add__ +
__mul__
*
__len__
len( )
__getitem__
[]

50
新增字串變數、加乘運算
string operation

string_operations.py 51
易讀多行字串 multi-line string
前後三個 單引號 或 雙引號

multiline_string.py 52
藉由 input 得到 string
love announcement

love_anouncement.py 53
取得字串長度 len() 、取出其中一字 [ ]
對 ASCII 以外不友
完整支援中文 善

str_getitem.py / 54
跑一遍整個字串

怎麼做?

str_join.py 55
開始

讀取成績

成績 >=60

Control Flow
?

輸出”當 輸出”合
掉” 格”

程式流程控制: if 、 while 回報結果

結束

Chapter07_ControlFlow
56
何謂好的排版?
讓人看起來舒服,層次分明的就是好排版
秘訣:每個區塊都有一條神聖不可侵犯的對齊線

99_bad.cpp / [Link]
57
九九乘法表

[Link] / [Link]
58
流程差異對照表
保留字對照 區塊 0 區塊 0
python C/C++ 開頭 條件 列: if/while 開頭 ( 條件 ) 列 {
elif else if
區塊 1 區塊 1
True true
False false 開頭 條件 列: if/while 開頭 ( 條件 ) 列 {
and &&
or || 區塊 2 區塊 2
not !

區塊 1 }
使用 區塊 0 區塊 1
<space> 、 <t
ab> 對齊吧 }
區塊 0 59
L[0] L[1] L[2] L[3] L[4]

‘ 成功巴 265 15 20.8 ‘297’


士’

list
串列

Chapter08_list
60
Python 串列 (list) vs C/C++ 陣列 (array)
int A[5];
A[0] A[1] A[2] A[3] A[4]

綁定型態 22 265 208 297 15

長度固定

L = [ ‘ 成功巴士’ , 265, + [ 20.8, ‘297’ ]


15 ]L[0] L[1] L[2] L[3] L[4]

不綁型態
長度可變 ‘ 成功巴 265 15 20.8 ‘297’
士’

61
Python class ‘list’
像是 C++ 的陣列加強版:每一格可以塞任何種類的變數、長度可以任意延伸

list

__add__ +
__mul__
*
__len__
len( )
__getitem__
[]

62
功能都很直覺

len( ) [ ] + *

Q:如何做到 C++ 的 int a[50];


A: a = [0] * 50
list_op.py
63
取出子部分
C++ 當中,我們該怎麼印出陣列 A 第 3~7 格的元素?
for( int i=3 ; i<8 ; i++ ){
cout << A[i] << endl;

}

In Python
A[3:8]

64
取出子部分 [ : ]
[ a:b ] 可以讓我們取出 str 或 list 從 第 a( 包括 ) 個元素 開始到 第 b( 不包括 ) 個元素
的子部分 省略的話,
會幫你補上
開頭或結尾

65
key value

123 ‘ 自由
日’
‘ 雙十

dict

‘ 萌節
1010 ’

字典 ‘ 聖誕
1225
節’

Chapter09_dict
66
Python class ‘dict’
字典:以 int 、 str 等 hashable 的 key 儲存 沒有順序關係的 value

dict

__len__ len( )
__getitem__
[]

67
新增字典、取用
紅藍都是新增的方法

注意:
101 是 int
‘ 成功高中’
是 str
[Link]
68
del : 刪除元素
適用於 list 、 dict…

list

dict

[Link]
69
for :將所有的子元素拿出來一次
for 子元素 in 元素集合:
區塊 1

區塊 1

70
for :將所有的子元素拿出來一次
元素集合: str 、 list 、 dict… 拿出來的是
key
str list dict

順序難以捉模

照順序

[Link]
71
電表度數(傳入
值)

function 電費計算系統
(函式)

函式(函數)
電費(回傳值)

Chapter10_function
72
定義函式
回傳型態 函式名稱 ( 參數型態 1 參數 1, 參數型態 2 參數 2,
def 函式名稱 ( 參數 1, 參數 2, 參數 … ){
3… ) : do anything you want
do anything you want

return 回傳值 ;
return 回傳值 1, 回傳值 2…
}

沒錯!可以
回傳多個變數
73
費波那西數列: 1, 1, 2, 3, 5, 8,
13…

[Link]
74
小排序:多個回傳值

[Link]
75
Homework

Practice
76
hw1
輸入若干 n 個數字,輸出指定第 k 大的數字。如果 k=0 就結束程式。
Sample input: 6 8 15 29 35 47 77 3 2 4 1 0
Sample output:
第 3 大的數字是 35
第 2 大的數字是 47
第 4 大的數字是 29
第 1 大的數字是 77

Hint:
[Link]
[Link] we mentioned in class.

77
hw2
寫一個 python 程式來判斷輸入字串有沒有雙回文。
( 回文 : aba / a)( 輸入字串不會有空白 )
( 是不是兩個回文字串黏在一起 )
Sample input:abcbaacca
Sample output: yes, abcbaacca = abcba + acca

Sample input:abcbaca
Sample output:no, abcbaca is not a double palindrome

Hint:
[Link] a function to determine whether an input string is a palindrome .
[Link] we mentioned in class.
78
C 到 python 一眼看去
不用打 ;
scanf / printf 變成 input / print input / print 預設是以整行為單位
變數不用宣告
變數名稱沒有綁住型態
while / if 的 ( ) 變成 : 強迫你排版好看 !
while / if 的 { } 改為用排版
else if 變成 elif
true / false 變成 True / False
沒有 i++ / i-- 請用 i+=1 / i-=1
for 有別的用途

79
學測分發: function 也可當變數傳

ability_exam.py
80
math 函式庫
math 函式庫的用法都是 math. 函數名稱 ( 引數 ) ,使用上非常的簡單,唯一需
要注意的就是引入的型態必須要是正確的數值型態。
常用的函式有:開根號 [Link] 、三角函數 [Link], [Link]…… 、對數
[Link], math.log10……

81
參考資料與延伸學習
Python 官方教學, [Link]
Python 官方函式庫, [Link]
Mark Lutz 《 Python 學習手冊》
台大資訊營 python 講義 by 姜姜
OpenFoundry 活動, [Link]
雪凡與好朋友們的 Ren'Py 遊戲引擎初學心得提示
Codecademy , [Link]
Wiki 條目《 Python 》, [Link]
TIOBE , [Link]
黃佑仁 b98902112 成功高中實習中
Google “Python3 教學”、 “ Python3 tutorial” 、”雪凡與好朋友…”!
82
THE END
課堂有限,學海無涯。
自學才是王道!

83
Bonus: Linux 工作站的一些用法
Connention: Pietty
[Link]
Fileupload: Filezilla(Client)
[Link]
Information
[Link]

s[Link]
權限與資料夾命名
/.foldername/

84

You might also like