python learning 3 (definition and use of variables)

Definition and use of variables

Four attributes of variables:

  1. Variable name: name
  2. Variable identification: id
  3. Variable type: type
  4. 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)

Insert picture description here

Rules for the use of variable names:

  1. Alphanumeric underscore composition
  2. case sensitive
  3. Cannot be a reserved word
  4. Can only start with a number

Variable identification: automatically provided by the system

Variable type:

Commonly used number types:

  1. Integer type: int
  2. Floating point type: float
  3. Boolean type: bool
  4. 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)

Insert picture description here

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

Guess you like

Origin blog.csdn.net/qq_40551957/article/details/113737363