Python实现树结构(有趣) T = [['a, b'], ['c'], ['d', ['e', 'f']]] 随机数里面的一些东西 import random #模块,对象,别名 n = random.random() #获得[0,1)内的随机小数 n = random.randint(1, 100) #[1,100]随机
- Python实现树结构(有趣)
- 随机数里面的一些东西
n = random.random() #获得[0,1)内的随机小数
n = random.randint(1, 100) #[1,100]随机整数
n = random.randrange(1,100) #[1,100)随机整数
- 位运算
- 分数(fraction)
x = Fraction(3,5) #创建分数对象
x.numerator #查看分子
x.denominator #查看分母
- 小知识点
# @矩阵相乘运算符
# i++ 不被允许
- 断言
assert (x < y), '{} is lower than {}'.format(x, y)
assert (y < x), '{} is not lower than {}'.format(x, y)
print('The end!')
运行结果
Traceback (most recent call last):File "E:/Programmer/PYTHON/基础学习/假期学习.py", line 37, in <module>
exec_1()
File "E:/Programmer/PYTHON/基础学习/假期学习.py", line 34, in exec_1
assert (y < x), '{} is not lower than {}'.format(x, y)
AssertionError: 1 is not lower than 2
- raise
s = None
if s is None:
print("s 是空对象")
raise NameError('Error!') # 如果触发了NameError异常,则抛出,并中断程序
print("xxx", len(s))
except TypeError:
print("空对象没有长度")
运行结果
s 是空对象Traceback (most recent call last):
File "E:/Programmer/PYTHON/基础学习/假期学习.py", line 59, in <module>
exec_2()
File "E:/Programmer/PYTHON/基础学习/假期学习.py", line 54, in exec_2
raise NameError('Error!')
NameError: Error!
- lambda
>>> f(2)
4
>>> f = lambda x : None
>>> f(4)
>>>