变量
- 变量,可以改变的量,一般用于简化代码或者保存动态的数据值。在python中不需要指定变量类型。每个变量在使用前都必须赋值,变量赋值以后变量才会被创建。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 整型变量
counter = 100
# 浮点型变量
miles = 1000.0
# 字符串
name = "runoob"
# 浮点型
height = 1.78
# 布尔类型
sex = True
多个变量赋值
#!/usr/bin/env python
# -*- coding:utf-8 -*-
a = b = c = 1
#!/usr/bin/env python
# -*- coding:utf-8 -*-
a, b, c = "张三", 20, True
变量交换
#!/usr/bin/env python
# -*- coding:utf-8 -*-
a = 10
b = 20
a, b = b, a
数据类型转换
字符串转整数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
student_num = "10086"
new_student_num = int(student_num)
浮点数转整数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
height = 1.78
new_height = int(height)
标准数据类型
- python3中有6个标准类型,Number(数字),String(字符串),List(列表),Tuple(元组),Sets(集合),
Dictionary(字典)
- 其中数字类型包括:int(有符号整型),float(浮点型),long(长整型,也可以代表八进制和十六进制),complex(复数)