1.編碼問題
默認情況下,Python 3源碼文件以 UTF-8 編碼,所有字符串都是 unicode 字符串。 也可以為源碼文件指定不同的編碼,在文件頭部加上:
# coding=gbk
2.關鍵字
保留字即關鍵字,Python的標准庫提供了一個keyword module,可以輸出當前版本的所有關鍵字:
>>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
3.注釋
Python中單行注釋以#開頭,多行注釋用三個單引號(''')或者三個雙引號(""")將注釋括起來。
4.變量
Python中的變量不需要聲明。每個變量在使用前都必須賦值,變量賦值以后該變量才會被創建
Python 3支持int、float、bool、complex(復數)。
數值運算:
- Python可以同時為多個變量賦值,如a, b = 1, 2。
- 一個變量可以通過賦值指向不同類型的對象。
- 數值的除法(/)總是返回一個浮點數,要獲取整數使用//操作符。
- 在混合計算時,Python會把整型轉換成為浮點數。
字符串:
python中的字符串str用單引號(' ')或雙引號(" ")括起來,同時使用反斜杠(\)轉義特殊字符
字符串可以使用 + 運算符串連接在一起,或者用 * 運算符重復
1 text = 'ice'+' cream' 2 print(text) 3 4 text = 'ice cream '*3 5 print(text)
使用三引號('''...'''或"""...""")可以指定一個多行字符串
1 text = '''啦啦啦 2 喔呵呵呵呵 3 呵呵你妹''' 4 print(text) 5 text = 'ice\ 6 cream' 7 print(text)
如果不想讓反斜杠發生轉義,可以在字符串前面添加一個 r 或 R ,表示原始字符串。
如 r"this is a line with \n" 則\n會顯示,並不是換行
1 text1 = r'E:\notice' 2 text2 = "let's go!" 3 text3 = r'this is a line with \n' 4 print(text1) # E:\notice 5 print(text2) # let's go! 6 print(text3) # this is a line with \n
字符串有兩種索引方式,第一種是從左往右,從0開始依次增加;第二種是從右往左,從-1開始依次減少。
python中沒有單獨的字符類型,一個字符就是長度為1的字符串
1 text = 'ice cream' 2 print(len(text)) 3 4 print(text[0]) # i 5 print(text[-9]) # i 6 7 print(text[8]) # m 8 print(text[-1]) # m
python字符串不能被改變。向一個索引位置賦值會導致錯誤
1 text = 'ice cream' 2 text[0] = 't' # 報錯 3 print(text)
還可以對字符串進行切片,獲取一段子串。用冒號分隔兩個索引,形式為變量[頭下標:尾下標]。
截取的范圍是前閉后開的,並且兩個索引都可以省略:
1 text = 'ice cream' 2 print(text[:3]) # ice 3 print(text[4:9]) # cream 4 print(text[4:]) # cream
5.三目運算符
1 x = 100 2 y = 200 3 z = x if x > y else y 4 print(z) # 200
6.分支
if-else 語句與其他語言類似,不再贅述
if-elif-else 語句,相當於c或java語言中的if-else if-else :
1 while True: 2 score = int(input("Please input your score : ")) 3 if 90 <= score <= 100: 4 print('A') 5 elif score >= 80: 6 print('B') 7 elif score >= 70: 8 print('C') 9 elif score >= 60: 10 print('D') 11 else: 12 print('Your score is too low')
7.循環
while循環語句一般形式:
while 判斷條件:
statements
1 import random 2 3 print("hello world!\n") 4 number = random.randint(1, 10) 5 temp = input("Please input a number : ") 6 i = int(temp) 7 8 while i != number: 9 print("wrong...") 10 if i < number: 11 print("required a bigger number") 12 else: 13 print("required a smaller number") 14 temp = input("Please input a number : ") 15 i = int(temp) 16 17 print("yes...")
for循環的一般格式如下:
for <variable> in <sequence>:
<statements>
else:
<statements>
1 languaegs = ['C','c++','java','python'] 2 for language in languaegs: 3 print(language, len(language))
循環語句可以有else子句
它在窮盡列表(以for循環)或條件變為假(以while循環)循環終止時被執行
但循環被break終止時不執行.如下查尋質數的循環例子
1 for num in range(2, 10): 2 for x in range(2, num): 3 if num%x == 0: 4 print(num, 'equals', x, '*', num//x) 5 break 6 else: 7 # 循環中沒有找到元素 8 print(num, 'is a prime number')
如果需要遍歷數字序列,可以使用內置range()函數:
1 # range()函數,含頭不含尾 2 # 0~4 3 for i in range(5): 4 print(i) 5 6 # 2~7 7 for i in range(2, 8): 8 print(i) 9 10 # 1~9 步長為3 11 for i in range(1, 10, 3): 12 print(i)
range()函數與for循環結合:
1 languaegs = ['c','c++','java','python'] 2 for i in range(len(languaegs)): 3 print(i, ':', languaegs[i])