標籤:lin 編譯 utf8 put style bin 演算法 習題 使用
1.python檔案命名
- 尾碼名可以是任意的,但為規範便於識別,尾碼名應為 .py
2.兩種執行方式
python解譯器 py檔案路徑 python 進入解譯器: 即時輸入並擷取到執行結果
3.解譯器路徑
在Linux系統中應添加 #!/user/bin/env python , windows系統中可不添加
4.編碼
# -*- coding:utf8 -*- (在python3中可不加,python只要出現中文頭部必須加)
ascill 只能編譯英文
unicode 萬國編碼,至少16個位元組
utf-8 能用多少位元組表示就用多少表示
5.變數名
由字母,數字,底線組成
PS: 數字不能開頭,不能是關鍵字,最好不和python內建的東西重複
6.條件陳述式
縮排用4個空格
a. input : 永遠等待,知道使用者輸入了值,就會將輸入的值賦值給一個東西
b. n1 = “liu” 單等號是賦值
n1 ==“liu” 雙等號是比較
c. if 條件1:
pass
elif 條件2:
pass
elif 條件3:
pass
print("end")
每個條件依次判斷
d. and 表示和,or 表示或
if n1 =="liu" and n2 =="xue"
print("OK")
else:
print("OK")
PS : pass代指空代碼,無意義,表示過
7.基礎資料型別 (Elementary Data Type)
字串: n1 = "liu" n2 = "xue" 引號之中的東西稱為字串
數字: age=19 weight=60 height=178
演算法:
字串:
加法:
n1 = "liu" n2 = "xue" n3 = n1+n2
#n3=="liuxue"
乘法:
n1 = 1 n2 = n1*10
數字:
n1 = 5 n2 = 2
n3 = n1+n2
n3 = n1-n2
n3 = n1*n2
n3 = n1 / n2
n3 = n1 % n2 (取餘)
n3 = n1 ** n2 (乘方)
8.迴圈
死迴圈
while 1==1:
print("OK")
9.練習題
1.使用while迴圈輸入 1 2 3 4 5 6 8 9 10
count = 1while count <= 10 : num = count if count == 7 : pass else: print(num) count += 1
2.求1-100的所有數的和
n = 1s = 0while n <= 100: s = s + n n += 1print(s)
3、輸出 1-100 內的所有奇數
count = 1while count <= 100 : if count%2 == 0: pass else: print(count) count += 1
4、輸出 1-100 內的所有偶數
count = 1while count <= 100 : if count%2 == 0: print(count) else: pass count += 1
5、求1-2+3-4+5 ... 99的所有數的和
n = 1s = 0while n < 100: num = n % 2 if num == 0: s = s - n else: s = s + n n = n + 1print(s)
6、使用者登陸(三次機會重試)
count = 0while count < 3: name1 = input("name:") passwd1 = input("passwd:") count += 1
Python成長之路 第一篇 《Python基礎》