Python 基础

SICP知识点

from operator import add, mul
>>>add(2,3)
5
>>>mul(2,3)
6
>>>add
<built-in function add>
>>>max(1,2,3,4,5)#接收多个参数
5
>>>‘draw’[::-1]#倒置字符串
‘ward'
text = file.read().split()#以空格分开字符串,构成列表
words = set(text)#将列表转为集合
{w for w in words if w == w[::-1] and len(w) > 4}#将符合条件的w组成集合
lower = {w.lower() for w in words}
#可以同时为多个变量赋值
area, circ = pi * radius *radus, 2* pi* radius
>>>def area():
            return pi * radius * radius
>>> area
<function area at 0x123456789>
>>> area()
#根据此时各变量的值进行求值
>>>print(print(1),print(2))
1
2
None None
>>>2013/10
201.3
#201: quotient
#3: remainder
>>>2013//10
201
$python -i functions.py
>>>func1()
[result of func1()]

$python -m doctest -v functions.py
[result of doctests]
#assert语句,条件满足则无事发生,不满足则报对应错
>>> assert 3>2,'Math is broken'
>>> assert 2>3,'That is false'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: That is false

短路求值

>>> None or 2020
2020

数据类型转换

repr(x) #将对象x转换为表达式字符串
eval(str) #用来计算字符串中有效的Python表达式,并返回一个对象
chr(x) #将整数转换为字符 65-A
unichr(x) #将整数转换为unicode字符
ord(x) #字符转换为整数 A-65
hex(x) #整数转为16进制字符串
oct(x)  #整数转为8进制字符串
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。