1.输入输出
# 输入
name = input("请输入您的姓名:") # 返回字符串
# 输出
print("Hello: ", name)
# 格式化输出
print('%2d-%02d' % (1, 1)) # ' 1-01'
print('A', 1, sep=',', end='!') # A,1!
# format格式化输出
print("{0},{1}".format('A','B')) # A,B
print("{a},{b}".format(a='A',b='B')) # A,B
print("{0[0]},{0[1]}".format(['A','B'])) # A,B
print("{:.2f}".format(3.1415926)) # 3.14
# 对象的format格式化输出
class A(object):
def __init__(self, value):
self.value = value
a = A(6)
print('{0.value}'.format(a)) # 6
2.数学函数
for i in range(5):
print(i) # 0 1 2 3 4
for i in range(1,5):
print(i) # 1 2 3 4
for i in range(1,5,2):
print(i) # 1 3
l = [-2, -1, 0, 1, 2]
print(max(l)) # 2
print(min(l)) # -2
print(len(l)) # 5
print(sum(l)) # 0
print(abs(-1)) # 1
print(pow(2, 5)) # 32
print(divmod(5, 2)) # (2, 1) (商,余数)
print(round(1.25361, 3)) # 1.254
print(round(1627731, -1)) # 1627730
# 返回对象的哈希值
print(hash('B')) # 8720545829485517778
a = complex(1, 2) # 或 a = 1 + 2j
print(a.real) # 1.0
print(a.imag) # 2.0
print(a.conjugate()) # (1-2j)
3.类型转换
print(int('123')) # 123
print(float('123.4')) # 123.4
print(str(123)) # '123'
print(bin(2)) # 0b10 二进制
print(oct(8)) # 0o10 八进制
print(hex(16)) # 0x10 十六进制
print(bytes(1)) # b'\\\\x00'
print(ord('a')) # 97
print(chr(97)) # a
print(list((1,2,3))) # [1, 2, 3]
print(set([1,2,3,3])) # {1, 2, 3}
print(frozenset([1,2,3,3]))
# frozenset({1, 2, 3})
print(dict([('A', 1), ('B', 2)]))
# {'A': 1, 'B': 2}
print(dict(zip(['A', 'B'], [1, 2])))
# {'A': 1, 'B': 2}
# zip打包
l1 = ['a','b','c']
l2 = [1,2,3]
for i in zip(l1,l2):
print(i)
# ('a', 1) ('b', 2) ('c', 3)
# zip解压
for i in zip(*zip(l1,l2)):
print(i)
# ('a', 'b', 'c') (1, 2, 3)
4.对象自省
print(all([0,1,2])) # 所有元素为真返回True
print(any([0,1,2])) # 任一元素为真返回True
print(bool(2)) # True
print(type(2)) # <class 'int'>
print(id(2)) # 返回内存地址
print(callable(2)) # 能否调用
print(isinstance(2,int)) # True
print(isinstance(2,(str,int))) # True
print(issubclass(str, int)) # False
print(dir([str])) # 返回对象的属性方法列表
print(help('sys')) # 返回对象的帮助文档
print(locals()) # 返回当前局部变量的字典
print(globals()) # 返回当前全局变量的字典
# 对象的属性相关方法
class Student(object):
def __init__(self, name):
self.name = name
s = Student('Tom')
print(getattr(s, 'name')) # Tom
print(hasattr(s, 'age')) # False
print(getattr(s, 'age', 5)) # 属性age不存在,但会返回默认值5
setattr(s, 'age', 5) # 设置age属性
print(hasattr(s, 'age')) # True
delattr(s, 'age') # 删除属性
print(hasattr(s, 'age')) # False
5.执行表达式
r = compile("print('hello,world')", "<string>", "exec")
exec(r)
# hello,world
print(eval("1+2*3"))
# 7
r = compile("3*4+5",'','eval')
print(eval(r))
# 17
6.函数式编程
# map映射
def func(x):
return x * x
for i in map(func, [1, 2, 3]):
print(i) # 1 4 9
for i in map(lambda x: x*x, [1, 2, 3]):
print(i) # 1 4 9
for i in map(lambda x,y: x+y, [1,3,5], [2,4,6]):
print(i) # 3 7 11
# reduce累积
from functools import reduce
def add(x, y):
return x + y
print(reduce(add, [1, 3, 5])) # 9
# filter过滤
for i in filter(lambda e: e%2, [1, 2, 3]):
print(i) # 1 3
# sorted排序
print(sorted([1, 5, 2], reverse=True))
# [5, 2, 1]
print(sorted([('b',2), ('a',1)], key=lambda x:x[0]))
# [('a', 1), ('b', 2)]