First, numeric data types
In Python, there are two types of numeric data:
Integers are represented by the "int" data type. Data of type int can be positive or negative, and python can handle integers of any size.
Floating-point numbers are represented by the "float" data type. Data of type float can be expressed in mathematical notation and scientific notation.
Integers and floating-point numbers are stored internally in different ways, and integer operations are always accurate (division is also accurate), while floating-point arithmetic can have rounding errors.
Python provides the type () function, which can give the data type of any value.
1 A = 32 b = 3.14159263print(type (a))4print(type (b))
The result is:
Second, Python built-in numeric operations
operator operation
+ Plus
-Minus
* Multiply
/floating point except
* * Index
ABS () absolute value
Integer except
% take-up
1 print(3+4, 3.0+4.0, 3.0+4, 3*4, 3.0*4.0, 3.0*4, 5/2, 5//2, 5//2.0, 5/2.0, 5.0/2.0, 4**3, 4.0**3, 4 .0**2.5, ABS ( -3.5))
The result is:
Iii. type conversion and rounding
Implicit type conversion: In a mixed-type expression, Python automatically converts an int to a floating-point number and performs a floating-point operation to produce a floating-point number.
Explicit type conversions: coercion of type conversions, which can convert values, strings, and input () inputs, such as int (3.3) = 3, int (3.9) = 3;float (2) = 2.0;int ("32") = 32; Flaot ("32") = 32.0.
rounded to the single digit method:
1. If the value is positive, you can add 0.5;int (3.14+0.5) = 3, int (3.9+0.5) = 4 before using int ().
2. Use the simple call of the built-in round () function; round (3.14) = 3, round (3.9) = 4.
A simple call to the round () function is to convert a float round to int, or, if the floating-point value is rounded to a specified number of decimal places, it can also be transformed under the first method above, or round (a,n), which indicates that the n decimal is reserved for a rounding.
Iv. using the Math library
In addition to the built-in operations, Python has a special math library that provides a number of other useful mathematical functions. It is commonly used as follows:
Sin (x): Finding the sine of X
cos (x): Finding the cosine of X
ASIN (x): Finding the inverse of X
ACOs (x): Finding the inverse cosine of x
Tan (x): Find the tangent of X
Atan (x): To find the inverse tangent of X
Hypot (x, y): To find the hypotenuse length of right triangle
Fmod (x, y): Find the remainder of X/y
Ceil (x): Take the smallest integer not less than X
Floor (x): A chia integer not greater than X
Fabs (x): Absolute value
EXP (x): x power of E
Pow (x, y): The Y power of X
LOG10 (x): The logarithm of 10-bit base for X
sqrt (x): Find the square root of X
The value of the pi:π
Day 2 python numerical calculation