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用單引號(' ')或雙引號(" ")括起來,同時使用反斜槓(\)轉義特殊字元
字串可以使用 + 運算子串連線在一起,或者用 * 運算子重複
text = 'ice'+' cream'
print(text) text = 'ice cream '*3
print(text)
使用三引號('''...'''或"""...""")可以指定一個多行字串
text = '''啦啦啦
喔呵呵呵呵
呵呵你妹'''
print(text)
text = 'ice\
cream'
print(text)
如果不想讓反斜槓發生轉義,可以在字串前面新增一個 r 或 R ,表示原始字串。
如 r"this is a line with \n" 則\n會顯示,並不是換行
text1 = r'E:\notice'
text2 = "let's go!"
text3 = r'this is a line with \n'
print(text1) # E:\notice
print(text2) # let's go!
print(text3) # this is a line with \n
字串有兩種索引方式,第一種是從左往右,從0開始依次增加;第二種是從右往左,從-1開始依次減少。
python中沒有單獨的字元型別,一個字元就是長度為1的字串
text = 'ice cream'
print(len(text)) print(text[0]) # i
print(text[-9]) # i print(text[8]) # m
print(text[-1]) # m
python字串不能被改變。向一個索引位置賦值會導致錯誤
text = 'ice cream'
text[0] = 't' # 報錯
print(text)
還可以對字串進行切片,獲取一段子串。用冒號分隔兩個索引,形式為變數[頭下標:尾下標]。
擷取的範圍是前閉後開的,並且兩個索引都可以省略:
text = 'ice cream'
print(text[:3]) # ice
print(text[4:9]) # cream
print(text[4:]) # cream
5.三目運算子
x = 100
y = 200
z = x if x > y else y
print(z) #
6.分支
if-else 語句與其他語言類似,不再贅述
if-elif-else 語句,相當於c或java語言中的if-else if-else :
while True:
score = int(input("Please input your score : "))
if 90 <= score <= 100:
print('A')
elif score >= 80:
print('B')
elif score >= 70:
print('C')
elif score >= 60:
print('D')
else:
print('Your score is too low')
7.迴圈
while迴圈語句一般形式:
while 判斷條件:
statements
import random print("hello world!\n")
number = random.randint(1, 10)
temp = input("Please input a number : ")
i = int(temp) while i != number:
print("wrong...")
if i < number:
print("required a bigger number")
else:
print("required a smaller number")
temp = input("Please input a number : ")
i = int(temp) print("yes...")
for迴圈的一般格式如下:
for <variable> in <sequence>:
<statements>
else:
<statements>
languaegs = ['C','c++','java','python']
for language in languaegs:
print(language, len(language))
迴圈語句可以有else子句
它在窮盡列表(以for迴圈)或條件變為假(以while迴圈)迴圈終止時被執行
但迴圈被break終止時不執行.如下查尋質數的迴圈例子
for num in range(2, 10):
for x in range(2, num):
if num%x == 0:
print(num, 'equals', x, '*', num//x)
break
else:
# 迴圈中沒有找到元素
print(num, 'is a prime number')
如果需要遍歷數字序列,可以使用內建range()函式:
# range()函式,含頭不含尾
# 0~4
for i in range(5):
print(i) # 2~7
for i in range(2, 8):
print(i) # 1~9 步長為3
for i in range(1, 10, 3):
print(i)
range()函式與for迴圈結合:
languaegs = ['c','c++','java','python']
for i in range(len(languaegs)):
print(i, ':', languaegs[i])