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队列 生 ...
随机推荐
- HDU OJ 2159 FATE
#include <stdio.h> #include <string.h> ][] ; ]; //»ñµÃ¾Ñé ]; //»¨·ÑµÄÈÌÄÍ¶È int main() ...
- 算法(Algorithms)第4版 练习 2.3.25
代码实现: public static void sort(Comparable[] a) { StdRandom.shuffle(a);//eliminate dependence on inp ...
- 算法(Algorithms)第4版 练习 2.2.9
package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; publi ...
- HDU 4001 To Miss Our Children Time(2011年大连网络赛 A 贪心+dp)
开始还觉得是贪心呢... 给你三类积木叫你叠楼房,给你的每个积木包括四个值:长 宽(可以互换) 高 类型d d=0:你只能把它放在地上或者放在 长 宽 小于等于 自己的积木上面 d=1:你只能把它放在 ...
- Windows- 改变cmd控制台默认显示编码
在中国的电脑会要求兼容gbk编码,所以微软进入中国市场也顺应中国的要求,其中最明显的就是在cmd上默认的显示就是GBK .当开发人员在运行一些有打印中文的程序时,由于编码采用国际兼容版本的utf_8等 ...
- CSS3特殊图形制作
CSS3特殊图形制作 现在IE8+的浏览器都支持CSS3+HTML5了,IE8及以下的浏览器的浏览器也快告一段落了,大前端的时代来了.废话不多说,现在开始用CSS3画图 1.心型 //HTML < ...
- BZOJ 3890 [Usaco2015 Jan]Meeting Time:拓扑图dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3890 题意: 给你一个有向图,n个点(n <= 100),m条边. 且所有的边都是从 ...
- jQuery Cloud Zoom:图片放大镜插件 破解插件
/* Cloud Zoom 10 Site License (CZ01-10). Version 3.1 rev 1312051822 */ (function(e) { function s(a) ...
- python遍历并获取对象属性--dir(),__dict__,getattr,setattr
一.遍历对象的属性: 1.dir(obj) :返回对象的所以属性名称字符串列表(包括属性和方法). for attr in dir(obj): print(attr) 2.obj.__dict__:返 ...
- codeforces 653B B. Bear and Compressing(dfs)
题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes inpu ...