Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- 1. 註解
- '''
- # 註解的文字
- '''
- 2. 延續多行
- '''
- s = 'test \
- test'
- s
- '''
- 3. 條件判斷式 if, elif, else
- 句尾使用冒號「:」表示程式尚未結束
- 條件判斷式內的內容需縮排(tab)
- x = 4
- if x == 1:
- print("對")
- elif x == 2:
- print("不對")
- elif x ==3:
- print(3)
- else:
- print("其他")
- '''
- 4. range函數
- '''
- range(10)
- range(5,10)
- range(1,10,2)
- '''
- 5. For loop
- 句尾使用冒號「:」表示程式尚未結束
- 迴圈內的內容需縮排(tab)
- i為自行定義的變數名稱,不會用到該變數時,可打底線_
- '''
- # for i in range(5,12,2):
- for i in range(5):
- print('hello')
- '''
- 6. while loop
- while (condition is True) 的時候執行
- 使用冒號「 :」
- 迴圈內的內容需縮排(tab)
- '''
- i = 1
- while i<5:
- print('hello')
- i += 1
- '''
- 7. 迴圈控制
- 終止迴圈 break
- 終止該圈迴圈 continue
- 不做任何事(佔位語句)pass
- '''
- s = 'Python'
- for letter in s:
- if letter == 'o':
- break
- print(letter)
- for letter in s:
- if letter == 'o':
- continue
- print(letter)
- for letter in s:
- if letter == 'o':
- pass
- print(letter)
- '''
- 8. 定義函式
- '''
- def minus(x,y):
- print(x-y)
- minus(1,2)
- #請定義一個可以求出平均數的函式
- def mean(x,y):
- '''
- 9. 異常處理
- try, except, else
- '''
- try:
- num = eval(input('請輸入一個數字'))
- print('你輸入的數字是:',num)
- except:
- print('你輸入的不是數字!')
- else:
- print('很乖喔~')
Advertisement
Add Comment
Please, Sign In to add comment