再一次编程中意外使用了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 的区别的更多相关文章

  1. python if elif else 区别

    if data_ori=='医疗': # 医疗 df = pd.read_excel(path_apply + 'apply/YS_ZY_HZSQ_样例.xls', encoding='gbk', e ...

  2. if 和 elif 的区别

    if:    如果一个判断中用if, 程序他会遍历所有的if, 即使你的判断条件遍历到了, 也会继续执行, 直到遍历完所有的if. elif:  而elif呢, 则效率很高. 只要遍历到你的判断条件, ...

  3. if嵌套和elif的区别

    if嵌套的使用场景: 2个(多个)条件有前后关系,必须先满足条件1,再判断是否满足条件2. elif的使用场景: 2个(多个)条件是各自独立的平级关系,满足条件几就执行响应的代码. --------- ...

  4. python中if和elif的区别

    多个if语句是每次单独判断 比如: 例子一: a = 5 if a < 6: #条件1 print(1) if a < 7: #条件2 print(2) else: print(3) 条件 ...

  5. python 中if和elif的区别

    如果程序中判断事件很多,全部用if的话,会遍历整个程序,用elif 程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后自动结束该轮if-elif(即不会再去冗 ...

  6. if else elif 用法和区别

    1.If语句:“如果条件为真,执行子句中的代码."始终包含以下部分: if关键字: 条件(即求值为True或False的表达式): 冒号: 在下一行开始,缩进的代码块(称为if子句) 例如: ...

  7. Python购物车

    product_list = [ ['Iphone',5888], ['Mac Air',8000], ['XiaoMi',19.9], ['coffee',30], ['Tesla',820000] ...

  8. if else 和if elif else的区别

    def fuck(a): if a ==1: print(a) if a ==2: print("not good") else: print("tamade" ...

  9. Python之路-python(paramiko,进程和线程的区别,GIL全局解释器锁,线程)

    一.paramiko 二.进程.与线程区别 三.python GIL全局解释器锁 四.线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生 ...

随机推荐

  1. Data Structure Binary Tree: Largest Independent Set Problem

    http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...

  2. Please enable network time synchronisation in system settings

    eth区块同步出现这样的WARN: WARN [06-17|13:02:42] System clock seems off by -51.509894715s, which can prevent ...

  3. Qt窗口屏幕居中显示

    转自--> http://blog.chinaunix.net/uid-20718335-id-364404.html 窗口的屏幕居中显示问题,在各开发工具中原理相同,首先使用特定的方法得到显示 ...

  4. python第三篇:python、flask关系映射

    python中关系映射主要包括三种:一对多关系映射.一对一关系映射.多对多关系映射. 一对多关系映射 一方:Student(学生) 添加关联属性和反向引用 多方:Article(文章) 添加外键关联 ...

  5. Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

    题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...

  6. BZOJ 2058 [Usaco2010 Nov]Cow Photographs:逆序对【环上最小逆序对】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2058 题意: 给你一个由1~n组成的排列,首尾相接围成一个环. 你可以任意次交换其中两个相 ...

  7. 常用的MySQL语句写法

    常用的MySQL语句写法 MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找.     好记性不如烂笔头,这话说的太有道理了,一段时间不写它,还真容易忘 ...

  8. hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)

    题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  9. Failed to install xxxx.apk on device 'emulator-5554!

    异常信息:[HelloAndroid] Performing com.example.helloandroid.HelloAndroid activity launch[HelloAndroid] U ...

  10. 七牛 python

    Python SDK使用指南 上传策略 变量 对象存储 API 参考手册 多媒体数据处理 API 参考手册