Python3 digit Number (6), python3 digit number

Source: Internet
Author: User
Tags integer division mathematical constants mathematical functions

Python3 digit Number (6), python3 digit number

The Python numeric data type is used to store numeric values.

The data type cannot be changed, which means that if you change the value of the numeric data type, the memory space will be re-allocated.

The Number object will be created when the following instances assign values to variables:

1 var1 = 12 var2 = 10

You can also use the del statement to delete some numeric object references.

The syntax of the del statement is:

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

You can use the del statement to delete the reference of one or more objects. For example:

1 del var2 del var_a, var_b

Like most languages, value assignment and computation are intuitive.

The built-in type () function can be used to query the object type referred to by a variable.

1 >>> a, b, c, d = 20, 5.5, True, 4+3j2 >>> print(type(a), type(b), type(c), type(d))3 <class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

In addition, you can use isinstance to judge:

1 >>>a = 1112 >>> isinstance(a, int)3 True4 >>>

The difference between isinstance and type is:

 1 class A: 2     pass 3  4 class B(A): 5     pass 6  7 isinstance(A(), A)  # returns True 8 type(A()) == A      # returns True 9 isinstance(B(), A)    # returns True10 type(B()) == A        # returns False

The difference is:

  • Type () is not considered as a type of parent class.
  • Isinstance () considers subclass as a parent class type.

Python3 supportInt, float, bool (Boolean), complex (plural).

  • INTEGER (Int)-It is usually called an integer or integer. It is a positive or negative integer without a decimal point. Python3 has no size limit and can be used as the Long type. Therefore, Python3 does not have the Long type of Python2.
  • Float)-The floating point type consists of the integer part and the decimal part. The floating point type can also be expressed by scientific notation (2.5e2 = 2.5x102 = 250)
  • Boolean (bool)-In Python3, True and False are defined as keywords, but their values are still 1 and 0. They can be added together with numbers .. In Python2, there is no boolean type. It uses numbers 0 to indicate False, and 1 to indicate True.
  • Plural (complex ))-A complex number consists of a real number and a virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual part B of a complex number are float.
Boolean (bool)

In Python, you can directly useTrue,FalseBoolean value (case sensitive), which can also be calculated using the Boolean operation:

1 >>> True 2 True 3 >>>> False 4 False 5 >>>3> 2 6 True 7 >>> 3> 5 8 False 9 10 # boolean type can be involved in the operation 11 >>> True + 112 213 >>>> False + 114 115 >>>> True = 116 True17 >>>> True = 218 False19 >>> False = 120 False21>> False = 022 True

Boolean value can be usedand,orAndnotOperation.

andOperation and operation, onlyTrue,andThe calculation result isTrue:

1 >>> True and True2 True3 >>> True and False4 False5 >>> False and False6 False7 >>> 5 > 3 and 3 > 18 True

orOperation is or, as long as one of them isTrue,orThe calculation result isTrue:

1 >>> True or True2 True3 >>> True or False4 True5 >>> False or False6 False7 >>> 5 > 3 or 1 > 38 True

notThe operation is not an operation. It is a single-object operator.TrueChangeFalse,FalseChangeTrue:

1 >>> not True2 False3 >>> not False4 True5 >>> not 1 > 26 True

Boolean values are often used in condition judgment, for example:

1 1 if age >= 18:2 2     print('adult')3 3 else:4 4     print('teenager')

Plural (complex ))

A complex number consists of a real number and a virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual part B of a complex number are float. Example: 3 + 26j

Generate a real number and convert it into a plural number through a real number:

1 >>> a=0.92 >>> b=complex(a)3 >>> b4 (0.9+0j)5 >>>

Define a plural number directly. The command is as follows:

1 >>> c=0.1+0.7j2 >>> c3 (0.1+0.7j)4 >>>

Use. real to access multiple real parts:

1 >>> d=9+8j2 >>> d.real3 9.04 >>>

Use. imag to access the plural virtual part:

1 >>> d=9+8j2 >>> d.real3 9.04 >>> d.imag5 8.06 >>>

Modulus of the plural using abs:

1 >>> d=9+8j2 >>> abs(d)3 12.0415945787922964 >>>

 

Python numeric type conversion

Sometimes, we need to convert the built-in data types and convert the data types. You only need to use the data type as the function name.

  • Int (x)Converts x to an integer.

  • Float (x)Converts x to a floating point number.

  • Complex (x)Convert x to a plural number. The real part is x, and the imaginary part is 0.

  • Complex (x, y)Convert x and y to a plural number. The real part is x, and the imaginary part is y. X and y are numeric expressions.

The following example converts the floating point variable a to an integer:

1 >>> a = 1.02 >>> int(a)3 1
1 >>> a=92 >>> b=83 >>> complex(a)4 (9+0j)5 >>> complex(a,b)6 (9+8j)7 >>>
Python numeric operations

The Python interpreter can be used as a simple calculator. You can enter an expression in the interpreter, which outputs the value of the expression.

