標籤:nis ffffff bsp 輸入 *** 練習題 glob 練習 lambda
day.1
a.Python基礎
1.基礎
print(‘hello world‘)
2.運行環境
#檔案內部:# #!/usr/bin/env python#檔案名稱:# ./2.py#Linux環境下才能運行##在py3環境不用加,py2頭部必須加==>環境內預設使用ASCII解釋,所以中文解釋不通## -*- coding:utf8 -*-
3.變數==>可以理解為未知數x(大霧)
注意:變數只能由字母、數字、底線組成。
特例:1.不能以數字開頭
2.python關鍵字不能使用:[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
3.最好不要和Python內建的東西重複***
4.條件陳述式 ① if ==> else
1 """格式如下 2 if(怎麼怎麼怎麼): 3 處理什麼什麼 4 else:(else可以省略) 5 處理什麼什麼 6 """ 7 #例如# 8 password = input(請輸入密碼:) 9 if(password = 123456): #預設密碼為123456# #注意if和else後有個冒號#10 print("密碼正確")11 else:12 print("密碼錯誤")
② if ==> elif
1 user_name = input("請輸入登入賬戶")2 if("user_name == administrator"):3 print("歡迎您,超級管理員")4 elif("user_name == 禁花")5 print("歡迎您,禁花")6 elif("user_name == guest")7 print("歡迎您,來賓")8 else:9 print("請註冊新賬戶或者使用來賓賬戶登陸")
③ while ==> break & continue
1 """2 格式如下3 while True4 處理怎麼怎麼5 迴圈直到條件不為真。6 """7 #break強行打斷迴圈#8 #continue退出當前迴圈,繼續下次迴圈#
練習題:
1、使用while迴圈輸入 1 2 3 4 5 6 8 9 10
1 count = 12 while (count <= 10):3 if(count == 7):4 count = count + 15 continue6 else:7 print (count)8 count = count + 1
練習題1
2、求1-100的所有數的和
1 count = 12 a1 = 03 while (count <= 100):4 a1 = a1 + count5 count = count + 16 print(a1)
練習題2
3、輸出 1-100 內的所有奇數
1 count = 12 while(count <= 100):3 if (count % 2 == 1):4 print(count)5 else:6 pass7 count = count + 1
練習題3
4、輸出 1-100 內的所有偶數
(參考練習題3)
5、求1-2+3-4+5 ... 99的所有數的和
1 count = 12 a1 = 03 while(count<100):4 if(count % 2 == 1):5 a1 = a1 + count6 else:7 a1 = a1 - count8 count = count + 19 print(a1)
練習題5
6、使用者登陸(三次機會重試)(輸對提示密碼正確、輸錯提示密碼錯誤、錯誤3次後提示明天再登陸)
1 count = 4 2 while(count > 0): 3 passkey = input(‘請輸入密碼:‘) 4 if(passkey == "123456"): 5 print("密碼正確") 6 break 7 else: 8 print("密碼錯誤,剩餘次數為") 9 count = count - 110 print(count)11 if(count == 0):12 print("請明天重試")13 else:14 pass
練習題6
python入門第一篇