一,while循環
while 條件: while語句塊(循環體) 運行: 判斷你給的條件是否為真,如果真則執行循環體。否則跳出循環。 執行完循環體之后再次判斷條件是否為真 例子1 我們玩聯盟的時候噴打野的的時候,可以用這種方式,這是一個死循環,條件為真,他會一直執行除非手動停止.
while True: print("你是不是傻") print("出來幫一下可不可以") print("出來幫一下可不可以") print("出來不可以")
例子2 就是while循環增加條件限制條件
num = 1 while num <= 10: # 條件判斷 print(num) num = num + 1 # 改變循環變量這個程序就會執行到num這個變量大於10的時候停止循環,會打印1到10之間的數字
例子3 while循環可嵌套
while True: content = input("請輸入你要噴的內容,輸入Q停止噴人:") if content == 'Q': # 退出循環 break # 徹底的打斷當前本層循環 #continue # 停止當前本次循環繼續執行下一次循環 else: print("發送給打野", content) 注意:break是指終止本次循環,而continue是停止當前循環執行下次循環. 用戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩余錯誤次數(提示:使用字符串格式化)
count=1 while count<4: name = input("name:") password = input("password:") mingpian = ''' ======in of %s===== name:%s password:%s =========end======= ''' % (name, name, password) count=count+1 if name=="liu": print("輸入用戶名正確") else: print("輸入用戶名錯誤") continue if password=="123456": print("你好,歡迎使用.") print(mingpian) break else: print("輸入密碼錯誤!")
例子4 除此之后運用while循環可以完成一些比較復雜的計算
- 求1-100的所有數的和
num=1 sum=0 while num<100: sum = sum + num num = num + 1 print(sum)
- 輸出 1-100 內的所有奇數
i=1 while i<99: i=i+2 print(i)
- 輸出 1-100 內的所有偶數
i=0 while i<99: i=i+2 print(i)
- 求1-2+3-4+5 ... 99的所有數的和.
i=1 si=0 sj=0 while i<100: si=si+i j=i+1 i=i+2 sj=sj+j print(si-sj)
例子5,not in 和 in可以用來屏蔽評論,或發布的內容
content = input("請輸入你的評論") if "馬化騰" in content: print("對不起, 您的評論不合法") else: print(content)
2)
guanggao=input("請輸入一句廣告詞:") if "最" in guanggao or "第一"in guanggao or "稀缺" in guanggao or "國家級"in guanggao: print("廣告不合法") else: print("廣告合法")
二,格式化輸出
關於格式化輸出
name ="john" phone = "10010" company = "oldboyedu" job = "老濕" print(''' ===========%s的名片============ 姓名:%s 電話:%s 公司:%s 職位:%s =============================== ''' % (name, name, phone,company,job)) # 末尾要跟上% (值)還有一種情況就是當你的百分號不做占位時python是執行不了的比如程序 print("我叫%s,我已經擁有了全國0.01%的財產了" % ("馬雲")) #執行的時候就會報以下錯誤.TypeError: not enough arguments for format string # %s 占位字符串(萬能) # %d 占位數字 # %f 占位浮點數
三,運算符
加+減-乘*除/整除//取余%次方**
and:並且, 左右兩端都為真. 結果才真.有一個是假.結果就是假 x and y if x is false , then x, else y, or:或者, 左右兩端有一個是真.結果就是真.全部都是假,結果才是假 x or y if x is false, then y, else x, not:取反。 非真即假, 非假即真 not x if x is false,then True,else false,
運算的優先級
() -> not -> and -> or
例子1判斷下列邏輯語句的True,False.這個可以根據優先級可以做出判斷
1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 答:True 2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 答:False 例子2, 1)、6 or 2 > 1 答: 6 6 or True 2)、3 or 2 > 1 答: 3 3)、0 or 5 < 4 答: False 4)、5 < 4 or 3 答: 3 5)、2 > 1 or 6 答:True or 6 True 6)、3 and 2 > 1 答True 7)、0 and 3 > 1 答: 0 8)、2 > 1 and 3 答:3 9)、3 > 1 and 0 答: 0 10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 答:2
剛開始我也很懵逼后來看了公式才知道的
四,就是字符,字節碼
早期. 計算機是美國發明的. 普及率不高, 一般只是在美國使用. 所以. 最早的編碼結構就是按照美國人的習慣來編碼 的. 對應數字+字母+特殊字符一共也沒多少. 所以就形成了最早的編碼ASCII碼. 直到今天ASCII依然深深的影響着我們 后來我們國家推出了GB2312也稱國標碼,中文 60000多個文字 后來推出GBK: 每個字符占2個字節, 16位.中國90000多漢字來說不夠不夠用, 后來國際標准化組織推出了unicode,可容納40億個編碼,夠用但很浪費,后來推出了utf編碼可變長度的編碼, GBK: 每個字符占2個字節, 16位 utf-8 可變長度的unicode 現在我們用的都是萬國碼UTF-8 英文占用1byte 歐洲文字 占用2byte 中文 占用3byte 計算機最小的單位: bit 0,1 byte: 8bit 1byte 8bit = 1byte 1024byte = 1kb 1024kb = 1mb 1024mb = 1gb 1024gb = 1tb