python数据类型定义及其特点

无纤尘

Python中的数据类型

1、什么是数据类型

对程序处理的数据进行分类,书籍类型是针对具体的字面量,而不是变量名称。

2、为什么产生数据类型

  • 区分存储的空间,根据不同数据类型分配不同存储空间。
  • 方便程序根据不同数据类型的特性做不同处理。

3、python中的数据类型

##### 首先掌握==判断数据类型==的方法

# 不认为子类是父类类型
type(x) 

# 认为子类是父类类型
isinstance(x,y)

type(x) 不认为子类是父类类型,isinstance(x,y)反之。

3.1 Number
  • int整型
num1 = 20;
num2 = 22079967899098770876;
  • float浮点型
num3 = 3.1415926;
num4 = 2.5e2;
num5 = 25e1;
  • complex复数 complex(a,b)
num6 = 3.0+1.3j;
  • bool布尔型
num7 = True;
num8 = False;

注意:bool类型是int的子类型。

num1 = 1;
num7 = True;

print(isinstance(num7,type(num1)));
print(type(num1) is type(num7));

结果:

True
False
3.2 字符串
str1 = "123";
str2 = "True";
str3 = "hello\n"
str4 = "hello\\n"
str5 = "https://www.lcbbs.org/"

打印结果:

123
True
hello 

hello\n
https://www.lcbbs.org/
3.3 列表
list1 = ['Google', 1998, 2022, ['IE',1,2],(1,2,3)];
list2 = [1, {2,3,3,3,3,4} ,{"name":"yueyue","age":20}];


print ("list1: ", list1)
print ("list2: ", list2)

打印结果:

list1:  ['Google', 1998, 2022, ['IE', 1, 2], (1, 2, 3)]
list2:  [1,{2, 3, 4}, {'name': 'yueyue', 'age': 20}]
3.4元组
tup1 = ('Google', 1998, 2022, ['IE',1,2],(1,2,3));
tup2 = (1, {2,3,3,3,3,4} ,{"name":"yueyue","age":20});
tup3 = (1,);
tup4 = "a", "b", "c", "d"; # 不需括号也可以

print ("tup1: ", tup1)
print ("tup2: ", tup2)
print ("tup3: ", tup3)
print ("tup4: ", tup4)

打印结果

tup1:  ('Google', 1998, 2022, ['IE', 1, 2], (1, 2, 3))
tup2:  (1, {2, 3, 4}, {'name': 'yueyue', 'age': 20})
tup3:  (1,)
tup4:  ('a', 'b', 'c', 'd')

注意:元组里面可以嵌套各种序列。其中列表,字典,还是可变的。

3.5 字典
person1 = {"name":"yueyue","age":20,"sex":"女","hobby":["climb","paint","read"],"weight":{98,98,98}}
cat = {"name":"小花脸","age":3,"sex":"女","hobby":("eat","sleep","play with yueyue")}

print ("person1: ", person1)
print ("cat: ", cat)

打印结果

person1:  {'name': 'yueyue', 'age': 20, 'sex': '女', 'hobby': ['climb', 'paint', 'read'], 'weight': {98}}
cat:  {'name': '小花脸', 'age': 3, 'sex': '女', 'hobby': ('eat', 'sleep', 'play with yueyue')}

注意:字典==不==能使用==可变类型==的对象作为==键==。

3.6 集合
set1 = {1,2,3,4,5,5,5,5,5};
set2 = {"apple","apple","banbana","orange"};
set3 = {"apple","apple","banbana","orange",2,3,4,5,5,5,5,5}

print ("set1: ", set1)
print ("set2: ", set2)
print ("set3: ", set3)

打印结果

set1:  {1, 2, 3, 4, 5}
set2:  {'orange', 'apple', 'banbana'}
set3:  {'orange', 2, 3, 4, 5, 'apple', 'banbana'}

注意:

集合具有去重功能。

集合内的元素没有固定顺序

新建一个空集合需要用到set(),而不是{}。

set1 = set();
set2 = {};

print ("set1: ", set1)
print ("set2: ", set2)
print(type(set1))
print(type(set2))

结果:

set1:  set()
set2:  {}
<class 'set'>
<class 'dict'>

版权协议须知!

本篇文章来源于 岳岳 ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

none

101 0 2022-06-06


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
  • 默认
  • 护眼
  • 夜晚
  • 壁纸
  • 默认