第二部分 认识数据¶

数字,文字,符号.......

一、字符串(str)¶

In [ ]:
name = '张三'
field = '经济学'
print(name + '在西华大学就读' + field + '专业' )
In [ ]:
number = "666"
symbol = '''&@#¥A!@'''
mixture = 'sadda===666&@#¥A!@'
print(number)
print(symbol)
print(mixture)

二、整数(int)¶

In [ ]:
a = 10
b = -60
c = 6841
d = 0
print(a)
print(b)
print(c)
print(d)
In [ ]:
print(6真实)
In [ ]:
print('6')

三、浮点数(float)¶

In [ ]:
a = 1.0
b = 3.14159
c = -0.33
print(a)
print(b)
print(c)
In [ ]:
print(0.55+0.3)
In [ ]:
round(0.85, 1)
In [ ]:
round(1.25361,3)
In [ ]:
print(round(1.5, 0))
print(round(2.5, 0))
In [ ]:
a = 1627731
print(round(a, -1))

print(round(a, -2))

print(round(a, -3))
In [ ]:
x = 1.23456
print(format(x, '0.2f'))

print(format(x, '0.3f'))

'value is {:0.3f}'.format(x)
In [ ]:
a = 2.1
b = 4.2
c = a + b
c
In [ ]:
c = round(c, 2) 
c
In [ ]:
a = 4.2
b = 2.1
a + b
In [ ]:
(a + b) == 6.3
In [ ]:
from decimal import Decimal
a = Decimal('4.2')
b = Decimal('2.1')
print(a + b)
In [ ]:
(a + b) == Decimal('6.3')
In [ ]:
from decimal import localcontext
a = Decimal('1.3')
b = Decimal('1.7')
print(a / b)
In [ ]:
with localcontext() as ctx:
    ctx.prec = 3
    print(a / b)
In [ ]:
with localcontext() as ctx:
    ctx.prec = 50
    print(a / b)
  • 以下是 Python 中支持的几种常见的舍入模式,定义在 decimal 模块中的常量中:
  1. ROUND_CEILING:向正无穷方向舍入。
  2. ROUND_FLOOR:向负无穷方向舍入。
  3. ROUND_DOWN:舍去小数部分(即向 0 方向舍入)。
  4. ROUND_UP:小数部分不为 0 时,向远离 0 的方向舍入。
  5. ROUND_HALF_UP:四舍五入(>=0.5 向上,<0.5 向下)。
  6. ROUND_HALF_DOWN:五舍六入(>0.5 向上,<=0.5 向下)。
  7. ROUND_HALF_EVEN:银行家舍入法(当小数部分为 0.5 时,向最接近的偶数舍入)。
  8. ROUND_05UP:如果小数部分是 0 或 0.5,向上舍入。
In [ ]:
from decimal import  ROUND_HALF_UP, ROUND_DOWN
with localcontext() as ctx:
    ctx.prec = 3
    ctx.rounding = ROUND_DOWN
    print(a / b)

四、二八十六进制¶

In [ ]:
x = 1234
print(bin(x))
print(oct(x))
print(hex(x))
In [ ]:
print(format(x, 'b'))
print(format(x, 'o'))
print(format(x, 'x'))
In [ ]:
x = 0x4d2
print(x)
In [ ]:
int('4d2', 16)
In [ ]:
int('10011010010', 2)
In [ ]:
int('2322', 8)

五、数据运算¶

