1. Basic use of variables
1.1 Definition and assignment of variables
Python is a dynamic language, so there is no need to specify the data type of the variable when defining the variable.
Variable name=value
qq_number = "12345"
price = 10
2. Basic data types
The basic data types of python are as follows:
-
Integer type (int): 3
-
Long (long): 32344343242424
-
Boolean type (boolean): True/False
-
Floating point type (float): 1.23
-
Complex type (complex): Mainly used for scientific calculations, such as fluctuation problems, capacitance and inductance problems, and calculus problems
3. Variable composition and its rules
3.1 identifier
Namely variable names, function names, etc.
-
Identifier consists of letters, underscores, and numbers
-
Cannot start with a number
-
Cannot be a keyword
3.2 Keywords
An identifier that has been defined or used within Python.
View keywords:
import keyword
print(keyword).kwlist)
The keywords are as follows:
False | assert | continue | except | if | nonlocal |
---|---|---|---|---|---|
True | await | def | finally | import | not |
None | break | of the | for | in | or |
and | async | elif | from | is | pass |
as | class | else | global | lambda | raise |
return | try | while | with | yield |
3.3 Naming rules
Identifiers are case sensitive and should be highly readable.
(1) Underscore style: first_name
(2) Camelback: FirstName
4. Variable input and output
4.1 Input
Input from the outside: input ("input prompt sentence")
>>>a = input("请输入金额:")
请输入金额:12
>>>a
'12'
Note that the content of input is a string type by default
4.2 Output
Through the print() function:
print(qq_number)
print(price)
4.3 Type conversion
function | Description |
---|---|
int(x) | Convert X to integer |
float(x) | Convert x to floating point |
Such as:
price =input("请输入苹果单价:")
weight=input("请输入苹果质量:")
#将字符串形式转换成小数形式
price=float(price)
weight=float(weight)
money=price*weight
print(money)
Note: The variables before and after conversion are not the same variable!
Improve:
price=float( input(“请输入苹果的单价:”) )
Weight=float(input(“请输入苹果的重量:”) )
Money=price*weight
Print(money)
4.4 Formatted output of variables
(% is a formatting character)
Formatting character | meaning |
---|---|
%s | String |
%f | Floating point number, %0.2f, %06f |
%d | Integer |
format:
-
print("text description %s"% variable name)
-
print("text description %s %s"% (variable name, variable name))
-
%06f description: a total of 6 bits of data, add 0 to the left when less than 6 bits; more than 6 bits will be output in the original position
-
%6d Description: A total of 6 bits of data, if less than 6 bits, add a space on the left; more than 6 bits will be output in the original position
-
%.2f Description: The output format is two digits after the decimal point (rounded)