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 ...
随机推荐
- UIKit框架使用总结--看看你掌握了多少
一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...
- MySQL之关系
目录 关系 多对多的关系,如何通过mysql来表示 一对一关系 关系 多对多的关系,如何通过mysql来表示 站在老师的角度 一个老师可以教多个学生, 一个老师也可以教一个学生. 站在学生的角度 一个 ...
- JS原型链的理解和使用(二)
根据在创建对象的时候,创建出来的对象的__proto__指向创建这个对象的函数的prototype属性. 由于在调用对象的属性或者方法的时候会首先在对象的作用域中查找指定的属性或者方法,如果未找到则会 ...
- Mate Linux 桌面的什么受GNOME 2 粉丝喜欢 ?
导读 如果你以前听过这个传闻:当 GNOME3 第一次发布时,很多 GNOME 用户还没有准备好放弃 GNOME 2. Mate(以马黛茶yerba mate植物命名)项目的开始是为了延续 GNOME ...
- ProgressBarForm 进度条
ProgressBarForm public partial class ProgressBarForm : Form { private Panel panel1 = new System.Wind ...
- 第2节 Scala中面向对象编程:12、13、14、15、16、trait
6.4. Scala中面向对象编程之trait 6.4.1. 将trait作为接口使用 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java ...
- Linux三剑客之awk精讲(基础与进阶)
第1章 awk基础入门 要弄懂awk程序,必须熟悉了解这个工具的规则.本实战笔记的目的是通过实际案例或面试题带同学们熟练掌握awk在企业中的用法,而不是awk程序的帮助手册. 1.1 awk简介 一种 ...
- [原]JointJS流程图
最近项目上需要用流程图来做问题定界分析,之前有同事用jsPlumb做过,但是阅读代码后觉得比较麻烦,所以自己又找了一圈,找到一个叫Dagre-D3的开源类库,画出来的效果如下图,Dagre-D3最大的 ...
- rarlinux安装和使用
rarlinux安装和使用
- 代码审计(1):sql注入漏洞
挖掘经验:sql注入经常出现在登录界面.获取HTTP请求头.订单处理等地方.而登录界面的注入现在来说大多是发生在HTTP头里面的client-ip和x-forward-for,一般用来记录登录的ip地 ...