python读书报告_Python读书笔记1

最近在学习《编写高质量代码-改善python程序的91个建议》,记录下读书笔记

使用版本:Python 3.4.0

系统:Windows7

1.字符串格式化:

1 defshow(name,age,email):2 #普通方法

3 print('your name is: %s \nyour age is: %i \nyour email is: %s' %(name,age,email))4 #更加清晰引用的方法

5 print('your name is: %(name)s \n your age is: %(age)i \nyour email is: %(email)s' %{'name':name,'age':age,'email':email})6 #str.format()字符串格式化方法

7 print('your name is: {name} \nyour age is {age} \nyour email is: {email}'.format(name = name,age = age,email =email))8

9 if __name__ == '__main__':10 show('wwl',24,'wwl1991@yeah.net')

2.包管理器:Pip

//pip.pypa.io/en/latest/Python3.4版本内置了pip 存放在python安装路径的Scripts目录,只需要把这个路径加到系统的path路径即可。

安装包:pip install SomePackage

卸载包:pip uninstall SomePackage

升级包:pip install --upgrade SomePackage

显示安装包:pip list

显示安装包哪些需要升级:pip list --outdated

显示包信息:pip show SomePackage

显示安装包包含哪些文件:pip show --files SomePackage

$ pip install SomePackage # latest version

$ pip install SomePackage==1.0.4 # specific version

$ pip install 'SomePackage>=1.0.4' # minimum version

3.代码分析工具

pylint官网:

http://pylint.org/Pylint 默认使用的代码风格是 PEP8

4.三元操作

C ? X: Y

当C为True的时候取值X,否则取值Y

Python等价形式 X if C else Y>>> a = 3

>>> print('haha') if a <5 else 'hehe'haha>>> print('haha') if a <2 else 'hehe'

'hehe'

5.switch..case

Python中没有switch...case...

以下两种方法代替:1.if...elif...if x ==0:return('000')elif x == 1:return('1111')2.{}.get(x)

{0:'000',1:'1111'}.get(0)

6.断言assert

>>> x = 1

>>> y = 2

>>> assert x == y,'not equals'Traceback (most recent call last):

File"", line 1, in

assert x == y,'not equals'AssertionError:notequals

等价于下面>>> if __debug__ and not x ==y:raise AssertionError('not equals')

Traceback (most recent call last):

File"", line 2, in

raise AssertionError('not equals')

AssertionError:notequals

其中__debug__为True而且不能修改

使用断言是有代价的,它会对性能产生一定影响。禁用断言的方法是在运行脚本时候加上-O(忽略与断言相关的语句)

断言是被设计用来捕获用户定义的约束的,而不是用来捕获程序本身错误

7.数值交换不推荐中间值

常用方法:

temp=x

x=y

y=temp

推荐方法:

x,y=y,x

解析:

先计算=右边的值为(y,x)然后分别赋值给x,y变量

性能对比:>>> importtimeit>>> timeit.Timer('x,y = y,x','x = 1;y = 2').timeit()0.03071109231768787

>>> timeit.Timer('temp = x;x = y;y = temp','x = 1;y = 2').timeit()0.035570626849676046

8.惰性计算lazy evaluayion

if x andy

在x为False的时候y不再计算if x ory

在x为True的时候y不再计算

x和y表达式的前后顺序会对程序执行时间造成影响。

9.枚举enum

Enums have been added to Python 3.4 as described in PEP 435. It has also been backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4on pypi.

To use backports, do $ pip install enum34, installing enum (no numbers) will install a completely differentandincompatible version.from enum importEnum

Animal= Enum('Animal', 'ant bee cat dog')

等价于classAnimals(Enum):

ant= 1bee= 2cat= 3dog= 4以上是下面链接的搬运:

http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python

10.类型检查推荐isinstance而不是type

对应内建的基本类型来说使用type()进行类型检查是ok的;

基于内建类型扩展的用户自定义类型,type并不能准确返回结果;

>>> import types

>>> class UserInt(int):

def __init__(self,val=0):

self._val = int(val)

>>> n = UserInt(2)

>>> n

2

>>> type(n)

>>> isinstance(n,int)

True

关于type() 2.7和3.4对于class的处理还是有区别的,如下图:

021418252788013.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OFFICIAL DESCRIPTION Using fun graphics and easy-to-follow instructions, Coding Projects in Python is a straightforward, visual guide that shows young learners how to build their own computer projects using Python, an easy yet powerful free programming language available for download. Perfect for kids ages 10 and over who are ready to take a second step after Scratch, Coding Projects in Python teaches kids how to build amazing graphics, fun games, and useful apps. All they need is a desktop or laptop, and an Internet connection to download Python 3. Step-by-step instructions teach essential coding basics like loops and conditionals, and outline seven fun and exciting projects, including a script that cracks secret codes, a quiz to challenge family and friends, a tic-tac-toe game, and much more. When they are feeling more confident, kids can think creatively and use the tips and tricks provided to personalize and adapt each project. The simple, logical steps in Coding Projects in Python are fully illustrated with fun pixel art and build on the basics of coding, so kids can have the skills to build whatever kind of project they can dream up. Supporting STEM education initiatives, computer coding teaches kids how to think creatively, work collaboratively, and reason systematically, and is quickly becoming a necessary and sought-after skill. DK's computer coding books are full of fun exercises with step-by-step guidance, making them the perfect introductory tools for building vital skills in computer programming.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值