15.Pythonic与python杂记
switcher ={
:'sunday',
:'monday',
:'thuesday'
}
day =
day_name=switcher.get(day,'Unknow')
print(day_name) # sunday
def get_sunday():
return 'sunday' def get_monday():
return 'monday' def get_thuesday():
return 'thuesday' def get_default():
return 'Unknow' switcher ={
:get_sunday,
:get_monday,
:get_thuesday
} day =
day_name=switcher.get(day,get_default)()
print(day_name) # sunday
# 列表推到式
# 集合推到式
# map filter
# set
# dict
a = [,,,,,,,]
# b = [i*i for i in a]
b = [i** for i in a]
print(b) # [, , , , , , , ]
c = [i** for i in a]
print(c) # [, , , , , , , ]
d = [i** for i in a if i > ]
print(d) # [, , ] e =(,,,,,,,)
f = [i** for i in a if i > ]
print(f) # [, , ]
g = {i** for i in a if i > }
print(g) # {, , }
students = {
'喜小乐':,
'石敢当':,
'张三':
}
# 字典
b = {key for key,value in students.items()}
print(b) # {'石敢当', '喜小乐', '张三'}
b = {value for key,value in students.items()}
print(b) # {, , }
b = {value:key for key,value in students.items()}
print(b) # {: '喜小乐', : '石敢当', : '张三'}
# 元组
b = (key for key,value in students.items())
for x in b:
print(x)
# 喜小乐
# 石敢当
# 张三
# None 空
# 空字符串 空的列表 False
a = ''
b = False
c =[]
print(a==None) # False
print(b==None) # False
print(c==None) # False
print(a is None) # False
print(type(None)) # <class 'NoneType'> a = []
a =''
a = None # 不存在
a = False # 真假 判断为空的方式
if a:
if not a:
class Test():
def __len__(self):
return
# return
# return True test = Test()
if test:
print('S') # True/
else:
print('F') # print(len(Test())) #
print(bool(Test())) # False
class Test():
def __bool__(self):
return False def __len__(self):
return True test = Test()
if test:
print('S')
else:
print('F') # F
15.Pythonic与python杂记的更多相关文章
- Python(十二) Pythonic与Python杂记
一.导言 二.用字典映射代替switch case语句 # 字典代替 switch 语句 # switch () # { # case 0 : # dayName= 'a'; # break; # ...
- Python3(十二) Pythonic与Python杂记
一.用字典映射代替switch case语句 if/else可以代替switch但是非常不合适. 用字典代替switch: day = 5 switcher = { 0:'Sunday', 1:'Mo ...
- Be Pythonic ,Google Python Style Guide
为了更规范的写代码,变得更专业 分号 1 不在句末添加分号,不用分号在一行写两句代码 行长度 2 每行不超过80字符,python会隐式行连接圆括号,中括号,花括号中的字符,如多参数方法调用可以写为多 ...
- python杂记二
1. 写文件可以直接使用print函数 file_name = open("file_name.txt","w") print("file conta ...
- PAT 1031 查验身份证(15)(C++&Python)
1031 查验身份证(15)(15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8, ...
- python 杂记-unittest
介绍单元测试的好文:https://mp.weixin.qq.com/s/njxc8GXSlc3z_RibK70ROg setUpModule/tearDownModule:在整个模块的开始和结束时被 ...
- python杂记-4(迭代器&生成器)
#!/usr/bin/env python# -*- coding: utf-8 -*-#1.迭代器&生成器#生成器#正确的方法是使用for循环,因为generator也是可迭代对象:g = ...
- python杂记-3(购买商品)
#!/usr/bin/env python# -*- coding: utf-8 -*-#如下是一个购物程序:#先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品.count = ...
- python杂记-1(os模块)
os模块说明:python os模块包含普遍的操作系统功能 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录os.chflags(pat ...
随机推荐
- ubuntu开启mysql远程连接,并开启3306端口
mysql -u root -p 修改mysql库的user表,将host项,从localhost改为%.%这里表示的是允许任意host访问,如果只允许某一个ip访问,则可改为相应的ip mysql& ...
- 后台框架 FastAdmin V1.0.0.20200228 发布,为疫情防控作贡献
后台框架 FastAdmin V1.0.0.20200228 发布,为疫情防控作贡献 https://www.oschina.net/news/113694/fastadmin-1-20200228- ...
- git add 添加错文件 撤销
git status 先看一下add 中的文件 git reset HEAD 如果后面什么都不跟的话 就是上一次add 里面的全部撤销了 git reset HEAD XXX/XXX/XXX.java ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:在 <tbody> 内添加斑马线形式的条纹 ( IE8 不支持)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java基础 -5
方法的定义与使用 方法(method)的基本定义 本次方法定义在主类之中并且由主方法直接调用,所以方法的定义语法形式如下: public static 返回值类型 方法名称([参数类型 变量, ... ...
- HTML5模板引擎 Thymeleaf 教程(转)
原文:http://www.open-open.com/lib/view/open1383622135586.html Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非 ...
- 列表推导式、生成器表达式以及zip()max()max()/min()sum()sort()map()filter()的用法
列表推导式: 基本格式: variable = [out_exp_res for out_exp in input_list if out_exp == 2] #out_exp_res: 列表生成元素 ...
- Linux centosVMware xshell使用xftp传输文件、使用pure-ftpd搭建ftp服务
一.xshell使用xftp传输文件 Ctrl+Alt+F 弹出 下载进入 填写任意名字,自己邮箱 进入邮箱点击网址就自动下载了 然后安装 二.使用pure-ftpd搭建ftp服务 yum insta ...
- word无法切换中文输入法的解决方法
问题: 在word编辑文字的时候,莫名其妙地出现只能输入英文字母无法输入中文的现象,输入法状态条也不显示,而输入法是正常的(在其他编辑器中可正常输入汉字) 解决方法如下:word 2003:菜单工具- ...
- js加密(九)hr.bibibi md5
1. 寻找加密js: 2. 结果: 3. execjs调用js即可.