码丁实验室,一站式儿童编程学习产品,寻地方代理合作共赢,微信联系:leon121393608。
可爱的小朋友们,今天我们来学习一下Python的基本数据类型吧。图1是我们今天要学习的所有内容啦!

图1基本数据类型
接下来我们一个个来学习吧
数字
1:数字包括整型(标准整型:int,长整型:long),浮点型(单精度浮点型:float,双精度浮点型:double)、复数类型、布尔类型。
其中复数类型是由实数部分和虚数部分组成,它们都是浮点型。虚数表示方法:real+imagj,其中虚数部分必须有后缀j或J。
布尔类型的值有2个,分别是True或False。
注意:
数值中的0, 0.0, 0j(虚数), 空字符串(”)、空元组(())、空列表([])、空字典({})、空集合(set()),对象默认为True,除非它有bool()方法且返回False或len()方法且返回。
2:获取数据类型方法: type或者 isinstance
其中isinstance是返回布尔类型的值,类型一致返回true,类型不一致返回false
案例:
> type(3.13)<class 'float'>> type(True)<class 'bool'>> a=2+7j> a(2+7j)> type(a)<class 'complex'>>>>> a='1'> isinstance(a,str)True> isinstance(3.14,str)False>
字符串
字符串通过单引号或者双引号创建,是序列类型,用数字作为索引,是不可变类型。
案例:
> a='sky'> type(a)<class 'str'>>
列表
列表是序列类型,列表常用的操作有弹出一个元素 pop、排序 sort、反转reverse、插入 insert、删除 remove。
案例:
> member=['fish','cat','dog']> mix=[1,'fish',3.12,[1,2,3]]> empty=[]> member.append('sky')> member['fish', 'cat', 'dog', 'sky']> member.extend(['abc','def'])> member['fish', 'cat', 'dog', 'sky', 'abc', 'def']> member.insert(1,'xiaoli')> member['fish', 'xiaoli', 'cat', 'dog', 'sky', 'abc', 'def']> member.remove('sky')> member['fish', 'xiaoli', 'cat', 'dog', 'abc', 'def']> member.pop(1)'xiaoli'> member['fish', 'cat', 'dog', 'abc', 'def']> member.reverse()> member['def', 'abc', 'dog', 'cat', 'fish']> member.sort()> member['abc', 'cat', 'def', 'dog', 'fish']
元组
元组是不可变的,是序列类型
注意:
创建的元组如果只有一个元素,要在后面加逗号
案例:
> tuple1=(1,2,3,4)> type(tuple1)<class 'tuple'>> tuple=()> temp=(1,)> type(temp)<class 'tuple'>
字典
字典是映射类型,常见操作有字典创建,字典赋值,字典值访问,字典更新,字典元素删除
案例:
> dict1={}> type(dict1)<class 'dict'>> dict2{1: 'one', 2: 'second', 3: 'three'}> b={3:'third'}> dict2.update(b)> dict2{1: 'one', 2: 'second', 3: 'third'}> dict2.pop(1)'one'
集合
集合包括可变集合和不可变集合,具有唯一性(元素不重复),无序的,不能用索引。可变集合可以添加删除元素。集合可以用in和not in检查成员,
案例:
> num2={1,2,3,4}> type(num2)<class 'set'>> 1 in num2True> num2.add(5)> num2{1, 2, 3, 4, 5}> num2.remove(1)> num2{2, 3, 4, 5}
THE END

