09-Data type
1: Commonly used data types
type of data | express | Example |
---|---|---|
integer | int | 2, 3, 45 etc. |
floating point number | float | 1.5, 2.5, 3.5 etc. |
Boolean | bool | true, flase |
string | str | Life is short, I use Python |
2: Integer type
-
English integer, abbreviated as int, can represent positive numbers, negative numbers and zero
-
Integers are expressed in different bases:
base basic number Every time I enter Representation decimal 0,1,2,3,4,5,6,7,8,9 10 520 binary 0,1 2 0b1000001000 Octal 0,1,2,3,4,5,6,7 8 0o1010 hexadecimal 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 16 0x208 -
Demo:
print("十进制: ",520) print("二进制: ",0b1000001000) print("八进制: ",0o1010) print("十六进制: ",0x208)
Output:
520 520 520 520
Three: Floating point type
-
Floating point numbers consist of an integer part and a decimal part
-
The storage of floating point numbers is inaccurate and the calculations are biased because the computer uses binary storage.
Solution:
from decimal import Decimal #导入模块 print(Decimal('1.1')+Decimal('2.2'))
3.3
Four: Boolean type
- Values used to represent true and false
- Boolean can be converted to integer, True→1, False→2
Five: String type
-
String is also known as immutable sequence of characters
-
Can be defined using ‘ ‘, " “,”’ '"
-
Strings defined by single quotes and double quotes must be on one line, and strings defined by triple quotes can be distributed on multiple consecutive lines.
-
Demo:
s1 = 'Hello' s2 = ("hello" "word") s3 = '''hello word''' print(s1) print(s2) print(s3)
Output:
Hello helloword hello word