python scientific computing ecology-002-number

python scientific computing ecology-002-number

digital:

Numbers can be accessed directly and are an unchangeable and indivisible atomic type. Unchangeable means that the essence of changing a numeric value is the creation of a new object. Python itself supports integers and floating-point numbers, and its integer type can store integers of any size (the range of numbers that can be expressed is related to the virtual memory size of the computer), which makes Python very suitable for large number calculations.

Python can perform large number calculations

Creation and assignment of digital objects

# 像大多数脚本语言一样,无需指定类型
a_int = 1
a_float = 3.1415

Boolean value

bool() # False
bool(1) # True
bool(0) # False
bool(True) # True
bool(False) # False
print(True+False) # 1  -->所以t和f可以看做1和0进行运算

Boolean operation

and or notThree Boolean operator
operation priority: not> and>or

plural

real + imag j

  • Both the real part and the imaginary part are floating-point types , and the end of the imaginary part must be j or J.
  • Complex numbers contain two floating-point properties: real (real part), imag (imaginary part), and a method:, conjugate()to get its conjugate complex number.
a_complex = 3.5 + 2.9j
print(a_complex) # (3.5 + 2.9j)
type(a_complex) # complex-->复数类型
print(a_complex.real) # 3.5 -->获取复数的真值部分
print(a_complex.imag) # 2.9 -->获取复数的虚部的值
a_complex.conjugate() # 获取虚数的共轭复数
# 当然实数也可以用这个函数,并且返回值就是他本身
a_real = 3
a_real.conjugate()

Update the digital object (ie reassign value)

Essence: creating a new object and pointing to the new object does not change the value of the original object

a_float = 3.14
a_float = 3.14159

'Delete' digital objects

del a_float
a_float # 报错啦

tip:
Pay attention to the relationship between reference counts and objects. When the reference count is 0, the object will not exist.

Mathematical operations in python

Operator description Example result
+ addition 5 + 8 13
- Subtraction 90 - 10 80
* multiplication 4 * 7 28
/ Floating point division 7 / 2 3.5
// Integer division 7 // 2 3
% Modulus (remainder) 7 % 3 1
** power 3 ** 4 81

Some operations assignment merge

+= -= *= /= //=
1/2 # 0.5
2/2 # 1.0 -->这里进行的是浮点数除法运算
1//2 # 0 -->整数整除运算
1.0//2.0 #0.0 --> 浮点数整除运算

division

  • /Floating point division performed
  • 5/0If the divisor is 0, an ZeroDivisionErrorexception will be generated

Base

Three base numbers

0b10 or 0B10 二进制
0o10 or 0010 八进制
0x10 or 0X10 十六进制

Base conversion

Simple type to complex type conversion
Inaccurate type to more precise type conversion.
Failure of type conversion will generate ValueErroran exception.

# 都会报错
int('10a')
int('98.6')

Mathematical function

int()
float()
complex()
bool()
complex() ## 只有虚部不可,但只有实部可
bool('') # False

Function function

abs()The absolute value function and can be used for complex absolute value operations, that is, it returns the square root of the sum of squares of the real and imaginary parts.
divmod()This function combines division and remainder, and returns a tuple containing the quotient and remainder.
pow()This function can achieve exponentiation.
round()This function can achieve true rounding

divmod(10,3) # (3,1)
divmod(2.5,10) # (0.0,2.5)
pow(5.2) #25
round(4.499) # 4
round(4.6) # 5
round(4.51) #5
round(4.492,2) # 4.49

Guess you like

Origin blog.csdn.net/qq_42906486/article/details/105262711