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

网友投稿 287 2022-08-28


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

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

一、基础操作

1.1、格式化输出

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

x = 1234.56789print(f'8.2f format {x}: {x:8.2f}') //空空1234.57print(f'>10.1f format {x}: {x: >10.1f}') //空空空空空1234.6print(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+03print(f'0.2E format {x} is: {x: 0.2E}')//1.23E+03print(f'x format: {x: 0.1f}') //1234.6print(f'-x format: {-x: 0.1f}')//取反操作 -1234.6

1.2、进制变换

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

不显示制表符

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

1.3、函数

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

1.4、四舍五入

print(round(1.23, 1)) #1.2#round函数对于5这种中间值会返回离它最近的偶数print(round(2.5, 0)) #2.0print(round(1.5, 0)) #2.0a = 1627731print(round(a, -2)) #1627700

1.5、无穷

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

print(f"float('inf') = {float('inf')}") #infprint(f"float('-inf') = {float('-inf')}") #-infprint(f"float('nan') = {float('nan')}") #nanimport mathprint(f"float('inf') type is inf: {math.isinf(float('inf'))}") #trueprint(f"float('nan') type is nan: {math.isnan(float('nan'))}") #ture

二、浮点数

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

a = 2.1b = 4.2print(a + b) # 这处是有误差的6.300000000000001# 浮点数要用这种格式from decimal import Decimala = Decimal('2.1')b = Decimal('4.2')print(f'a + b = {a + b}') # a + b = 6.3from decimal import localcontexta = Decimal('1.3')b = Decimal('1.7')print(f'a / b = {a / b}') # a / b = 0.7647058823529411764705882353with localcontext() as ctx: ctx.prec = 3 # 位数 print(f'a / b = {a / b}') # a / b = 0.765with 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.0import mathprint(f'math sum result: {math.fsum(num_list)}') # math sum result: 1.0

三、分数

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

from fractions import Fractiona = Fraction(5, 4)b = Fraction(7, 16)print(f'{a} + {b} = {a + b}')print(f'{a} * {b} = {a * b}')c = a * bprint(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.75print(f'{x} to fractions is: {Fraction(*x.as_integer_ratio())}')5/4 + 7/16 = 27/165/4 * 7/16 = 35/64numerator of 35/64 is: 35denominator of 35/64 is: 64float(35/64) = 0.54687535/64 limit denominator 8 = 4/73.75 to fractions is: 15/4

四、随机数

import randomvalues = [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)}')

五、Numpy

这是一个三方的库,是很多科学与工程库的基础。在机器学习中应用广泛。下面只举两个简单的例子。

5.1、数组运算

import numpy as npax = np.array([1, 2, 3, 4])ay = np.array([5, 6, 7, 8])print(f'{ax} * 2 = {ax * 2}') #[2 4 6 8]print(f'{ax} + 10 = {ax + 10}') #[11 12 13 14]

5.2、矩阵运算

import numpy as np"""[[ 1 -2 3] [ 0 4 5] [ 7 8 -9]]"""m = np.matrix([[1,-2,3],[0,4,5],[7,8,-9]])print(f'm is:\n{m}')"""[[ 1 0 7] [-2 4 8] [ 3 5 -9]]"""print(f'm.T is:\n{m.T}')"""[[ 0.33043478 -0.02608696 0.09565217] [-0.15217391 0.13043478 0.02173913] [ 0.12173913 0.09565217 -0.0173913 ]]"""print(f'm.I is:\n{m.I}')v = np.matrix([[2],[3],[4]])print(f'v is:\n{v}')"""[[ 8] [32] [ 2]]"""print(f'm * v is:\n{m * v}')


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python从门到精通(一):基础-03-Date和Zone(python date())
下一篇:【推荐】Python中你必须要知道的IDE工具!(四种常用于python的IDE)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~