Python Basic data type

Source: Internet
Author: User

There are many types of data in Python, primarily simple data types (basic data types) and structural data types (composite data types).

Simple data types are often the data of oil in daily life, and each programming language provides a set of basic data types, such as integers, floating-point numbers, character types, logical types, and so on.

Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned. In Python, a variable is a variable, it has no type, and what we call "type" is the type of object in memory that the variable refers to. The basic data types commonly used in Python are numeric types, including integer (int), floating-point (float), complex number (complex), and Boolean type (Boolean).

1. Numbers (number)

The python numeric data type is used to store numeric values. Numeric data types are not allowed to change, so if you change the value of the numeric data type, the memory space will be redistributed.

(1) integer (int)

Can be a positive integer, a negative integer, or 0, without a decimal points. Python3 is not limited in size, can be used as long, but in fact, because of the limited machine memory, we use an integer is not infinitely large.

The integers in Python can be written in several different ways, and there are several types of binary flags:

0O[0O] Number, which represents an octal integer

0X[0X] Number, which represents a hexadecimal integer

0B[0B] number, representing a binary integer

Decimal integer without a binary flag

(2) floating point (float)

Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are equal. In an operation, the result of an integer and a floating-point number operation is a floating-point number, and the integer and floating-point numbers are stored inside the computer in a different way, and the integer operation is always accurate, while the floating-point arithmetic may have errors. How to write floating-point numbers in addition to the general form there are several ways to express:

19. Decimal part is zero, can not write

.98 integer part zero, can not write

-2E3 Scientific counting

(3) plural

The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers.

2. Boolean type (BOOL)

There is no Boolean in Python2, which represents false with the number 0 and 1 for true. To Python3, BOOL is a subclass of int, and it cannot be subclass, and true and false are defined as keywords, but their values are 1 and 0, and they can be added to the numbers. In Python, the data has its implicit true or false value, none, 0, 0.0, empty string ', empty tuple (), empty list [], empty Dictionary {} can be converted to false, and other objects are true.

>>> BOOL (2)

True

>>> bool ([])

False

>>> bool (')

False

>>> bool (' False ')

True

It is important to note that although there is an implicit bool value, the comparison (' = = ') is not equal

>>> 2 = = True

False

>>> [] = = False

False

>>> 2 = = False

False

>>> 1 = = True

True

>>> ' = = False

False

Python Basic data type

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.