定义一个变量并赋值后,数据类型会自动确定。无需说明类型,系统会自动辨别
变量类型:
1. 数字:
int(有符号整型)
取值范围理论上是无限的,即[-∞,∞]。但实际上受限于运行python程序的计算机内存大小
num=0x77f
print('变量num的内容为:',num,' 类型为:',type(num))
输出:
变量num的内容为: 1919 类型为: <class 'int'>
long(长整型)
float(浮点型)
python语言要求所有浮点数必须带有小数部分,小数部分可以是0,这种设计可以区分浮点数和整数类型
数值范围和小数精度受不同计算机系统的限制
对于高精度科学计算外的绝大部分运算来说,浮点数类型足够“可靠”,一般认为浮点数类型没有范围限制,运算结果准确
f=1.23
print('变量f的内容为:',f,' 类型为:',type(f))
u的挂号费
输出:
变量f的内容为: 1.23 类型为: <class 'float'>
高精度运算方法之一:使用整数表达浮点数(整数可以输出完全准确的运算结果)
f1=3.141592653
f2=3141592653
f0=1.23456789
print("浮点数运算:",f1*f0)
print("使用整数表示浮点数运算:",f2*f0)
输出:
浮点数运算: 3.878509412853712
使用整数表示浮点数运算: 3878509412.8537116
complex(复数)
2. 布尔:true flase
b=True
print('变量b的内容为:',b,' 类型为:',type(b))
输出:
变量f的内容为: 1.23 类型为: <class 'float'>
3. string(字符串)
str='You are pretty!'
print('变量str的内容为:',str,' 类型为:',type(str))
输出:
变量str的内容为: You are pretty! 类型为: <class 'str'>
4. list(列表) [ ]
list=['dh',123,'123','ueytv',True]
print('变量list的内容为:',list,' 类型为:',type(list))
输出:
变量list的内容为: ['dh', 123, '123', 'ueytv', True] 类型为: <class 'list'>
5. tuple(元组) ( )
tuple=('sf',123,True)
print('变量tuple的内容为:',tuple,' 类型为:',type(tuple))
输出:
变量tuple的内容为: ('sf', 123, True) 类型为: <class 'tuple'>
6. dictionary(字典) { }
d={1:'sg',
'dg':'ddfg',
False:12}
print('变量d的内容为:',d,' 类型为:',type(d))
输出:
变量d的内容为: {1: 'sg', 'dg': 'ddfg', False: 12} 类型为: <class 'dict'>