Python【第二章】:Python的数据类型

基本数据类型

一、整型

如: 18、73、84

 

二、长整型

如:2147483649、9223372036854775807

 

三、浮点型

如:3.14、2.88

 

四、字符串

如:'wupeiqi'、'alex'、'lzl'

1、字符串常用功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#-Author-Lian
#str功能操作
name = "my name is lzl"
 
print(name.capitalize())            #首字母大写
#My name is lzl
 
print(name.count("l"))              #统计字符串出现某个字符的个数
#2
 
print(name.center(30,"-"))          #打印30个字符,不够的-补齐
#--------my name is lzl--------
 
print(name.ljust(30,"-"))           #打印30个字符,不够的-补齐,字符串在左边
#my name is lzl----------------
 
print(name.endswith("lzl"))         #判断字符串是否以lzl结尾
#True
 
print(name[name.find("na"):])       #find寻找na所在的索引下标 字符串也可以切片
#name is lzl
 
 
print("5.3".isdigit())              #判断字符是否为整数
#False
print("a_1A".isidentifier())        #判断是不是一个合法的标识符(变量名)
#True
print("+".join(["1","2","3"]))     #把join后的内容加入到前面字符串中,以+为分割符
#1+2+3
print("\nlzl".strip())              #去换行符
#lzl
print("1+2+3+4".split("+"))        #以+为分隔符生成新的列表,默认不写为空格
#['1', '2', '3', '4']
 
