0
点赞
收藏
分享

微信扫一扫

Python从门到精通(一):基础-02-基本类型

虽说python是弱类型的,但在使用时还是需要明确一些基本类型的。在所有的基本类型中最复杂的就是数字类型。本章就一起熟悉中基本类型中的-数值。

一、基础操作

1.1、格式化输出

通用格式: 【[<>^]?width[,]?(.digits)?】,1、width和digits为整数,分别表示整体输出的宽度(不足的用空格填充),digits表示小数点占了几位(默认四舍五入);2、?可选可不写:3、<>^表示补空格的方向;4、,.分别表示是否使用分隔符;5、f代表格式化的意思,如果档加f则{}当做普通字符串输出。

x = 1234.56789
print(f'8.2f format {x}: {x:8.2f}') //空空1234.57
print(f'>10.1f format {x}: {x: >10.1f}') //空空空空空1234.6
print(f'<10.1f format {x}: {x: <10.1f}') //1234.6空空空空空
print(f'^10.1f format {x}: {x: ^10.1f}') //空空空1234.5空空
print(f', format {x}: {x: ,}') //1,234.56789,整数部分用,分隔
print(f'0,.1f format {x}: {x: ,.1f}') //1,234.6,整数部分用,分隔,小数部分为.分隔

指数计数

print(f'e format {x} is: {x: e}')  //1.234568e+03
print(f'0.2E format {x} is: {x: 0.2E}')//1.23E+03
print(f'x format: {x: 0.1f}') //1234.6
print(f'-x format: {-x: 0.1f}')//取反操作 -1234.6

1.2、进制变换

x = 1234
print(f'binary of {x} is: {bin(x)}') //二进制 0b10011010010
print(f'octal of {x} is: {oct(x)}') //十进制 0o2322
print(f'hex of {x} is: {hex(x)}') //16进制 0x4d2

不显示制表符

print(f'binary not show 0b:{x:b}') //二进制 10011010010
print(f'octal not show 0o:{x:o}') //十进制 0o2322

1.3、函数

print(int('4d2', 16))         //1234
print(int('10011010010', 2)) //1234
print(int('2322', 8)) //1234

1.4、四舍五入

print(round(1.23, 1)) #1.2

#round函数对于5这种中间值会返回离它最近的偶数
print(round(2.5, 0)) #2.0
print(round(1.5, 0)) #2.0

a = 1627731
print(round(a, -2)) #1627700

1.5、无穷

在python中没有特定的对象来表示这样的浮点数,所以需要用float来创建。

print(f"float('inf') = {float('inf')}") #inf
print(f"float('-inf') = {float('-inf')}") #-inf
print(f"float('nan') = {float('nan')}") #nan

import math
print(f"float('inf') type is inf: {math.isinf(float('inf'))}") #true
print(f"float('nan') type is nan: {math.isnan(float('nan'))}") #ture

二、浮点数

建议根据实际情况来,虽然普通的用法有误差但速度比较快,所以不是严格的不要用decimal,它的精度是17位。

a = 2.1
b = 4.2
print(a + b) # 这处是有误差的6.300000000000001

# 浮点数要用这种格式
from decimal import Decimal
a = Decimal('2.1')
b = Decimal('4.2')
print(f'a + b = {a + b}') # a + b = 6.3

from decimal import localcontext
a = Decimal('1.3')
b = Decimal('1.7')
print(f'a / b = {a / b}') # a / b = 0.7647058823529411764705882353
with localcontext() as ctx:
ctx.prec = 3 # 位数
print(f'a / b = {a / b}') # a / b = 0.765

with localcontext() as ctx:
ctx.prec = 50
print(f'a / b = {a / b}') # a / b = 0.76470588235294117647058823529411764705882352941176

# 在做浮点数运算时要注意精度的问题,第一个例子是错误的,第二个是正确的
num_list = [1.23e+18, 1, -1.23e+18]
print(f'sum result is: {sum(num_list)}') # sum result is: 0.0

import math
print(f'math sum result: {math.fsum(num_list)}') # math sum result: 1.0

三、分数

程序中一般很少用到,如果不是必须的还是建议定义成整数或浮点数来运算。

from fractions import Fraction
a = Fraction(5, 4)
b = Fraction(7, 16)

print(f'{a} + {b} = {a + b}')
print(f'{a} * {b} = {a * b}')

c = a * b
print(f'numerator of {c} is: {c.numerator}')
print(f'denominator of {c} is: {c.denominator}')

print(f'float({c}) = {float(c)}')

print(f'{c} limit denominator 8 = {c.limit_denominator(8)}')

x = 3.75
print(f'{x} to fractions is: {Fraction(*x.as_integer_ratio())}')

5/4 + 7/16 = 27/16
5/4 * 7/16 = 35/64
numerator of 35/64 is: 35
denominator of 35/64 is: 64
float(35/64) = 0.546875
35/64 limit denominator 8 = 4/7
3.75 to fractions is: 15/4

四、随机数

import random
values = [1, 2, 3, 4, 5, 6]
"""随机选择1个"""
print(f'random choice from {values} is {random.choice(values)}') #1
"""随机选择2个"""
print(f'random sample 2 from {values} is {random.sample(values, 2)}') #[5, 3]

"""只是打乱元素的顺序"""
random.shuffle(values)
print(f'random shuffle is:{values}') #[3, 5, 4, 6, 2, 1]

"""生成随机数"""
print(f'random.randint(0,10) = {random.randint(0, 10)}') #9

"""生成0~1之间的随机数"""
print(f'random.random() = {random.random()}') #0.6299961967523697

"""获取N位随机数的整数"""
x = random.getrandbits(200)
print(f'random.getrandbits(200) = {x}') #636975414748915127401449454055264672180771111158792457403568

"""random()采用了Twister算法,这是一个确定性算法,可以在计算前用seed()设置种子"""
print(f'random.seed() = {random.seed()}')
print(f'random.seed(123) = {random.seed(123)}')


举报

相关推荐

0 条评论