Python actual combat (IV) | number
feiren_ java 2021-07-25 10:35:51
01 Preface

My brother is a dog , A programmer . done Android、 Rolled over Java、 At present, I am studying myself Python . register 「 A good loser 」 This company has been for some days , The idea that really wants to operate it is the idea that came into being in these two days . The original intention of registering this number is to share my Python Learning notes . A knowledge , You know , Not really understand , You can make others understand , Is to really master . Share , It's the best review process .

02 What is? Python Numbers (Number)

Python The numeric data type is used to store values .
Data type cannot be changed , This means that if you change the value of a numeric data type , Memory space will be reallocated .
The following example is used in variable assignment Number Object will be created ：

```var1 = 1
var2 = 10
```

You can also use the del Statement to delete references to some numeric objects .
del The syntax of the sentence is ：

```del var1[,var2[,var3[....,varN]]]]
```

You can use the del Statement to delete references to single or multiple objects , for example ：

```del var
del var_a, var_b
```

Python Three different numerical types are supported ：

• integer (Int) - It's usually called an integer or an integer , It's a positive or negative integer , Without decimal point .Python3 There is no limit to the size of an integer , Can be viewed as Long Type used , therefore Python3 No, Python2 Of Long type .
• floating-point (float) - Floating point type consists of integer part and decimal part , Floating point types can also be represented by scientific notation （2.5e2 = 2.5 x 102 = 250）
• The plural ( (complex)) - The plural consists of the real part and the imaginary part , It can be used a + bj , perhaps complex(a,b) Express , The real part of a complex number a Deficiency part of harmony b It's all floating point .

We can use hexadecimal and octal to represent integers ：

```>>> number = 0xA0F # Hexadecimal
>>> number
2575
>>> number=0o37 # octal
>>> number
31
```
int float complex
10 0.0 3.14j
100 15.20 45.j
-786 -21.9 9.322e-36j
080 32.3+e18 .876j
-0490 -90. -.6545+0J
-0x260 -32.54e100 3e+26J
0x69 70.2-E12 4.53e-7j

Python Support complex number , The plural consists of the real part and the imaginary part , It can be used a + bj, perhaps complex(a,b) Express , The real part of a complex number a Deficiency part of harmony b It's all floating point .

#### Python Digital type conversion

occasionally , We need to transform the built-in types of data , Conversion of data types , You just need to use the target data type you want to convert as the function name .

grammar ： Target data type ( Variable )

• int(x) take x Convert to an integer .

• float(x) take x Converts to a floating point number .

• complex(x) take x To a complex number , The real part is x, The imaginary part is 0.

complex(x, y) take x and y To a complex number , The real part is x, The imaginary part is y.x and y It's a number expression .
The following example shows floating point variables a Convert to integer ：

```>>> a = 1.0
>>> int(a)
1
```
03 Python Number operation

Python The interpreter can be used as a simple calculator , You can enter an expression in the interpreter , It will output the value of the expression .
The syntax of expressions is straightforward ： +, -, ***** and /, And other languages （ Such as Java or C） Same as in . for example ：

```>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # Always return a floating point number
1.6
```

Be careful ： The result of floating-point operation may be different on different machines .

In integer division , division / Always return a floating point number , If you just want to get the result of integers , Discard possible fractions , You can use operators // ：

```>>> 17 / 3 # Integer division returns floating point
5.666666666666667
>>>
>>> 17 // 3 # Integer division returns the result of rounding down
5
>>> 17 % 3 # ％ The operator returns the remainder of the division
2
>>> 5 * 3 + 2
17
```

Be careful ：// What you get is not necessarily a number of integer type , It has something to do with the data type of denominator .

```>>> 7//2
3
>>> 7.0//2
3.0
>>> 7//2.0
3.0
```

Equal sign = Used to assign values to variables . After the assignment , Except for the next prompt , The interpreter doesn't show any results .

```>>> width = 20
>>> height = 5*9
>>> width * height
900
```

Python have access to ""** Operation to do the power operation ：

```>>> 5 ** 2 # 5 The square of
25
>>> 2 ** 7 # 2 Of 7 Power
128
```