name = "my name is {name} and i an {year} old"
print(name.format(name="lzl",year=20)
#my name is lzl and i an 20 old
print(name.format_map({"name":"lzl","year":20}))            #很少用
#my name is lzl and i an 20 old
 
= str.maketrans("abcdefli","12345678")         #转换  一一对应
print("lianzhilei".translate(p))
#781nzh8758
 

五、列表

如:[11,22,33,44,55]、['wupeiqi', 'alex','lzl']

1、创建列表:

1
2
3
4
5
6
7
#两种创建方式
name_list = ['alex''seven''eric']
print(name_list)
# ['alex', 'seven', 'eric']
name_list = list(['alex''seven''eric'])
print(name_list)
# ['alex', 'seven', 'eric']

2、列表类常用功能

① 切片 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
print(name_list[0:3])       #取下标0至下标3之间的元素,包括0,不包括3
#['Alex', 'Tenglan', 'Eric']
print(name_list[:3])        #:前什么都不写,表示从0开始,效果跟上句一样
#['Alex', 'Tenglan', 'Eric']
print(name_list[3:])        #:后什么不写,表示取值到最后
#['Rain', 'Tom', 'Amy']
print(name_list[:])         #:前后都不写,表示取值所有
#['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy']
print(name_list[-3:-1])     #从-3开始到-1,包括-3,不包括-1
#['Rain', 'Tom']
print(name_list[1:-1])      #从1开始到-1,下标有正有负时,正数在前负数在后
#['Tenglan', 'Eric', 'Rain', 'Tom']
print(name_list[::2])       #2表示,每个1个元素,就取一个
#['Alex', 'Eric', 'Tom']
#注:[-1:0] [0:0] [-1:2] 都是空

 ② 追加

1
2
3
4
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
name_list.append("new")          #append追加,加到最后,只能添加一个
print(name_list)
#['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', 'new']

③ 插入

1
2
3
4
#插入
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
name_list.insert(3,"new")          #insert插入,把"new"加到下标3的位置
print(name_list)

④ 修改 

1
2
3
4
#修改
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
name_list[2= "lzl"                #把下标2的字符串换成lzl
print(name_list)

⑤ 删除 

1
2
3
4
5
6
7
8
9
10
11
#3种删除方式
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
del name_list[3]                      #del删除,指定要删除的下标
print(name_list)
#['Alex', 'Tenglan', 'Eric', 'Tom', 'Amy']
name_list.remove("Tenglan")          #remove删除,指定要删除的字符
print(name_list)
#['Alex', 'Eric', 'Tom', 'Amy']
name_list.pop()                       #pop删除,删除列表最后一个值
print(name_list)
#['Alex', 'Eric', 'Tom']

⑥ 扩展

1
2
3
4
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
age_list = [11,22,33]
name_list.extend(age_list)               #extend扩展,把列表age_list添加到name_list列表
print(name_list)

⑦ 拷贝

1
2
3
4
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
copy_list = name_list.copy()                #copy拷贝,对列表进行复制
print(copy_list)
#注:博客最下有关于深浅copy的详细区分

⑧ 统计 

1
2
3
name_list = ["Alex","Tenglan","Eric","Amy","Tom","Amy"]
print(name_list.count("Amy"))               #count统计,统计列表Amy的个数
#2

⑨ 排序和翻转

1
2
3
4
5
6
7
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy","1","2","3"]
name_list.sort()                              #sort排序,对列表进行排序
print(name_list)
#['1', '2', '3', 'Alex', 'Amy', 'Eric', 'Rain', 'Tenglan', 'Tom']
name_list.reverse()                           #reverse翻转,对列表进行翻转
print(name_list)
#['Tom', 'Tenglan', 'Rain', 'Eric', 'Amy', 'Alex', '3', '2', '1']

 ⑩ 获取下标

1
2
3
name_list = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
print(name_list.index("Tenglan"))              #index索引,获取字符的下标
#1

 

六、元组

如:(11,22,33,44,55)、('wupeiqi', 'alex','lzl')

1、创建元组

1
2
3
4
5
6
7
8
9
10
11
#5种创建方式
age = 11,22,33,44,55            #直接写数字或者字符串,默认创建类型元组 字符串类型用引号'lzl'
#输出: (11, 22, 33, 44, 55)   
age = (11,22,33,44,55)          #常见命名方式,()指定类型元组
#输出: (11, 22, 33, 44, 55)
age = tuple((11,22,33,44,55))   #tuple 以类的方式创建(()) 双括号 里面的()不可去掉
#输出: (11, 22, 33, 44, 55)
age = tuple([11,22,33,44,55])   #同(()) 效果一样 很少用 忘记它
#输出: (11, 22, 33, 44, 55)
age = tuple({11,22,33,44,55})   #({})创建的元组,随机排列  没卵用
#输出: (33, 11, 44, 22, 55)

2、元组类常用功能:

1
2
3
4
5
6
7
8
##count        #统计元组字符出现的次数   
name =  ('wupeiqi''alex','lzl')
print(name.count('alex'))             
# 1
##index             #查看字符串所在的索引位置
name =  ('wupeiqi''alex','lzl')
print(name.index('lzl'))               
# 2

 

七、字典 无序

如:{'name': 'wupeiqi', 'age': 18} 、{'host': '2.2.2.2', 'port': 80}

注:字典一种key:value 的数据类型,也称键值对。字典dict是无序的,key值必须是唯一的,不能有重复。循环时,默认循环的是key

 1、创建字典
1
2
3
4
5
6
7
#两种创建方式:
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
print(info_dic)
#{'stu1102': 'LongZe Luola', 'stu1101': 'TengLan Wu', 'stu1103': 'XiaoZe Maliya'}
info_dic = dict({'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",})
print(info_dic)
#{'stu1102': 'LongZe Luola', 'stu1101': 'TengLan Wu', 'stu1103': 'XiaoZe Maliya'}

2、字典类常用功能:

① 增加

1
2
3
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
info_dic['stu1104'= "JingKong Cang"           #增加
print(info_dic)

 ② 修改

1
2
3
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
info_dic["stu1101"= "Jingkong Cang"         #有相应的key时为修改,没有为增加
print(info_dic)

③ 删除

1
2
3
4
5
6
7
8
9
10
11
12
#3种删除方式
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
info_dic.pop('stu1101')                       #pop删除,指定删除的key
print(info_dic)
#{'stu1103': 'XiaoZe Maliya', 'stu1102': 'LongZe Luola'}
del info_dic['stu1102']                      #del删除,指定删除的key
print(info_dic)
#{'stu1103': 'XiaoZe Maliya'}
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
info_dic.popitem()                             #随机删除,没卵用
print(info_dic)
#{'stu1101': 'TengLan Wu', 'stu1103': 'XiaoZe Maliya'}

④ 查找value值

1
2
3
4
5
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
print(info_dic.get('stu1102'))                  #get查找,通过key查找value值
#LongZe Luola
print(info_dic['stu1102'])                      #通过key直接查找,但是如果输入查找的key不存在的话,就会报错,get则不会
#LongZe Luola

⑤ 字典多级嵌套

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
av_catalog = {
    "欧美":{
        "www.youporn.com": ["很多免费的,世界最大的","质量一般"],
        "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
        "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
        "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
    },
    "日韩":{
        "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
    },
    "大陆":{
        "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
    }
}
 
av_catalog["大陆"]["1024"][1+= ",可以用爬虫爬下来"
print(av_catalog["大陆"]["1024"])
#['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']

⑥ 循环

1
2
3
4
5
6
7
8
9
10
11
info_dic = {'stu1101'"TengLan Wu",'stu1102'"LongZe Luola",'stu1103'"XiaoZe Maliya",}
for stu_nu in info_dic:
    print(stu_nu,info_dic[stu_nu])             #循环默认提取的是key
#stu1103 XiaoZe Maliya
#stu1101 TengLan Wu
#stu1102 LongZe Luola
for k,v in info_dic.items():                  #先把dict生成list,数据量大的时候费时,不建议使用
    print(k,v)
#stu1103 XiaoZe Maliya
#stu1101 TengLan Wu
#stu1102 LongZe Luola

 

 

八、集合 

如:{'lzl', 33, 'alex', 22, 'eric', 'wupeiqi', 11}

注:集合是一个无序的,不重复的数据组合。去重性,把一个列表变成集合,就自动去重了。关系测试,测试两组数据之前的交集、差集、并集

1、创建集合

1
2
3
4
#标准创建方式
info_set = set(["alex","wupeiqi","eric","lzl",11,22,33])
print(info_set,type(info_set))
#{33, 11, 'wupeiqi', 'lzl', 'alex', 'eric', 22} <class 'set'>

2、集合类常用功能

① 添加

1
2
3
4
5
6
7
8
9
10
#添加的两种方式
set_1 = set(["alex","wupeiqi","eric","lzl"])
set_1.add(11)                         #add只能添加一个元素
print(set_1)
#{'alex', 'lzl', 'eric', 11, 'wupeiqi'}
 
set_1 = set(["alex","wupeiqi","eric","lzl"])
set_1.update([11,22,33])
print(set_1)                           #update可以添加多个元素
#{33, 11, 'alex', 'wupeiqi', 'eric', 22, 'lzl'}

② 删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#删除的三种方式
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_1.remove("alex")                    #remove 删除指定元素
print(set_1)
#{'eric', 33, 'lzl', 11, 22, 'wupeiqi'}
 
set_1.pop()                             #pop 随机删除元素
print(set_1)
#{33, 'wupeiqi', 11, 22, 'lzl'}
 
set_1.discard("lzl")                   #discard 删除指定元素,与remove区别在于,如果元素不存在也不会报错
set_1.discard(55)
print(set_1)
#{33, 'wupeiqi', 11, 22}

3、集合关系测试

① 交集

1
2
3
4
5
6
#交集
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_2 = set([11,22,33,44,55,66])
 
print(set_1.intersection(set_2))            #intersection 取两个set的交集 set_1和set_2可以互换位置
#{33, 11, 22}

② 并集

1
2
3
4
5
6
#并集
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_2 = set([11,22,33,44,55,66])
 
print(set_1.union(set_2))                     #union 取两个set集合的并集 set_1和set_2可以互换位置
#{33, 66, 11, 44, 'eric', 55, 'lzl', 22, 'wupeiqi', 'alex'}

③ 差集

1
2
3
4
5
6
#差集
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_2 = set([11,22,33,44,55,66])
 
print(set_1.difference(set_2))                 #difference  取两个set集合的差集 set_1有但是set_2没有的集合
#{'lzl', 'eric', 'wupeiqi', 'alex'}

④ 子集、父集  

1
2
3
4
5
6
7
8
#子集
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_2 = set([11,22,33,44,55,66])
set_3 = set([11,22,33])
print(set_1.issubset(set_2))                      #issubset 子集
#False
print(set_1.issuperset(set_3))                    #issuperset 父集
#True

⑤ 对称差集

1
2
3
4
5
6
#对称差集
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_2 = set([11,22,33,44,55,66])
 
print(set_1.symmetric_difference(set_2))           #symmetric_difference 对称差集=两个集合并集减去合集
#{66, 'lzl', 'eric', 'alex', 55, 'wupeiqi', 44}

⑥ 运算符做关系测试

1
2
3
4
5
6
7
8
9
10
11
#运算符做关系测试
set_1 = set(["alex","wupeiqi","eric","lzl",11,22,33])
set_2 = set([11,22,33,44,55,66])
 
set_union = set_1 | set_2           # 并集
 
set_intersection = set_1 & set_2    # 交集
 
set_difference = set_1 - set_2      # 差集
 
set_symmetric_difference = set_1 ^ set_2  # 对称差集

 

posted @ 2016-08-26 11:43  杨坚  阅读(357)  评论(0编辑  收藏  举报