对象的类型决定了该对象可以保存什么类型的值,可以进行什么样的操作,以及遵循什么样的规则。用type()查看对象类型,交互模式下执行的所有东西都是存在内存里面,肯定要占用空间,分类型就是为了管理内存,字长都是固定的长度,语言都是有类型的。
python不需要指定类型直接赋值。
- 用type()查看类型:
>>> a="1"
>>> b=1
>>> print(a)
1
>>> print(b)
1
>>> type(a) #a是字符串类型
<class 'str'>
>>> type(b) #b是整型
<class 'int'>
>>> a=1
>>> type(a)
<class 'int'> #整型
>>> b=1.2222
>>> type(b)
<class 'float'> #浮点型
>>> c=1+1j
>>> type(c)
<class 'complex'> #复数型,复数后面必须是j
>>> d="ewewe"
>>> type(a)
<class 'int'> #字符串类型
>>> e="gjhjhj!".encode("gbk")
>>> type(e)
<class 'bytes'> #bytes类型
>>> 1e10 #e是科学计数法,科学计数法也是浮点数,10表示1后面多少位
10000000000.0
>>> f=1e10
>>> type(f)
<class 'float'>
>>> g=[1,32] #列表list
>>> type(g)
<class 'list'>
>>> i=(1,23) #元组tuple
>>> type(i)
<class 'tuple'>
>>> h={1:2,"a":100} #字典dict
>>> type(h)
<class 'dict'>
>>> j=set([1,2,3]) #集合set
>>> type(j)
<class 'set'>
2.isinstance()判断一个数是否是什么类型:
>>> a=1
>>> type(a)
<class 'int'>
>>> isinstance (a,int)
3.不同的数据类型进行拼接会报错
>>> a=1
>>> b=2
>>> a+b
3
>>> b="s"
>>> a+b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str' #不支持的操作类型
>>>
4.不可以像函数一样被调用
>>> a=1
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable #int对象不可以被调用
5.数据类型相互转化:字符串类型 转为 整型
>>> age="10"
>>> type(age)
<class 'str'>
>>> type(int(age)) #int(age) 把age原来的字符串型转为整型
<class 'int'>
>>>