current position:Home>[Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out

[Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out

2022-01-30 16:05:06 Mainong Feige

Hello! , I'm Manon Feige , Thank you for reading this article , Welcome to three links with one button . My life depend on myself not the fate , Today is still a day of study . This article is about 【Python From entry to mastery 】 The fourth in the series , Its main introduction Python The number in the built-in data type of
Dry cargo is full. , Recommended collection , I need to use it. I need to look at it from time to time . If you have any questions and need , Welcome to leave a message ~ ~ ~.

Preface

This article mainly introduces Python Numbers in built-in data types (Number), Include integers (int), decimal (float), The plural (Complex), Boolean type (bool) These data types . This article is all about Python3.x Data types in .

Variable

Before talking about data types , Please think about the following questions :

  1. How is the data stored ?
  2. What is the use of data types ?
  3. What are the differences between various data types ?

To answer these questions , First of all, we should understand the concept of variables . So what is a variable ? Variable (Variable) It can be regarded as a box for storing data , Each variable has a specified name , The data stored in the variable can be found through the variable name . From the bottom logic , Variable is to open up an area in computer memory , The stored data is then placed in this area . The corresponding variable is a constant (Constant), They are all used to store data , The difference is that constants cannot be modified once the data is saved , The data saved by variables can be modified many times .

In programming language , The process of putting data into variables is called assignment (Assignment),Python Use the equal sign in = As an assignment operator , Variable is a kind of identifier , Therefore, the naming of variables also needs to be observed Python Naming conventions for identifiers .

name=value
 Copy code 

there name It's the variable name , and value That is, to the data in the variable . A variable can hold only one value , When a variable is assigned a value , If assigned again, the original value will be overwritten . Take a chestnut :

a=100
 Copy code 

It's about putting numbers 100 Assign a value to a variable a. After explaining the concept of variables , The first problem is solved , That's it Data is stored in memory through variables , Of course, you can also persist the data to the hard disk .

Overview of data types

After clarifying the concept of variables , Next, let's give a general introduction to Python Various built-in data types in , As shown in the mind map below , There are many types of data , This article will mainly introduce several data types belonging to numbers . The rest of the data types will be introduced in subsequent articles .  Insert picture description here

data type effect data format give an example
int( Integers ) Used to store integers 100 100
float( decimal ) Used to store decimals Writing must include a decimal point 100.1
complex( The plural ) Used to store binary ordered real number pairs a+bj
bool( Boolean type ) Used to store Boolean values True perhaps False True
## Python It's a weak type of language
Unlike other strongly typed programming languages ( such as Java,C Language, etc. ),Python It's a weak type of language , It is mainly reflected in
  1. When defining a variable, you do not need to specify the data type of the variable . Variables can be assigned directly without declaration , Assigning a value to a non-existent variable is equivalent to defining a new variable .
  2. The data type of the variable can be changed at any time , such as , The same variable can be assigned to an integer , It will be assigned as a string .

It's still an illustration : For example, define a variable a, And will 100 Assign to it ,Java The way of writing in is :

int a=100
 Copy code 

Python The way of writing in is :

a=100
 Copy code 

It can be seen that Java Variables must be declared in a Data type of int, After declaration, you can only add to the variable a Assign an integer in , You cannot assign other types of data such as strings . and Python There are no such restrictions . Weak data type does not mean no data type , This is to write without deliberately declaring data types , But there are data types inside the programming language , Can pass type() Method to view its data type .

>>> type(100)
<class 'int'>
>>> type(100.1)
<class 'float'>
>>> type(True)
<class 'bool'>
>>> type(2+10j)
<class 'complex'>
 Copy code 

Detailed description of each data type

Integers (int)

Python3 Integers in are of no type , That is, there is no long integer type (Long) Or short integer type (short) Points , Its value range is infinite , That is, no matter how big or small the number is ,Python Can easily deal with . Here are two maximal or minimal integers .

>>> 100000-0000000000000000000000000000000000000000
1000000000000000000000000000000000000000000000
>>> print(-1000000000000000000000000000000000000000000000)
-1000000000000000000000000000000000000000000000

 Copy code 

It can be seen that no matter how large or small the number is, there will be no overflow , This illustrates the Python The ability to handle integers is very strong .

Different base numbers of integers

Python You can use a variety of hexadecimal to represent integers .

  1. Decimal form

We usually use decimal form for integers , It consists of 09 A total of ten numbers are arranged and combined . Be careful , Integers in decimal form cannot be expressed in 0 As the beginning , Unless the value itself is 0. 2) Binary form from 0 and 1 Two numbers make up , Write with 0b or 0B start . for example ,1001 The corresponding decimal number is 9. 3) Octal form Octal integer consists of 07 There are eight numbers in total , With 0o or 0O start . Be careful , The first symbol is a number 0, The second symbol is a capital or lowercase letter O. 4) Hexadecimal form from 09 Ten numbers and AF( or a~f) Six letters , Write with 0x or 0X start .

