基础数据类型

  对象的类型决定了该对象可以保存什么类型的值,可以进行什么样的操作,以及遵循什么样的规则。用type()查看对象类型,交互模式下执行的所有东西都是存在内存里面,肯定要占用空间,分类型就是为了管理内存,字长都是固定的长度,语言都是有类型的。
  python不需要指定类型直接赋值。
  1. 用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'>
>>>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值