Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the built-in data types in python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what built-in data types are in python. It is very detailed and has a certain reference value. Friends who are interested must finish reading it!

Variable

Before you talk about data types, consider the following questions:

How is the data stored?

What is the use of data types?

What is the difference between the various data types?

To answer these questions, you must first understand the concept of variables. So what is a variable?

Variables (Variable) can be thought of as a box for storing data, each variable has a specified name, and the data stored in the variable can be found through the variable name. From the underlying logic, a variable opens up an area in computer memory, and then the stored data is placed in that area.

Variables correspond to constants (Constant), which are all used to store data. The difference is that constants cannot be modified once the data is saved, while the data stored by variables can be modified many times.

In programming language, the process of putting data into variables is called Assignment. In Python, the equal sign = is used as the assignment operator. Variables are a kind of identifiers, so the naming of variables also needs to follow the naming convention of Python identifiers.

Name=value

The name here is the name of the variable, and value is the data you want to go to the variable. A variable can hold only one value, and when a variable is assigned, it will overwrite the original value if it is assigned again.

Take a chestnut:

Axiom 100

Is to assign the number 100 to the variable a.

After explaining the concept of variables, the first problem is solved, that is, the data is stored in memory through variables, of course, the data can also be persisted to the hard disk.

Overview of data types

After clarifying the concept of variables, we will give a general introduction to the various built-in data types in Python. As shown in the mind map below, there are many data types. This article will mainly introduce several data types that belong to numbers. The rest of the data types will be covered in subsequent articles.

Data type action data format example int (integer) for storing integers 100100float (decimal) for decimal writing must include a decimal point 100.1complex (complex) for storing binary ordered real pairs a+bj

Bool (Boolean type) is used to store Boolean values True or False

TruePython is a weakly typed language

Unlike other strongly typed programming languages (such as Java,C), Python is a weakly typed language, which is mainly reflected in

There is no need to force the data type of a variable to be specified when defining a variable. Variables can be assigned directly without declaration, and assigning a value to a variable that does not exist is tantamount to defining a new variable.

The data type of a variable can be changed at any time, for example, the same variable can be assigned to an integer and a string.

For example, define a variable an and assign a value to it. In Java, it is written as follows:

Int aura 100

It is written in Python as follows:

Axiom 100

You can see that the data type int of variable a must be declared in Java. After declaration, integers can only be assigned to variable a, not other types of data such as strings. Python does not have these restrictions.

Weak data types do not mean that there are no data types, which do not need to be declared when writing, but there are data types within the programming language, which can be viewed through the type () method.

> type (100.1) > type (100.1) > type (True) > type (2x10j) detailed introduction of various data types of integers (int)

Integers in Python3 are independent of types, that is to say, there is no long integer type (Long) or short integer type (short). Its value range is infinite, that is, no matter how big or small the number is, Python can easily deal with it. Here are two very large or very small integers.

> 100000-0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

You can see that no number, no matter how large or small, will overflow, which shows that Python has a very strong ability to handle integers.

Different bases of integers

Integers can be represented in a variety of bases in Python.

Decimal form

Our usual integer is the decimal form, which is formed by the arrangement and combination of ten numbers in all.

Note that integers in decimal form cannot start with 0 unless the value itself is 0.

Binary form

It consists of the numbers 0 and 1, and begins with 0b or 0B when writing. For example, 1001 corresponds to a decimal number of 9.

Octal form

An octal integer consists of eight digits in all, starting with 0o or 0O. Notice that the first symbol is the number 0 and the second symbol is the uppercase or lowercase letter O.

Hexadecimal form

It consists of ten digits of 0nine and six letters of Awarf (or axif). It begins with 0x or 0X when written.

# binary a=0b1001print ('axiomorphic a=0b1001print a) # octet b=0o207print (' breadcrumb) # hexadecimal c=0x45print ('cantilevered pencil c)

The result of the operation is:

A = 9b = 135c = 69

Python 3.x allows the use of an underscore _ as a separator for numbers (including integers and decimals), usually with an underscore every three digits, for example: click = 1 "301" 547

Floating point / decimal (float)

In programming languages, decimals are usually stored as floating-point numbers, which are relative to fixed-point numbers; if the decimal point moves during the stored procedure, it is called a floating-point number; if the decimal point does not move, it is called a fixed-point number.

The writing form of decimals

Decimals in Python can be written in two forms:

Decimal form

This is the decimal form that we often see, such as 101.1, 234.5, and 0.23.

Exponential form

Python decimal point index form is written as: aEn or aen

An is the Mantissa part, is a decimal system, n is the exponential part, is a decimal system, E or e is a fixed character, used to divide the Mantissa part and the exponential part, the true expression is a × 10n.

Take a chestnut:

The fifth power of 2.3E5=2.3x10

Still give me a chestnut:

X=10.01print ("x") y=-0.031print ("y") z=2.3E10print ("z") w=-0.00000001print ("w")

The result of the operation is:

X = 10.01y =-0.031z = 23000000000.0w =-1e-08 plural (complex)

The complex number (complex) is composed of the real part (real) and the imaginary part (imag). In Python, the imaginary part of the complex number is suffixed with j or J, and the specific format is:

A+bj

Where a represents the real part and b represents the imaginary part

C1=100+2jprint ("C1 value is:", C1) c2=200+3Jprint ('C2 value is:', c2)

The result of the operation is:

The value of C1 is (100x 2j) C2 and the value is (200x 3j) Boolean type (bool)

Boolean types are used to indicate true (right) or false (wrong), such as the common 3 > 2 comparison formula, which is correct, which is represented by True in Python, and 2 > 3 comparison expression, which is wrong and represented by False.

Print (3 > 2) print (2 > 3) print ('True==1 result is:', True==1) print ('False==0 result is:', False==0)

The result of the operation is:

The result of TrueFalseTrue==1 is: the result of TrueFalse==0 is True

As you can see from the above code, the True value of type bool corresponds to the integer value 1, while the false value corresponds to the integer value 0.

The above is all the content of the article "what are the built-in data types in python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report