#  Binary system 
a=0b1001
print('a=',a)
#  octal 
b=0o207
print('b=',b)
#  Hexadecimal 
c=0x45
print('c=',c)
 Copy code 

The result of the operation is :

a= 9
b= 135
c= 69
 Copy code 

Python 3.x Underscores are allowed _ As a number ( Including integers and decimals ) The delimiter , Usually add an underscore every three numbers , such as :click = 1_301_547

Floating point numbers / decimal (float)

In programming languages , Decimals are usually stored as floating point numbers , Floating point numbers and fixed-point numbers are relative ; Decimal in the stored procedure, if the decimal point moves , It's called a floating point number ; If the decimal point doesn't move , It's called a fixed-point number .

The written form of decimals

Python There are two writing forms of decimal in :

  1. Decimal form

This is the decimal form we often see , such as 101.1;234.5;0.23 2. Exponential form Python The decimal point exponential form is written as :aEn or aen a Is the mantissa , It's a decimal ,n For the exponential part , It's a decimal ,E perhaps e Is a fixed character , Used to split the mantissa part and the exponential part , The real expression is a×10n. Take a chestnut : 2.3E5=2.3x10 Of 5 Power Still a chestnut :

x=10.01
print('x=',x)
y=-0.031
print('y=',y)
z=2.3E10
print('z=',z)
w=-0.00000001
print('w=',w)
 Copy code 

The result of the operation is :

x= 10.01
y= -0.031
z= 23000000000.0
w= -1e-08
 Copy code 

How many decimal places are reserved

In actual development, we often keep the specified mantissa after the decimal point , For example, keep two decimal places after .Python There are three ways to keep the number of digits after the decimal point

  1. Use the built-in round function , The syntax is :round([data],[num]), among data Is the data ,num Is the number of digits reserved after the decimal point
  2. Use float function , The syntax is :float('%.[num]f' % [data]), among data Is the data ,num Is the number of digits reserved after the decimal point
  3. Use Decimal class , The syntax is :Decimal([data]).quantize(Decimal('0.00')), among data Is the data ,'0.00' Indicates that two digits are reserved , If you keep three, it's '0.000'
a = 100.01125
print(a)
print('round After decimal point 3 position ', round(a, 3))
print('float Processing keeps the decimal point after 2 position ', float('%.2f' % a))

from decimal import Decimal

print('Decimal Processing keeps the decimal point after 4 position ', Decimal(a).quantize(Decimal('0.0000')))

 Copy code 

The result of the operation is :

100.01125
round After decimal point 3 position  100.011
float Processing keeps the decimal point after 2 position  100.01
Decimal Processing keeps the decimal point after 4 position  100.0113
 Copy code 

The plural (complex)

The plural (complex) It is the real part (real) Deficiency part of harmony (imag) constitute , stay Python in , The imaginary part of a complex number is represented by j perhaps J As a suffix , The specific format is :

a+bj
 Copy code 

among a Represents the real part ,b The empty part

c1=100+2j
print("c1 The value of is :",c1)
c2=200+3J
print('C2 The value of is :',c2)
 Copy code 

The result of the operation is :

c1 The value of is  (100+2j)
C2 The value of is  (200+3j)
 Copy code 

Boolean type (bool)

Boolean types are used to represent true ( Yes ) Or false ( wrong ), For example, the common 3>2 Comparison formula , This is right ,Python Use in True To represent the ; Another example 2>3 Comparison formula , This is wrong , use False To represent the .

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

The result of the operation is :

True
False
True==1 The result is : True
False==0 The result is : True
 Copy code 

As can be seen from the above code ,bool Type of True Value corresponds to an integer value 1, and False Value corresponds to an integer value 0.

summary

This paper mainly introduces Python Numbers in built-in data types , Content comparison basis , I hope it can help readers .

I'm Manon Feige , Thank you again for reading this article .

The whole network has the same name 【 Manon Feige 】. Short step , A thousand miles , Enjoy sharing I'm Manon Feige , Thank you again for reading this article .

copyright notice
author[Mainong Feige],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301605037505.html

Random recommended