Definition and use of variables
Four attributes of variables:
- Variable name: name
- Variable identification: id
- Variable type: type
- variable
The syntax for defining variables:
- Variable name = variable value
Use of variable attributes:
- Variable identification: id (variable)
- Variable type: type (variable)
- Variable value: variable
name='玛丽亚'
print(id(name))
print(type(name))
print(name)
Rules for the use of variable names:
- Alphanumeric underscore composition
- case sensitive
- Cannot be a reserved word
- Can only start with a number
Variable identification: automatically provided by the system
Variable type:
Commonly used number types:
- Integer type: int
- Floating point type: float
- Boolean type: bool
- String type: str
age=100
print('age',type(age),age)
weight=90.1
print('weight',type(weight),weight)
girl=True
print('girl',type(girl),girl)
name='玛丽亚'
print('name',type(name),name)
variable:
1. Integer type:
other decimal conversions can be used: 0b(2) 0o(8) 0x(16)
2. Floating point type: the
addition of float may cause decimal place errors.
Solution:
from decimal import Decimal
print(Decimal(' 1.1')+Decimal('1.2'))
3. Boolean type: bool
can be converted to integer type
True=1/False=0
4. String type: str
''/" ”/''''/"" “ "The
content is on one line "
''''/""" """ The
content is not on the line