在python中,一起皆对象,省略号也不例外

在python3中你可以直接写…来得到它

...
Ellipsis

它转python时为True

bool(...)
True

最后

id(...)
140727377573056

这东西有啥用呢

1.它是 numpy的一个语法糖

2.在python3中可以用…代替pass

使用end来结束代码块

def a(x):
    if x > 0"
        return x
    else:
        return -x
    end
end
print(a(10))
print(a(-10))

可直接运行zip包,zip包如何制作呢

python -m zipfile -c demo.zip demo/*

制作完之后,我们就可以去执行它

python demo.zip

\ 在行尾时,用做续行符

在字符串中,作为转义符

python解释器提示符修改,该修改只能在终端里进行,无法在IDEL修改

cmd ---python3---
import sys
sys.ps1
sys.ps2
sys.ps1='python时光'
sys.ps2='~~~'

简洁优雅的链式比较

False ==False ==True
False

当or值都为真,python会选择第一个

当and值都为真,python会选择第二个

(2 or 3 ) * (5 and 7)
14

连接多个列表最极客的方式

a = [1,2]
b = [3,4]
c = [5,6]
sum((a,b,c),[])
[1, 2, 3, 4, 5, 6]

字典也可以排序

mydict={str(i):i for i in range(5)}
mydict
{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}

续行符可以增加代码的可读性

a = 'talk is cheap,'\
...     'show me the code.'
print(a)
talk is cheap,show me the code.

哪些情况下可以不需要呢,在这些符合中的换行[],(),{}

mylist = [1,2,3,
...           4,5,6]
mylist
[1, 2, 3, 4, 5, 6]

在多行文本注释中,续行符也是不需要写的”’

text = '''talk is cheap ,
... show me the code '''
text
'talk is cheap ,\nshow me the code '

为避免整数频繁申请和销毁内存空间,python定义一个小整数池,这些对象时提前建立好的,不会被垃圾回收。

a = -6
b = -6
a is b
False
a = 5 
b = 5
a is b
True
a = 257
b =257
a is b
False
a = 257;b=257
a is b
True
#最后一个为啥为True呢,因为在同一行里,python知道对象已经生成,那么它就会引用到同一个对象,如果分成两行, 解释器并不知道对象已经生成,就会重新生成内存存放这个对象

神奇的intern机制,同样的字符对象仅仅会保存一份,放在同一个字符串储蓄池中,是共用的

s1 = "hello"
s2 = "hello"
s1 is s2
True
s2 = "hello 0"
s1 = "hello 0"
s1 is s2 #如果用空格则不启用共享
False

argument和parameter的区别

parameter #形参
argument  #实参
def output(msg):
    print(msg)
output_msg("error")
#error 为argum ,而msg为param

dict()与{}有什么区别呢

{}会比dict()快3倍,使用timeit可以轻松测出这个效果

python中一些有趣的模块

import __hello__ #无聊的包
Hello world!

import this #python之禅

import antigravity #反地心引力漫画

Categories: python

0 Comments

发表评论

Avatar placeholder

邮箱地址不会被公开。 必填项已用*标注