In [ ]:
print(2+1)
print(2-1)
print(2*2)
print(6/3)
print(5%2)
print(5//2)
print(2*2)
  • 分数计算
In [ ]:
from fractions import Fraction
a = Fraction(5, 4)
b = Fraction(7, 16)
print(a + b)
print(a * b)
In [ ]:
c = a * b
print(c.numerator)
print(c.denominator)
In [ ]:
float(c)
In [ ]:
print(c.limit_denominator(8))  #分母不超过8  最接近的
In [ ]:
x = 3.75
y = Fraction(*x.as_integer_ratio())
y
  • 大型数组运算
  1. NumPy 是Python领域中很多科学与工程库的基础
  2. 一个数组对象,相比标准的Python列表而已更适合用来做数学运算
  3. 网址是: http://www.numpy.org
In [ ]:
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
print(x * 2)
# print(x + 10)
print(x + y)
x[0] = 2
print(x)
In [ ]:
import numpy as np
ax = np.array([1, 2, 3, 4])
ay = np.array([5, 6, 7, 8])
print(ax * 2)
print(ax + 10)
print(ax + ay)
print(ax * ay)
In [ ]:
import numpy as np

# 将列表转换为 NumPy 数组
x = [1, 2, 3, 4]
arr = np.array(x)
In [ ]:
def f(x):
    return 3*x**2 - 2*x + 7

f(ax)
In [ ]:
print(np.sqrt(ax))
print(np.cos(ax))
  • NumPy 数组使用了C或者Fortran语言的机制分配内存。
  • 它们是一个非常大的连续的并由同类型数据组成的内存区域。
  • 所以,你可以构造一个比普通Python列表大的多的数组。
In [ ]:
grid = np.zeros(shape=(10000,10000), dtype=float)
grid
In [ ]:
grid += 10
grid
In [ ]:
np.sin(grid)
In [ ]:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a
In [ ]:
a[1]
In [ ]:
a[:,1]
In [ ]:
a[1:3, 1:3]
In [ ]:
a[1:3, 1:3] += 10
a
In [ ]:
a + [100, 101, 102, 103]
In [ ]:
a
In [ ]:
np.where(a < 10, a, 10)
  • 矩阵
In [ ]:
import numpy as np
m = np.matrix([[1,-2,3],[0,4,5],[7,8,-9]])
m
In [ ]:
m.T
In [ ]:
m.I
In [ ]:
v = np.matrix([[2],[3],[4]])
v
In [ ]:
m * v
In [ ]:
import numpy.linalg
# 行列式
numpy.linalg.det(m)
In [ ]:
# 特征值
numpy.linalg.eigvals(m)
In [ ]:
x = numpy.linalg.solve(m, v)  #用于解线性方程组 𝐴𝑥=𝑏 
x
In [ ]:
print(m * x)
print(v)
  • 字符串的运算
In [ ]:
name = '张三'
action = '是'
organization = '西华大学'
ID = '2023级'
identity = '学生'



print(name+action+organization+ID+identity)
In [ ]:
name = '张三'
action = '是'
organization = '西华大学'
ID = '2023级'
identity = '学生'
xh = '学号'
num = 20230013491


print(name+action+organization+ID+identity+xh+action+num)
In [ ]:
name = '张三'
action = '是'
organization = '西华大学'
ID = '2023级'
identity = '学生'
xh = '学号'
num = 20230013491


print(type(name))
print(type(num))

str()
int()
float()

In [ ]:
name = '张三'
action = '是'
organization = '西华大学'
ID = '2023级'
identity = '学生'
xh = '学号'
num = 20230013491


print(name+action+organization+ID+identity+xh+action+str(num))
In [ ]:
number1 = '6'
number2 = '1'
print(int(number1)+int(number2))  # 只有符合整数规范的字符串类数据,才能被int()强制转换
In [ ]:
print(int('3.8'))  
In [ ]:
print(int(3.8)) # 虽然浮点形式的字符串,不能使用int()函数。但浮点数是可以被int()函数强制转换的

cell()向上取整
int()向下取整
round()四舍五入

In [ ]:
height = '188.0'
weight = 180
age = '89'
print(type(height))
print(float(height))
print(float(weight))
print(float(age))
In [ ]:
print(0.25+0.25-0.4)
  • 随机
In [ ]:
import random
values = [1, 2, 3, 4, 5, 6]
random.choice(values)
In [ ]:
random.sample(values, 2)
In [ ]:
random.shuffle(values)
values
In [ ]:
random.randint(0,10)
In [ ]:
random.random()
  • 有多随机?
In [ ]:
random.seed(12345) 
random.random()

六、无穷大与NaN¶

In [ ]:
a = float('inf')
b = float('-inf')
c = float('nan')
print(a)
print(b)
print(c)
In [ ]:
import math
math.isinf(a)
In [ ]:
math.isinf(b)
In [ ]:
math.isnan(c)
In [ ]:
a = float('inf')
print(a + 45)
print(a * 10)
print(10 / a)
In [ ]:
a = float('inf')
print(a/a)
b = float('-inf')
print(a + b)
In [ ]:
c = float('nan')
print(c + 23)
print(c / 2)
print(c * 2)
print(math.sqrt(c))
In [ ]:
c = float('nan')
d = float('nan')
print(c == d)
c is d

#测试一个NaN值得唯一安全的方法就是使用 math.isnan()