Before introducing Python variables and data types, we also need to understand the reserved words and identifiers in Python.
Reserved words: some words are given special meanings in Python, and these words cannot be used when naming objects.
# View reserved words in Python import keyword print(keyword.kwlist) # Reserved word list '''['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while','with','yield']'''
Identifier: the name given to variables, functions, classes, modules, and other objects.
Naming rules: letters, numbers and underscores are strictly case sensitive. They cannot start with numbers or be named with reserved words.
1. Definition and use of variables
A variable is a box with labels, which can put the required data into it. It consists of three parts:
Identification: indicates the memory address stored by the object, which can be obtained by using the built-in function id();
Type: represents the data type of the object, which can be obtained by using the built-in function type();
Value: represents the specific data stored by the object. You can use the print() function to print.
name = "michael" print(name, id(name), type(name)) '''name Is a variable name, which can be customized; "= "Is an assignment operator, which can assign a value to a variable; "michael"This string is the value''' # Output results michael 2623526618800 <class 'str'>
Variables, called variables, can be changed. After multiple assignments, the variable name will point to the new space.
name = "michael" print(name, id(name), type(name)) # Reassign name name = "jackson" print(name, id(name), type(name)) # Output results '''As you can see, name The memory address of the variable is changed, which proves that it points to the new space, And disconnect from the original address, and the original memory address will also become memory garbage''' michael 2787167717040 <class 'str'> jackson 2787163459952 <class 'str'>
2. Data type of Python
Integer type: int (98)
Floating point type: float (3.14)
String type: str ("michael jackson")
Boolean type: bool (True, False)
a = 98 b = 3.14 c = "michael jackson" d = True # Print data type print(type(a), type(b), type(c), type(d)) # Output results <class 'int'> <class 'float'> <class 'str'> <class 'bool'>
2.1 integer type
English is integer, abbreviated as int, which can represent positive numbers, negative numbers and zero.
Different decimal representations of integers:
Base system | Basic number | Representation method | example |
---|---|---|---|
Decimal (default) | 0,1,2,3,4,5,6,7,8,9 | Default representation | 118 |
Binary | 0,1 | Start with 0b | 0b111010 |
octal number system | 0,1,2,3,4,5,6,7 | Start with 0o | 0o116 |
hexadecimal | 0,1,2,3,4,5,6,7,8,9 A,B,C,D,E,F | Start with 0x | 0x76 |
a = 118 b = 0b111010 c = 0o116 d = 0x76 print(a, b, c, d) # Output results 118 58 78 118
2.2 floating point number type
It consists of integer part and decimal part.
Storage imprecision: when using floating-point number type for calculation, the number of decimal places may be inaccurate.
e = 3.1415 print(e, type(e)) n1 = 1.1 n2 = 2.2 print(n1+n2) '''Floating point number calculation has uncertainty, so we need to introduce Decimal library''' from decimal import Decimal print(Decimal("1.1")+Decimal("2.2")) # Output results 3.1415 <class 'float'> 3.3000000000000003 3.3
2.3 string type
Also known as immutable character sequence, it can be defined with single quotation marks, double quotation marks (must be on one line) or three quotation marks (multiple lines).
str1 = "michael" str2 = 'jackson' str3 = '''michael jackson''' print(str1, type(str1)) print(str2, type(str2)) print(str3, type(str3)) # Output results michael <class 'str'> jackson <class 'str'> michael jackson <class 'str'>
2.4 boolean type
Used to represent true or False values. True is true and False is False.
Boolean values can also be converted to integers, true -- > 1, false -- > 0.
f1 = True f2 = False print(f1, type(f1)) print(f2, type(f2)) print(1+f1) print(1+f2) # Output results True <class 'bool'> False <class 'bool'> 2 1
3. Data type conversion
Splice data of different data types together.
Function name | effect | matters needing attention | example |
int() | Convert other data types to integers | 1. Text and decimals cannot be converted to integers 2. Convert floating-point number to integer and erase it | int("123") int(9.8) |
float() | Convert other data types to floating point numbers | 1. Text cannot be converted to floating point number 2. Convert integer to floating point number, and add. 0 at the end | float("9.9") float(9) |
str() | Convert other data types to strings | You can also use quotation marks to convert | str(123) "123" |
a1 = "michael" a2 = 18 a3 = 98.5 print("My name is"+a1+",this year"+str(a2)+"Years old, my grades are"+str(a3)) print(type(a1), type(a2), type(a3)) print(type(str(a2)), type(int(a3))) # Output results My name is michael,I am 18 years old and my grade is 98.5 <class 'str'> <class 'int'> <class 'float'> <class 'str'> <class 'int'>