本文摘自php中文网,作者不言,侵删。
本篇文章给大家带来的内容是关于python中if 条件判断代码解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。条件语句的执行过程:

if 条件判断注意:
1.每个条件后面要使用冒号 : ,表示条件为True时要执行的代码;
2.使用缩进来划分代码块,相同缩进数的语句在一起组成一个代码块。
if...else,单条件判断
1 2 3 4 5 6 7 8 9 10 | username_store = 'lipandeng'
password_store = '123'
username_input = input( 'your username:' )
password_input = input( 'your password:' )
if username_input == username_store and password_input == password_input:
print ( 'welcome {0}' .format(username_input))
else :
print ( 'Invalid username and password!' )
|
if...elif...else,多条件判断
1 2 3 4 5 6 7 8 9 10 | score = int(input( 'Enter your score:' )) # input()返回的数据类型是str,int()函数是把str转int。
if score < 0 or score > 100: # 条件1,此条件为True时执行print(),elif后面的代码不再执行。
print( 'Scores of the range is 0-100.' )
elif score >= 90: # 条件2,当条件1为False时判断条件2,此条件为True时执行print(),elif后面的代码不再执行。
print( 'Your score is excellent.' )
elif score >= 60: # 条件3,当条件1和条件2为False时判断条件3,此条件为True时后执行print(),elif后面的代码不再执行。
print( 'Your score is good.' )
else : # 条件4,以上判断条件都为False时执行的print()。
print( 'Your score is not pass the exam.' )
|
以上就是python中if 条件判断代码解析的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python实现爬虫设置代理ip和伪装成浏览器的方法分享
Python的numpy数组怎么合并
Python实现简单的图片文字识别脚本
Python如何求水仙花数?
Python实现购物车购物小程序
Python如何遍历字符串
Python read lines() 有什么用?能用在什么地方?
Python中format函数什么意思
Python函数学习的注意要点
Python实训之调用math库进行数学运算
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中if 条件判断代码解析