Python 运算符 if和while的使用
運算符:
1)算術運算符
+ -*/ %(取余) //(地板除,取整)**(冪運算) ,返回一個值
2)比較運算符
3 ) > >= < <= ==(比較值是否相等) !=(比較值是否不相等) ,返回一個布爾值
4 ) 賦值運算符
= += -= *= /= **=
5)邏輯運算符(把多個條件同時疊加)
and or not ,加括號優(yōu)先級最高
6)身份運算符(id)
x= 257 y = x z= 257print(id(x) == (id(y))) # True print(x is y) # is比較的是內(nèi)存地址 True print(x is not y) # is not判斷是否不等于 print(not x is y) #False print(id(x) == id(z)) #False print(x is z) #False7 ) 位運算符
60 13 十進制 0,1,2,3,4,5,6,7,8,9,10 0,1,2,3,4,5,6,7,8,9,逢十進一位,10,11,12,13,...19,20...90,91,92,..99,1000和1 二進制
0,1,逢二進一位,10,11,100,101,111,1000
方法一,計算器:67
方法二:手工計算
9892 == 2*10**0 + 9*10**1 + 8*10**2 + 9*10**3 print(2*10**0 + 9*10**1 + 8*10**2 + 9*10**3) 01000011 == 1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0 print(1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0)8)成員運算符:判斷元素是否在容器類元素里面(字符串)
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' class_student_lt = ['s1','s2','s3'] print('s1' in class_student_lt) # True print('s1' not in class_student_lt) # False print('s4' in class_student_lt) # False s = 'nick' print('n' in 'nick')2. 流程控制之if判斷
多分支結構1:
if 條件1:code1 條件1成立執(zhí)行code1 elif 條件2:code2 條件1不成立條件2成立執(zhí)行code2 elif 條件3:code3 條件1和2不成立,條件3成立執(zhí)行code3 elif可以有無限個。。。coden else:code4 所有條件都不成立,執(zhí)行code4elif事例
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' height=input('請輸入你的身高》》》:').strip() height=int(height) if height>130:print('全票') elif height>70:print('半票') elif height>30:print('可能你需要付費一點點!') else:print('免費')多分支結構1:
if 條件1:code1 條件1成立執(zhí)行code1,再判斷下面的if條件是否成立 if 條件2:code2 條件2成立執(zhí)行code2,再判斷下面的if條件是否成立 if 條件3:code3 條件3成立執(zhí)行code3,再判斷下面的if條件是否成立 if可以有無限個。。。coden else:code4 所有條件都不成立,執(zhí)行code4if 事例
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' height=input('請輸入你的身高》》》:').strip() height=int(height) if height>130:print('全票') if height>70 and height<=130:print('半票') if height>30 and height<=70:print('可能你需要付費一點點!') else:print('免費')多個同一級別if和elif建立多分支結構的區(qū)別:
如果程序中判斷事件過多,全部用if的話,會遍歷整個程序,用elif程序運行時,只要if或后續(xù)某一個elif之一滿足邏輯值為True,則程序執(zhí)行完對應輸出語句后會自動結束該輪if—elif,即不會再冗余地執(zhí)行后續(xù)的elif或else,提高了程序的運行效率。
3.流程控制之while循環(huán)
break:結束當前層while循環(huán),continue:跳出本次while循環(huán),進入下一次循環(huán)
while 條件:# 條件成立運行代碼,不成立結束while循環(huán)
代碼 # 代碼執(zhí)行結束后會進入下一次循環(huán)(再一次判斷條件)
while+條件
count=0 t_age=18 while count<3:# if count==3:# print('不好意思,只有三次機會!')# breakage=input('請輸入你的年齡:')age=int(age)if age>t_age:print('太大了')elif age<t_age:print('太小了')else:print('恭喜你猜對了!')count += 1while + break
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' count = 0 while 1:if count == 100:break # break終止循環(huán)count += 1print(count)print('bzr')while + continue 不打印50
count = 0 while 1:if count == 100:break # break終止循環(huán)count += 1if count == 50:continue # continue跳出本次循環(huán),不執(zhí)行下面的代碼print(count)print('bzr')打印1-100內(nèi)偶數(shù)(不包括[22,46,68,98])的和
分解題目
while + else 僅作了解(非用不可可以使用,不要和if。。else混了)
count = 0 while count < 100:count += 1print(count) else:print('沒有被break干掉我就能出來')#打印結果:除了1~100之外,沒有break掉,else內(nèi)print也能打印while + else
count=0 while True:count+=1print(count)if count==100:break else: # 沒有被break干掉就執(zhí)行,被break終止了就不執(zhí)行了print('沒有被break干掉我就能出來') # 可以判斷while是否被break終止# 打印結果: else 內(nèi)的print不能打印,上面有break終止循環(huán)了。猜年齡游戲 只有三次機會
count=0 t_age=18 while count<3:age=input('請輸入你的年齡:')age=int(age)if age>t_age:print('太大了')elif age<t_age:print('太小了')else:print('恭喜你猜對了!')count += 1總結
以上是生活随笔為你收集整理的Python 运算符 if和while的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python if条件判断和while循
- 下一篇: python基本数据类型的结构和使用方法