The expression syntax is straightforward: +,-, * and/are the same as those in other languages (such as Pascal or C. For example:

1 >>> 2 + 22 43 >>> 50-5*64 205 >>>( 50-5*6) /46 5.07> 8/5 # Always Returns a floating point number of 8 1.6

Note:The results of floating point operations on different machines may be different.

In integer division, Division (/) always returns a floating point number. If you only want to get the result of an integer and discard the possible fraction, you can use the operator//:

1 >>> 17/3 # integer division returns floating point type 2 5.6666666666666673 >>>> 4 >>> 17 // 3 # integer division returns the result rounded down 56 >>> 17% 3 # % operator returns the remainder of Division 7 28 >>> 5*3 + 2 9 17

Equal sign (=) is used to assign values to variables. After the value is assigned, the interpreter will not display any results except the next prompt.

1 >>> width = 202 >>> height = 5*93 >>> width * height4 900

Python can be used**Operation:

1> 5 ** 2 #5 of the square 2 253> 2 ** 7 #2 of the 7 Power 4 128

The integer is converted to a floating point number during mixed operation of different types of numbers:

1 >>> 3 * 3.75 / 1.52 7.53 >>> 7.0 / 24 3.5

In interactive mode, the output expression result is assigned to the variable._. For example:

1 >>> tax = 12.5 / 1002 >>> price = 100.503 >>> price * tax4 12.56255 >>> price + _6 113.06257 >>> round(_, 2)8 113.06

Here,_Variables should be treated as read-only variables.

Mathematical functions
Function Return Value (description)
Abs (x) Returns the absolute value of a number. For example, abs (-10) returns 10.
Ceil (x) Returns the upper integer of a number. For example, math. ceil (4.1) returns 5.

Cmp (x, y)

If x <y returns-1, if x = y returns 0, if x> y returns 1.Python 3 is obsolete. UseUse (x> y)-(x <y)Replace.
Exp (x) Returns the x power of e (ex). For example, math. exp (1) returns 2.718281828459045
Fabs (x) Returns the absolute value of a number. For example, math. fabs (-10) returns 10.0
Floor (x) Returns the rounded down integer of a number. For example, math. floor (4.9) returns 4.
Log (x) For example, math. log (math. e) returns 1.0, math. log (2.0) returns
Log10 (x) Returns the logarithm of x on the base of 10. For example, math. log10 (100) returns 2.0.
Max (x1, x2 ,...) Returns the maximum value of a given parameter, which can be a sequence.
Min (x1, x2 ,...) Returns the minimum value of a given parameter, which can be a sequence.
Modf (x) Returns the integer and decimal parts of x. The numerical symbols of the two parts are the same as those of x, and the integer part is expressed as a floating point.
Pow (x, y) The value after the x ** y operation.
Round (x [, n]) Returns the rounding value of floating point x. If n is given, it indicates the number of digits rounded to the decimal point.
Sqrt (x) Returns the square root of x.

Note: The round function is a pitfall!

1 >>> round(10.5)2 103 >>> round(11.5)4 125 >>>
1 round(2.355,2)2 2.35

Unless there are no requirements for accuracy, avoid using the round () function whenever possible. We have other options for approximate calculation:

  • Use some functions in the math module, such as math. ceiling (ceiling Division ).
  • Python's built-in division, which is/In python2,/in 3, and div functions.
  • String formatting can be used for truncation. For example, "%. 2f" % value (retain two decimal places and convert them into strings ...... If you want to use floating point numbers, please put on the coat of float ).
  • Of course, if you have high requirements on floating point precision, use the decimal module. If not, use the decimal module.

The address of the article about the clear explanation after searching for this question is as follows: http://www.runoob.com/w3cnote/python-round-func-note.html

Random Number Function

Random numbers can be used in mathematics, games, security, and other fields. They are often embedded in algorithms to improve algorithm efficiency and program security.

Python contains the following common random number functions:

Function Description
Choice (seq) Randomly select an element from the element of the sequence, such as random. choice (range (10), and randomly select an integer from 0 to 9.
Randrange ([start,] stop [, step]) Obtains a random number from a set that increments by the specified base number within the specified range. The default value of the base number is 1.
Random () The next real number is randomly generated within the range of [0, 1.
Seed ([x]) Change the seed of the random number generator. If you do not understand the principle, you do not need to set seed. Python will help you select seed.
Shuffle (lst) Random sorting of all elements in a sequence
Uniform (x, y) The next real number is randomly generated, which is within the range of [x, y.
Trigonometric function
Function Description
Acos (x) Returns the arc cosine radians of x.
Asin (x) Returns the arc sine radians of x.
Atan (x) Returns the arc tangent radians of x.
Atan2 (y, x) Returns the arc tangent of the given X and Y coordinate values.
Cos (x) Returns the cosine of the radians of x.
Hypot (x, y) Returns the euclidean norm sqrt (x * x + y * y ).
Sin (x) Returns the sine of x radians.
Tan (x) Returns the tangent of x radians.
Degrees (x) Converts radians to degrees. For example, if degrees (math. pi/2), 90.0 is returned.
Radians (x) Converts degrees to radians.
Mathematical constants
Constant Description
Pi Mathematical constant pi (circumference rate, usually expressed by π)
E Mathematical constants e, e are natural constants (natural constants ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.