YouTip LogoYouTip

Python3 Number

# Python3.x Python3 Number Python number data types are used to store numerical values. Data types are immutable, which means that changing the value of a number data type will reallocate memory space. The following example creates a Number object when assigning a value to a variable: var1 = 1 var2 = 10 You can also use the del statement to delete the reference to some number objects. The syntax of the del statement is: del var1[,var2[,var3[....,varN]]] You can delete the reference to a single or multiple objects using the del statement, for example: del vardel var_a, var_b Python supports three different numerical types: * **int (integer)** - Usually referred to as an integer, it is a positive or negative integer without a decimal point. Python3 integers have no size limit and can be used as a Long type, so Python3 does not have the Long type from Python2. Boolean (bool) is a subtype of integer. * **float (floating-point)** - A floating-point number consists of an integer part and a fractional part. Floating-point numbers can also be expressed in scientific notation (2.5e2 = 2.5 x 10^2 = 250). * **complex (complex number)** - A complex number consists of a real part and an imaginary part, and can be represented as a + bj or complex(a,b). The real part a and the imaginary part b of a complex number are both floats. We can represent integers using hexadecimal and octal: >>> 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.3e+18 | .876j | | -0490 | -90. | -.6545+0J | | -0x260 | -32.54e100 | 3e+26J | | 0x69 | 70.2E-12 | 4.53e-7j | * Python supports complex numbers. A complex number consists of a real part and an imaginary part, and can be represented as a + bj or complex(a,b). The real part a and the imaginary part b of a complex number are both floats. * * * ## Python Number Type Conversion Sometimes, we need to convert the built-in data types. To convert data types, you simply use the data type as a function name. * **int(x)** converts x to an integer. * **float(x)** converts x to a floating-point number. * **complex(x)** converts x to a complex number with a real part of x and an imaginary part of 0. * **complex(x, y)** converts x and y to a complex number with a real part of x and an imaginary part of y. x and y are numeric expressions. The following example converts the floating-point variable a to an integer: >>> a = 1.0>>> int(a)1 * * * ## Python Number Operations The Python interpreter can act as a simple calculator. You can enter an expression in the interpreter, and it will output the value of the expression. The syntax of expressions is straightforward: +, -, *, and /, just like in other languages (such as Pascal or C). For example: >>> 2 + 24>>> 50 - 5*620>>> (50 - 5*6) / 45.0>>> 8 / 5 # Always returns a floating-point number1.6 **Note:** The result of floating-point operations may vary on different machines. In integer division, the division / always returns a floating-point number. If you only want the integer result and discard the possible fractional part, you can use the // operator: >>> 17 / 3 # Integer division returns a float5.666666666666667>>>>>> 17 // 3 # Integer division returns the result after floor division5>>> 17 % 3 # The % operator returns the remainder of the division2>>> 5 * 3 + 2 17 **Note:** The result of // is not necessarily an integer type; it depends on the data types of the numerator and denominator. >>> 7//23>>> 7.0//23.0>>> 7//2.03.0>>> The equals sign = is used to assign values to variables. After assignment, the interpreter does not display any result except for the next prompt. >>> width = 20>>> height = 5*9>>> width * height 900 Python can use the ** operator for exponentiation: >>> 5 ** 2 # 5 squared25>>> 2 ** 7 # 2 to the power of 7128 Variables must be "defined" (i.e., assigned a value) before use, otherwise an error will occur: >>> n # Trying to access an undefined variableTraceback (most recent call last): File "", line 1, in NameError: name 'n' is not defined When different types of numbers are mixed in an operation, the integer is converted to a float: >>> 3 * 3.75 / 1.57.5>>> 7.0 / 23.5 In interactive mode, the result of the last output expression is assigned to the variable **_**. For example: >>> tax = 12.5 / 100>>> pric
← Python3 StringPython3 Comment β†’