Variables must be changed before they are used " Definition "（ That is to give a value to the variable ）, Otherwise, an error will occur ：

```>>> n # Trying to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
```

Different types of mixed operations convert integers to floating-point numbers ：

```>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5
```

In interactive mode , Finally, the output expression result is assigned to the variable _ . for example ：

```>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
```

here , _ Variables should be treated by the user as read-only variables .

04 Mathematical functions

Python Contains the following common mathematical functions ：

function Return value ( describe )
abs(x) Returns the absolute value of the number , Such as abs(-10) return 10
ceil(x) Returns an upinteger of a number , Such as math.ceil(4.1) return 5
cmp(x, y) If x < y return -1, If x == y return 0, If x > y return 1. Python 3 obsolete . Use Use (x>y)-(x<y) Replace .
exp(x) return e Of x The next power (ex), Such as math.exp(1) return 2.718281828459045
fabs(x) Returns the absolute value of the number , Such as math.fabs(-10) return 10.0
floor(x) Returns the rounded value of a number , Such as math.floor(4.9) return 4
log(x) Such as math.log(math.e) return 1.0,math.log(100,10) return 2.0
log10(x) Return to 10 For the base x The logarithmic , Such as math.log10(100) return 2.0
max(x1, x2,...) Returns the maximum value of the given parameter , Parameters can be sequences .
min(x1, x2,...) Returns the minimum value for a given parameter , Parameters can be sequences .
modf(x) return x The integral and fractional parts of , Two parts of the numerical symbols and x identical , The integer part is represented as a floating point .
pow(x, y) x**y The calculated value .
round(x [,n]) Return floating point number x Round the value of , Such as given n value , Represents the number rounded to the decimal point .
05 Random number function

Random numbers can be used in Mathematics , game , In areas such as security , It's often embedded in algorithms , To improve the efficiency of the algorithm , And improve the security of the program .
Python Contains the following common random number functions ：

function describe
choice(seq) Pick one element at random from the elements of the sequence , such as random.choice(range(10)), from 0 To 9 Pick an integer at random .
randrange ([start,] stop [,step]) From the specified range , Gets a random number from a set incremented by the specified cardinality , The cardinality default is 1
random() I'm going to randomly generate the next real number , It's in [0,1) Within the scope of .
seed([x]) Change the seed of the random number generator seed. If you don't know how it works , You don't have to specify seed,Python Will help you choose seed.
shuffle(lst) Sort all the elements of the sequence at random
uniform(x, y) I'm going to randomly generate the next real number , It's in [x,y] Within the scope of .
06 Trigonometric functions

Python Include the following trigonometric functions ：

function describe
acos(x) return x Of arc cosine radians .
asin(x) return x Of arc sine radians .
atan(x) return x The arctangent of PI radians .
atan2(y, x) Returns the given X And Y The inverse tangent of the coordinate value .
cos(x) return x Cosine of PI radians .
hypot(x, y) Returns the Euclidean norm sqrt(xx + yy).
sin(x) Back to x Sine of radians .
tan(x) return x Tangent of radians .
degrees(x) Convert radians to degrees , Such as degrees(math.pi/2) , return 90.0
07 Mathematical constant
Constant describe
pi Mathematical constant pi（ PI , General with π To express ）
e Mathematical constant e,e That is the constant of nature （ constants ）.
08 Afterword

Okay , The article is over , Don't be surprised if you see similar articles elsewhere , It's just that someone is jealous of my talent , Let them go . Don't talk much , I have specially arranged a mind map for you so that you can understand and remember the knowledge of this chapter . That's all for this section , Next section ： self-taught Python The string of (String)

Similar articles

2021-06-04

2021-06-04

2021-06-04

2021-06-05

2021-06-05

2021-06-05

2021-06-06

2021-06-06

2021-06-07

2021-06-07

2021-06-09

2021-06-09

2021-06-09

2021-06-09

2021-06-09

2021-06-09

2021-06-10

2021-06-11

2021-06-12

2021-06-12

2021-06-16

2021-07-24

2021-07-25

2021-07-28

2021-07-30

2021-08-02

2021-08-04

2021-08-05