好幾天沒寫blog了,今天整理整理。寫blog一定要堅持下去。 Python解釋器 #!/usr/bin/env python #-*-coding:utf-8-*- # 無效的內容,只做註釋用(單行註釋) """ """ 多行註釋(三引號) 例: """ 123 123 """ 1.創建xxx.p ...
好幾天沒寫blog了,今天整理整理。寫blog一定要堅持下去。 Python解釋器 #!/usr/bin/env python #-*-coding:utf-8-*- # 無效的內容,只做註釋用(單行註釋) """ """ 多行註釋(三引號) 例: """ 123 123 """ 1.創建xxx.py文件(不要有中文路徑) #!/usr/bin/env python #-*-coding:utf-8-*- (- 是 gang) 2.寫代碼 a.頭部兩行 b.寫功能代碼 3.執行代碼 a.打開終端 cmd b.Python代碼文件的路徑 raw_input 用於和用戶交換,等待輸入內容 raw_input("111:") py2.7 raw_input('111') py3 input('>>>') 變數 il代指某個“東西” il = raw_input("111:") 變數名: 數字,字母,下劃線(_) (只能用這些) alex = 123 sb = "alex" a_lex = "sb" 不能以數字開頭 例:1alex 變數不能是Python內部的關鍵字 基本的數據類型: 數字:123 age = 18 字元串: al = "asdf" al = 'ffa' al = """sad21a""" "都是英文的引號 布爾值: Ture/False a4 = True a5 = False 條件: 一個等號是賦值 = 兩個符號是比較 == !=表示不等於 if條件: ——》內容一 縮進必須用 ——》內容二 (四個空格) else: ——》內容三 ——》內容四 if 1 == 1: print("111") else: print("222") name = raw_input("Username:") pwd = raw_input("Password:") if name == "qiao" and pwd == "666": print("yes") else: print("no") if條件一: xxx elif條件二: xxx elif條件三: xxx else: xxx 例: inp = raw_input(">>>") if inp == "1": print("111") elif inp == "2": print("222") elif inp == "3": print("333") else: print("...") if name == "alex" or name == "eric" 只要滿足任何一個or條件進入當前代碼塊 1. if條件: 代碼塊 else: 代碼塊 2. if條件: 代碼塊 elif條件: 代碼塊 else: 代碼塊 3.條件 Ture False 1>2 n1>n2 n1 == n2 name == "alex" or name == "eric" name 1 == "alex" name == "alex" and pwd == "123" while 代碼塊: 代碼塊 import time n1 = Ture while n1: print("1") time.sleep(1) n1 = False print("end') ______________________________________________________________________________________ 例: kaishi = 1 while Ture: print(kaishi) kaishi = kaishi+1 time.sleep(1) ###列印(1) ——————————————————————————————————————— break 用於跳出當前迴圈,並且break下麵的代碼,將不再執行 例: kaishi = 1 while Ture: print(kaishi) if kaishi == 10: break kaishi = kaishi+1 _____________________________________________________________________________________________ continue 用於跳出本次迴圈,繼續下一次迴圈 while True: print("123") break print("456") ###列印(123) while Ture: print("123") continue print("456") ###列印(123) ——————————————————————————————————————————— 例: 1.輸入1-10整數 start = 1 while Ture: print(start) if start == 10: break start+= 1 2.輸入除7以外的1-10所有正整數 start = 1 while Ture: if start == 7: start += 1 continue print(start) if start == 10: break start += 1 3.輸入1-100以內所有整數 start = 1 while Ture: print(start) if start == 100: break start += 1 4.求1-100內所有整數合 sum = 0 start = 1 while Ture: sum = sum + start if start == 100: break start += 1 print(sum) 5.輸出1-100內所有奇數 start = 1 while start < 101: temp = start%2 if temp == 1: print(start) else: pass start += 1 ###temp是餘數 6.輸出1-100內所有偶數 start = 1 while start < 101: temp = start%2 if temp == 0: print(start) else: pass start += 1 + - * / %(求餘) 嗯,目前就寫到這裡吧。 ——————————————————————————————————————————