数学函数、随机数函数、Python转义字符、字符串格式化

数学函数

print(abs(-9))#返回数字的绝对值
print(pow(2,3))#求2的3次方
print(round(3.213124,3))#四舍五入,3代表小数点后的位数
print(“最小值为:”,min(21,321,432,523,213))#求最小值
print(“最大值为:”,max(21,321,432,523,213))#求最大值
import math
print(math.ceil(1.24141))#向上取整,把小数点变成1相加
print(math.floor(1.24141))#向下去取整数,把小数丢掉
print(“开方为:”,math.sqrt(81))#开方

随机数函数

import random
print(random.choice([1,231,41,325,252]))#指定的列表中随机一个
print(random.choice(“hello”))#字符串,随机一个字符
for i in range(30):
print(i,end=”\t”)
print()
print(random.choice(range(8)))
print(random.randrange(0,214,4))
print(random.random())#生成0-1之间的随机小数
list=[1,2,3,4]
print(random.shuffle(list))
print(“随机的一个四位数为:”,random.choice(range(1000,10000)))

Python转义字符

在需要在字符中使用特殊字符时,python用反斜杠()转义字符

print(r”\t:横向制表符,默认四个空格”)
print(r”\n:换行”)
print(r”\r:回车”)
print(“\”内容\”“)#给“”中加入“”
print(r”\:失效单个特殊含义字符”)

字符串格式化就是把数据插入到字符串占位符中.

# 常用占位符如下:

# %s:赋值字符串类型

# %d:赋值数值类型

# %f:赋值浮点数 %.2f保留2位小数

猜你喜欢

转载自blog.csdn.net/qq_42817166/article/details/81258898