Learn any programming language , Including but not limited to C,C++,Java,Python, We all need to understand its basic data types first : It can be summarized as integer , Floating point and string ——
Because all program code will use these .

1 Python Convenient mathematical operation

1.1 Integer calculation

first , Well known integers are integers without decimal parts ,Python support 4 Basic arithmetic operation :+( plus ),-( reduce ),*( ride ) and /( except ). And use ** and % Represents the sum of powers and the remainder .
And its priority is consistent with the priority learned in mathematics . 

python An arithmetic of integral division is also provided in , The operator is // , Principle and / similar , But the result is always an integer
. The result of this operator is that the value after the decimal point is always discarded , Instead of rounding .

It can be concluded that python The basic operators of are summarized into a table , See the table below :

name

operator

addition

+

subtraction

-

multiplication

*

division

/

Power

**

Surplus

%

to be divisible by

//

also ,Python One difference from most other programming languages is , There is no limit on the length of integers . It can perform operations of tens or even more bits . 

1.2 Floating point calculation

Python It not only provides support for integer operations , It also provides convenient operation for our commonly known decimal . Its operators and rules are basically consistent with integer calculation , Here are a few small examples :

among ,e-09 express 10-9,Python The application of decimal point is very flexible , However, there is also a small approximation error , such as 9.1*0.06 Value of .
But one thing to note is :Python Some versions are bit limited for floating point numbers ( Compare the following two figures ), Therefore, overflow or unknown error may occur , In the process of real development , Try not to write such code ( Otherwise, carry the pot ).
 

1.3 Complex number calculation

Python Complex number calculation is also supported , The plural is generally a design -1 The number of the square root of , stay Python Generally used in 1j representative -1 Square root of . for example :

The calculation of complex numbers is usually used in statistics and scientific calculation , Ordinary developers rarely use the plural .

1.4 Built in mathematical function

In addition to some basic mathematical operations ,Python There are also many built-in mathematical function operations , For example, absolute value , Maximum , minimum value , Round floating point numbers , Sum, etc . for example :

1.abs() Function to find the absolute value of a value ;

2.max() Function for maximum ;

3.min() Function to find the minimum value ;

4.divmod() Function to find the quotient and remainder of two numbers ;

5.round() Function rounds a floating-point number ;

6.pow() Function can calculate the power of two values , Or the power operation of the first two of the three values calculates the remainder of the third value ;

7.sum() Function iteratively calculates the sum of each object ; Attention should be paid to the use of parentheses

For more complex mathematical operations ,Python Encapsulates a feature rich package :math
Some commonly used mathematical formulas are listed , See the table below :

function

meaning

ceil(x)

Greater than or equal to x Integer of

sin(x)

x Sine of

cos(x)

x Cosine of

tan(x)

x Tangent of

degrees(x)

take x Radians to degrees

exp(x)

e of x Power

factorial(n)

calculation n factorial (n!).n! = 1*2*3…*n, among n Must be an integer

log(x)

with e Bottom x Logarithm of

log(x, b)

with b Bottom x Logarithm of

pow(x, y)

x of y Power

sqrt(x)

x Square root of

radians(x)

take x Degrees to arc degrees

  Next, let's look at some examples , Note: import first math package , Use command :import math. of course , You can also use additional commands for importing tool libraries , as :from math import
ceil( Import specific functions ), It depends on what functionality your code needs .

2 Simple string processing

2.1 String identification

In addition to digital processing ,Python It also provides powerful string parsing function . Need to know , In the process of daily programming , Strings are also tasks that we cannot avoid . character string — What is enclosed in quotation marks
! for example :"12345","my name","more 123" wait , Include numbers , letter , Punctuation and many special symbols .
stay Python in , There are three main ways to represent strings 1. Single quotation mark -' ': as 'helloworld',' Hello '; 2. Double quotation mark -"
": as "helloworld","verygood"; 3. Three quotation marks -''' ''': as '''ni hao ma wo ting hao de ni ne
''' The content can be a multiline string
So when writing programs , To reduce input , Prefer to use single quotes to identify strings . But when you need to use a multi line string , Using three quotation marks is a better choice . 

2.2 String length

In programming , When you need to determine the length of a string , You can't count it with the naked eye . stay Python in , Provided len Function to see the length of a string . as :

Can see ,Python The format of the input is very strict , At the wrong point marked in red , The author just typed one more space , So pay more attention ! We can also use it where integers are used len() Function to evaluate integers .

2.3 Simple splicing of strings

The simple splicing of strings can be understood as adding strings to create a new string , As the following examples :

If you need to splice the same string several times , Then we can do this

3 Type conversion

In coding , The conversion between different field types is a task that programmers often encounter . For the implementation of such tasks , It also provides very convenient and easy-to-use built-in functions !

1. Convert integers and strings to floating point numbers :float() function , Of course, the string here refers to the string with numbers in quotation marks ;

2. Converts integers and floating-point numbers to strings :str() function ;

3. Convert floating point numbers to integers :int(),round() function ;

4. Convert string to number ,int() perhaps float() function ; It should be noted that ,int() Function is a function that converts an integer string ;

In daily learning , at work , use int(x),
float(x) and round(x) Can meet the needs of numerical conversion . But there are also requirements to deal with more specific transformations ,math() Function provides a number of functions to remove decimal parts :math.trunc(),math.ceil() and math.
floor(). If you are interested, you can try it yourself in combination with the documents .

Technology
©2019-2020 Toolsou All rights reserved,
Android Using wechat in H5 Payment result refresh during payment shock !!C++ Can make a sound ! Basic operation of single linked list C Language explanation Java Implement an epidemic number management system 2021 year 11 World programming language ranking linux upper mysql Invalid command _linux lower mysql The command is useless Java project : Campus dormitory management system (java+jsp+javaweb+mysql+ajax) Wechat applet development project directory linux ubuntu Which version ,Ubuntu Which version is the best ?python Code painting cherry blossoms - How to use Python Draw a beautiful cherry blossom