Python Learning "Second article": Numbers in Python and the methods they have

Source: Internet
Author: User

1. Preface

The Python numeric (number) data type is used to store numeric values. The data type is not allowed to change, which means that if you change the value of the numeric data type, the memory space will be redistributed.

2. Create a Digital object

The following instance is created when the variable is assigned a value:

num1 = 10num2 = 20
3. Delete a Numeric object

You can use the DEL statement to delete a numeric object:

del num1        #删除单个数据del num1,num2   #删除多个数据,中间用逗号分隔
4. Numeric type

The PYTHON3 supports three different numeric types:

    • integral Type (INT): usually referred to as an integral or integer, is a positive or negative integer, without a decimal. The Python3 integral type is not limited in size and can be used as a long integer, so Python3 does not have a long type of Python2.
    • float (float): floating-point types consist of integral and fractional parts, and floating-point types can also be represented using scientific notation (2.5e2 = 2.5 x 102 = 250)
    • complex Numbers (complex): complex numbers are made up of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where both the real and imaginary part of a complex number are floating-point types.
5. Numeric Type conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

    • int (x): converts x to an integer, and if x is floating point, only the integer portion is converted.
x = 10y = 3.14z = 3.99print(int(x))      #输出10print(int(y))      #输出3print(int(z))      #输出3
    • float (x): converts x to a floating-point number.
x = 10y = 3.14print(float(x))      #输出10.0print(float(y))      #输出3.14
    • Complex (x): converts the X into a complex number, the real part is x, and the imaginary part is divided into 0.
x = 10y = 3.14print(complex(x))      #输出(10+0j)print(complex(y))      #输出(3.14+0j)
    • complex (x, y): divides x and y into a complex number, the real part is x, and the imaginary part is Y. X and y are numeric expressions.
x = 10y = 3.14print(complex(x,y))      #输出(10+3.14j)

Python Learning "Second article": Numbers in Python and the methods they have

Related Article

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.