Created
December 28, 2018 04:27
-
-
Save caoya171193579/69d03ac9d66b1b922784af862c139b2b to your computer and use it in GitHub Desktop.
进阶
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch 语法结构; | |
switch语句用于编写多分支结构的程序,类似与if...elif...else 语句。 | |
switch语句表达的分支结构比if ... elif ... else 语句表达的更清晰,代码的可读性更高。 | |
switch 在其它编程语言里面都存在, 但python 并没有提供switch关键字和语句。 | |
switch 的 实现:、 字典 get() | |
python可以通过字典实现switch语句的功能。 | |
实现方法分为两步: | |
1、首先,定义一个字典。 | |
2、其次,调用字典的get() 参数获取相应的表达式。 | |
# return(瑞陈儿) 定义返回的类型、值 | |
模块引用, 要引用的模块一定要写在前面。 | |
form __future__ import division ## 引用此模块可以不用定义小数点就能整除,得到准确结果,例子: 5/2 正常会等于2 要5.0/2 才得到正确答案,现在引用这个模块就可以直接 5/2 得2.5 | |
 | |
root@kali:~/xuexi# python 11.py | |
1.5 | |
当调用字典中的key ,没有的这个键值的情况下会报错: | |
 | |
root@kali:~/xuexi# python 11.py | |
Traceback (most recent call last): | |
File "11.py", line 23, in <module> | |
print caoya["%"](6,4) | |
KeyError: '%' 报错说没有% ,的键值 | |
 | |
利用了.get() 参数 。。。 | |
############################################ | |
 | |
root@kali:~/xuexi# python 111.py | |
0.5 | |
更加简化了上面的代码,直接用变量,而没有采用函数。 | |
 | |
root@kali:~/xuexi# python 111.py | |
62018.2450031 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment