阿布云

你所需要的,不仅仅是一个好用的代理。

Python的基础函数类型(三)

阿布云 发表于

p1.png

4.range

  • 1)创建

py2.7:立即生成数字

range(1,11) # 生成 1,23,,4,56.10

py3:不会立即生成,只有循环迭代,才一个个生成;

for i in range(1,11):

    print(i)

for j in range(1,11,2):

    print(j)

for k in range(10,0,-1):

    print(k)

例子

# li = ['eric','alex','tony']

# for i in range(0,len(li)):

#     ele = li[i]

#     print(ele)

 

 

li = ['eric','alex','tony']for i in li:

    print(i)

 

# for i in range(0,len(li)):

#     print(i+1,li[i])

输出

eric

alex

tony

5.enumerate

功能:额外生成一列有序的数字

例子

li = ['eric','alex','tony']for i,ele in enumerate(li,1):

    print(i,ele)

#1 eric#2 alex#3 tony

li = ['eric','alex','tony']for i,ele in enumerate(li,1):

    print(i,ele)

 

v = input('请输入商品序号:')

v = int(v)

item = li[v-1]print(item)

 

#1 eric#2 alex#3 tony#请输入商品序号:1#eric

6.tuple元组

不可被修改类型,儿子不可被修改,孙子可以

  • 1)创建

user_tuple = ('hex','eric','seven','hex')

  • 2)count

功能:获取个数

user_tuple = ('hex','eric','seven','hex')

v = user_tuple.count('hex')

print(v)

#2

  • 3)index

功能:获取值得第一个索引位置

user_tuple = ('hex','eric','seven','hex')

v = user_tuple.index('hex')

print(v)

#0

  • 4)注意:元组最后加逗号

例子

li = ('hx',)

print(li)

  • 5)本身不可修改,但是孙子可以

user_tuple = ('alex','eric','seven',['1','2','3'],'a4')

# user_tuple[0] = 123 执行错误

# user_tuple[3] = [11,22,33] 执行错误

user_tuple[3][1] = '0'

print(user_tuple)

7.dict

可变类型

  • 创建

 v = { 'name': 'al','password': '123123'}

  • 1)clear

功能:清空

dic = {'k1':'v1','k2':'v2'}

dic.clear()print(dic)

  • 2)copy

功能:浅拷贝

dic = {'k1':'v1','k2':'v2'}

v = dic.copy()

print(v)

  • 3)get

功能:根据key获取指定value,不存在不报错

dic = {'k1':'v1','k2':'v2'}

v = dic.get('k1111',1111)print(v)

  • 4)pop

功能:删除并获取对应的value值

# dic = {'k1':'v1','k2':'v2'}

# v = dic.pop('k1')

# print(dic)

# print(v)

输出:

{'k2': 'v2'}

v1

  • 5)popitem

功能:随机删除键值对,并获取到删除的键值

dic = {'k1':'v1','k2':'v2'}

v = dic.popitem()print(dic)print(v)

输出:

{'k1': 'v1'}

('k2', 'v2')

dic = {'k1':'v1','k2':'v2'}

k,v = dic.popitem() # ('k2', 'v2')print(dic)print(k,v)

输出:

{'k2': 'v2'}

k1 v1

dic = {'k1':'v1','k2':'v2'}

v = dic.popitem() # ('k2', 'v2')print(dic)print(v[0],v[1])

输出:

{'k1': 'v1'}

k2 v2

  • 6)setdefault

功能:增加,如果不存在即删除

dic = {'k1':'v1','k2':'v2'}

dic.setdefault('k3','v3')print(dic)

dic.setdefault('k1','1111111')print(dic)

输出:

{'k2': 'v2', 'k1': 'v1', 'k3': 'v3'}

{'k2': 'v2', 'k1': 'v1', 'k3': 'v3'}

  • 7)update

功能:批量增加或修改

dic = {'k1':'v1','k2':'v2'}

dic.update({'k3':'v3','k1':'v24'})

print(dic)

输出:

{'k1': 'v24', 'k2': 'v2', 'k3': 'v3'}

  • 8)fromkeys

功能:从序列键和值设置为value来创建一个新的字典。

例子:

dic = dict.fromkeys(['k1','k2','k3'],123)

dic['k1'] = 'asdfjasldkf'print(dic)

输出:

{'k2': 123, 'k1': 'asdfjasldkf', 'k3': 123}

8.set

集合,不可重复列表,可变类型。

  • 1)创建

s1 = {"alex",'eric','tony'}print(type(s1))print(s1)

输出:

<class 'set'>

{'alex', 'eric', 'tony'}

  • 2)difference

功能:输出s1中存在,s2中不存在的值

s1 = {"alex",'eric','tony','ii'}

s2 = {"alex",'eric','tony','hexin'}

v = s1.difference(s2)

print(v)

输出:

{'ii'}

  • 3)difference_update

功能:s1中存在,s2中不存在,然后对s1清空,然后在重新赋值

s1 = {"alex",'eric','tony','ii'}

s2 = {"alex",'eric','tony','hexin'}

s1.difference_update(s2)

print(s1)

输出:

{'ii'}

  • 4)symmetric_difference

功能:s1中存在,s2中不存在的值及s2中存在,s1中不存在的值

s1 = {"alex",'eric','tony','ii'}

s2 = {"alex",'eric','tony','hexin'}

v = s1.symmetric_difference(s2)

print(v)

{'ii', 'hexin'}

  • 5)intersection

功能:交集

s1 = {"alex",'eric','tony','ii'}

s2 = {"alex",'eric','tony','hexin'}

v = s1.intersection(s2)

print(v)

输出:

{'eric', 'alex', 'tony'}

  • 6)union

功能:并集

s1 = {"alex",'eric','tony','ii'}

s2 = {"alex",'eric','tony','hexin'}

v = s1.union(s2)

print(v)

输出:

{'alex', 'hexin', 'eric', 'ii', 'tony'}

  • 7)discard

功能:移除

s1 = {"alex",'eric','tony','ii'}

s2 = {"alex",'eric','tony','hexin'}

s1.discard('alex')

print(s1)

输出:

{'eric', 'tony', 'ii'}

  • 8)update

功能:添加

s1 = {"alex",'eric','tony','李泉','李泉11'}

s1.update({'alex','123123','fff'})

print(s1)

输出:

{'fff', '李泉', '123123', 'tony', 'alex', 'eric', '李泉11'}

9.布尔值

  • 创建

a = True

b = Flase

  • 转换

数字转换只有0是Flase,字符串只有""为Flase

v = bool(12)

print(v)