Common data types
Integer type -> int
Can represent a positive number 、 negative 、0 The representation of integers in different base numbers Decimal system -> Default base , No special indication is required Binary system -> With 0b start octal -> With 0o start Hexadecimal -> With 0x start
Base number Basic numbers Several in a row Representation form Decimal system 0,1,2,3,4,5,6,7,8,91011 Binary system 0,120b11111111 octal 0,1,2,3,4,5,6,780o1544 Hexadecimal 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F160x11
# -----------python Data types commonly used in -----------
# Integers can be expressed as binary 、 octal 、 Decimal system 、 Hexadecimal
print(1)
print(0b11111111) # 2 Base number ,0b start
print(0o1544) # 8 Base number ,0o start
print(0xFFFF) # 16 Base number ,0x start
Floating point numbers -> float
Floating point numbers are composed of integer parts and decimal parts Floating point storage imprecision When calculating with floating-point numbers , The number of decimal places may be uncertain , Because the computer uses binary to store , Floating point numbers are stored imprecisely , There will be some errors
# Floating point numbers
print(1.1+2.2) # 3.300000000000003
print(1.1+2.1) # 3.2
resolvent
The import module decimal
from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))
Boolean type -> bool
A value used to indicate true or false True Express as true ,False Said the false Boolean values can be converted to integers True by 1,False by 0
# Boolean type
print(True+1) # 2
character string -> str
Strings are also called immutable character sequences You can use single quotes 、 Double quotes 、 Three quotation marks to indicate The string defined by single quotation marks and double quotation marks must be on one line A string defined by three quotation marks can be distributed on multiple consecutive lines
# character string
str1 = ' character string '
str2 = " String string string "
str3 = ''' character string
character string '''
print(str1, str2, str3)