这篇文章主要介绍了简单了解python代码优化小技巧,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
对比以下两种写法,思考一下为何可以这样写。
成绩在 [0,50)、[50,60)、[60,80)、[80,100)、100、其它
score = float(input("请输入你的成绩:")) if score == 100 : print('666呀,走吃大餐去') elif 80 <= score <100 : print('还行,优秀,走,喝饮料去') elif 60 < 80 print('加油呀,弄明白点') 50 score print('这可有点浪哟') 0 print('学不懂吗?') else print('你输入的是啥?')<pre></div><p><= score = 80 ? 上一条语句不满足时往下执行,这时 score <100 就不需要了;<br></p><p>注意: 如果把这些条件表达式的顺序换下,那么这种写法是错误的。<br /></p><div class="gaodaimacode"><pre class="prettyprint linenums"> score = float(input("请输入你的成绩:")) if score == 100 : print('666呀,走吃大餐去') elif score >= 80 : print('还行<i style="color:transparent">来源gaodai$ma#com搞$代*码网</i>,优秀,走,喝饮料去') elif score >= 60 : print('加油呀,弄明白点') elif score >= 50 : print('这可有点浪哟') elif score >= 0 : print('学不懂吗?') else : print('你输入的是啥?')
利用break进行程序运行时间的优化
在循环语句中,可以用break来退出不必要继续执行的循环
有break后
以上就是简单了解python代码优化小技巧的详细内容,更多请关注gaodaima搞代码网其它相关文章!