Day 2 Python Numerical Computing

1. Numerical data types

  In Python, there are two types of numeric data types:

  • integer

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 number

Floating point numbers are represented by the "float" data type. Data of type float can be represented in mathematical notation and scientific notation.

The way integers and floating-point numbers are stored inside the computer is different. Integer operations are always exact (and division is also exact), while floating-point operations may have rounding errors.

  Python provides the type() function, which can give the data type of any value.

1 a = 3
2 b = 3.1415926
3 print(type(a))
4 print(type(b))

  The result is:

Second, Python built-in numerical operations

  operator operation

  + add

  - reduce

  * take

  / floating point division

  ** Index

  abs() absolute value

  // integer division

  % remainder

 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:

3. Type conversion and rounding

  Implicit type conversion: In mixed-type expressions, Python automatically converts an int to a floating-point number and performs floating-point operations to yield a floating-point number.

  Explicit type conversion: cast type conversion, which can convert numbers, strings, and input() input functions; such as int(3.3) = 3, int(3.9) = 3; float(2) = 2.0; int( "32") = 32; flaot("32") = 32.0.

  Rounding to one place method:

    1. If the value is positive, add 0.5 before using int(); int(3.14+0.5) = 3, int(3.9+0.5) = 4.

    2. A simple call using the built-in round() function; round(3.14) = 3, round(3.9) = 4.

  The simple call of the round() function is to round the float to int; if the floating-point value is rounded to the specified last few decimal places, it can also be transformed by the first method above, or round(a, n), It means rounding a to n decimal places.

Fourth, use the math library

  In addition to built-in operations, Python has a special math library that provides many other useful math functions. Commonly used are as follows:

sin(x): find the sine of x

cos(x): find the cosine of x

asin(x): find the arc sine of x

acos(x): find the arc cosine of x

tan(x): find the tangent of x

atan(x): find the arc tangent of x

hypot(x,y): Find the length of the hypotenuse of a right triangle

fmod(x,y): find the remainder of x/y

ceil(x): take the smallest integer not less than x

floor(x): Find a positive integer not greater than x

fabs(x): find the absolute value

exp(x): find e to the power of x

pow(x,y): find the y power of x

log10(x): Find the base 10 logarithm of x

sqrt(x): find the square root of x

pi: the value of pi

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324978989&siteId=291194637