if if 和 if elif 的区别
再一次编程中意外使用了if if 也实现了 if elif的功能,所以搜索了下其中的区别:
1、if if 和 if elif 是有区别的,只是在某些情况下才会一样的效果;
2、随意使用会导致意外的错误。
现在举几个例子区别:
程序一
def analyzeAge( age ):
if age < 21:
print "You are a child"
if age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
analyzeAge( 18 ) #Calling the function
>You are a child
>The age must be a positive integer!
程序二
def analyzeAge( age ):
if age < 21:
print "You are a child"
elif age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
analyzeAge( 18 ) #Calling the function
>You are a child
上面的例子结果出错,表明 所有的if 后的命令都会运行,而elif后面的命令是根据上一个if是否运行,如果运行了,elif则略过,否则才运行 另外时间也有区别,再举个例子:
def multipleif(text):
if text == 'sometext':
print(text)
if text == 'nottext':
print("notanytext")
def eliftest(text):
if text == 'sometext':
print(text)
elif text == 'nottext':
print("notanytext")
text = "sometext"
timeit multipleif(text)
100000 loops, best of 3: 5.22 us per loop
timeit elif(text)
100000 loops, best of 3: 5.13 us per loop
if if 和 if elif 的区别的更多相关文章
- python if elif else 区别
if data_ori=='医疗': # 医疗 df = pd.read_excel(path_apply + 'apply/YS_ZY_HZSQ_样例.xls', encoding='gbk', e ...
- if 和 elif 的区别
if: 如果一个判断中用if, 程序他会遍历所有的if, 即使你的判断条件遍历到了, 也会继续执行, 直到遍历完所有的if. elif: 而elif呢, 则效率很高. 只要遍历到你的判断条件, ...
- if嵌套和elif的区别
if嵌套的使用场景: 2个(多个)条件有前后关系,必须先满足条件1,再判断是否满足条件2. elif的使用场景: 2个(多个)条件是各自独立的平级关系,满足条件几就执行响应的代码. --------- ...
- python中if和elif的区别
多个if语句是每次单独判断 比如: 例子一: a = 5 if a < 6: #条件1 print(1) if a < 7: #条件2 print(2) else: print(3) 条件 ...
- python 中if和elif的区别
如果程序中判断事件很多,全部用if的话,会遍历整个程序,用elif 程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后自动结束该轮if-elif(即不会再去冗 ...
- if else elif 用法和区别
1.If语句:“如果条件为真,执行子句中的代码."始终包含以下部分: if关键字: 条件(即求值为True或False的表达式): 冒号: 在下一行开始,缩进的代码块(称为if子句) 例如: ...
- Python购物车
product_list = [ ['Iphone',5888], ['Mac Air',8000], ['XiaoMi',19.9], ['coffee',30], ['Tesla',820000] ...
- if else 和if elif else的区别
def fuck(a): if a ==1: print(a) if a ==2: print("not good") else: print("tamade" ...
- Python之路-python(paramiko,进程和线程的区别,GIL全局解释器锁,线程)
一.paramiko 二.进程.与线程区别 三.python GIL全局解释器锁 四.线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生 ...
随机推荐
- python调用java jython
环境:openjdk8,python2.7,jython2.7jython下载地址 http://www.jython.org/downloads.html 下载完成后,运行下面命令 java ...
- mutation与action
mutation 作用: 更改state的状态 说明: 每个mutation对象都有字符串类型(type)与回调函数,在回调函数内进行状态修改,回调函数的第一个参数为state eg: mutatio ...
- OJ的runtime error exit code对应SIGTERM代码
Signal Name Number Description SIGHUP 1 Hangup (POSIX) SIGINT 2 Terminal interrupt (ANSI) SIGQUIT 3 ...
- 日期类(C++实现)
//-------------------------------------------------------------------------- /* **功能:实现日期的简单操作 ** ** ...
- Spring 相关
1. spring的bean的scope属性范围 参考:http://jiangshuiy.iteye.com/blog/1667316 原理分析(bean的scope属性范围) scope用来声明 ...
- Java -- Swing 组件使用
1. 示例1 public class Main { JFrame f = new JFrame(); Icon okIcon = new ImageIcon("/home/test/sta ...
- linux开机过程
一.MBR(main boot record)主引导记录 主引导记录位于0盘面,0磁道,0扇区,早期由512个字节组成. 其中446个字节是boot loader程序.Boot Loader是在操作系 ...
- CentOS安装wireshark
yum install wireshark-gnome yum install libpcap
- CNN检测模型统计检出率
X, y = get_feature_charseq() #max_document_length=64 volcab_file = "volcab.pkl" assert os. ...
- javaScript-基础篇(一)
1.如何插入JS 使用<script>标签在HTML网页中插入JavaScript代码.注意, <script>标签要成对出现,并把JavaScript代码写在<scri ...