Some advanced additions to Python

Python advanced supplement, refer to the book "Python3 from entry to actual combat".


Refer to "Python3 from entry to actual combat"

1. You can use isinstance() to determine whether an object is a certain type of object

print(isinstance(2,int)) #判断2int类型的对象吗?
运行结果如下:
True

Second, Python can express the base by prefixing the number:

1. Add 0b or 0B to represent the binary number;

2. Add prefix 0o or 0O to represent octal number;

3.Add prefix 0x or 0X to represent hexadecimal number.

print(0b0110100101)
print(0o703516)
print(0x7F3BA)
运行结果如下:
421
231246
521146

Three: You can use the built-in functions bin(), oct(), and hex() to get the corresponding string of a number in binary, octal, and hexadecimal:

print(bin(412))
print(oct(412))
print(hex(412))
运行结果如下:
0b110011100
0o634
0x19c

Four: Python provides high-precision Decimal class and Fraction class for fraction calculation:

import Decimal
from fractions import Fraction as F
print(0.1)
print(F(1,3))#表示分数1/3
print(1/F(5,6))#表示分数6/5

#Python用于数值计算的函数
print(abs(-3.4))
print(min(3.4,2.8))
print(max(3.4,2.8))
print(pow(0.3,4))
print(round(2.8))#取最接近的整数
print(round(2.3))
print(round(-2,3))
运行结果如下:
3.4
2.8
3.4
0.0081
3
2
-2

Five: Import the math module:

	import math
    print("*"*50)
    print(math.pi)
    print(math.e)
    
    #浮点计算可能产生两个异常值,inf和nan,也可能抛出一个OverflowError异常。
    #当浮点数超过float类型数值表示范围时,产生的结果是无穷大(inf)
    #并不是所有溢出的值都用inf表示,结果是inf还是异常是底层C Python决定的
    
    print("*"*50)
    x = 10.0 ** 200
    y = x*x
    print(y)
    print(math.isinf(y)) 
    
    #如果除inf的结果是未定的,则结果是nan(未定的数值).
    #因为nan不能和其他值进行比较,所以只能用函数ianan()检查nan
    
    print("*"*50)
    x = (10.0) ** 200 * (10.0) ** 200
    y = x/x
    print(y)
    print(math.isnan(y))
    
	运行结果如下:
	**************************************************
	3.141592653589793
	2.718281828459045
	**************************************************
	inf
	True
	**************************************************
	nan
	True

Six: You can also use the function isfinite() to check whether a value is a regular value or a special value inf or nan

import math
for i in [math.pi,math.nan,math.inf]:
	print(math.isfinite(i))
运行结果如下:
True
False
False

Seven: equality comparison of floating-point numbers

1.isclose(a,b,*,rel_tol=le-09,abs_tol=0.0): The default parameters rel_tol and abs_tol represent relative error and absolute error respectively

import math
a = 1.0 - 0.99
b = 100 - 99.99
print(a==b)
print(math.isclose(a, b))
运行结果如下:
False
True

Eight: floating-point number converted to integer

1. The function trunc() truncates the decimal part of a floating-point number

2. The function floor() can be converted to the largest integer smaller than it

3. The function ceil() converts a floating point number to the smallest integer larger than it

import math
a = 1.8
print(math.trunc(a))
print(math.floor(a))
print(math.ceil(a))
运行结果如下
1
1
2

4.sqrt(x) is used to calculate the square root of a number x

5.pow(x,y) is used to calculate the y power of x, similar to x ** y, but pow (x, y) can be guaranteed to operate according to floating point numbers, and x ** y can only return an int or float

6.exp(x) Exponential function with natural constant as the base

7.expml(x) calculate e**x-1

8. The logarithmic function log(x,base) has base and some variants log2(x) log10(x) log1p(x):

from math import *
print(sqrt(9))
print(pow(3,3))
print(exp(1))
print(expm1(1))
print(log10(10))
print(log1p(1))
#什么都不写就是ln
print(log(e))
运行结果如下:
3.0
27.0
2.718281828459045
1.718281828459045
1.0
0.6931471805599453
1.0

Nine: Angles and radians

1. The function radians() converts the angle value into radians

2. The function degrees() converts a radian value into an angle value

from math import *
print(radians(45))
print(degrees(pi))
运行结果如下:
0.7853981633974483
180.0

Ten: The math module contains various trigonometric functions and inverse trigonometric functions such as sin, cos, tan, atan.

from math import *
print(sin(0))
print(cos(0))
print(tan(0))
print(degrees(atan(1)))#转化成角度了

运行结果如下:
0.0
1.0
0.0
45.0

11. Random number: random module

import random
l = [1,2,3,4]
print(random.random())#0-1的随机小数
print(random.randint(100, 900))#100-900的随机数
print(random.choice(l))#任意选取一个列表中的元素
random.shuffle(l)#打乱列表
print(l)

'''
0.9724452742237619
188
1
[1, 2, 4, 3]
'''


Guess you like

Origin blog.csdn.net/qq_45911278/article/details/110311326