python 基础篇 :基本类型

一、元组

元组是不可变序列,故不能修改元组中的元素
元组无增、删、改元素的方法
只有元组的创建和删除以及其中元素的访问和计数

(一) 元组的3种创建方式

1. ()

小括号可以省略

	a = 1,2,3
	b = (4,5,6)
	c = (7,)		# 只有一个元素时,必须加逗号

2. tuple()

接受参数

  1. 列表
  2. 字符串
  3. 其他序列类型、迭代器等可迭代对象
    如果元组只有一个元素,则必须后面加逗号
	b = tuple()   # 创建一个空元组对象
	b = tuple("abcd")
	b = tuple(range(5))
	b = tuple([1,2,3,4,5])

3. 生成器推导式

与列表推导式相似,不过生成的并非元组,而是生成器对象
生成器对象只能访问一次
可使用__next__()方法来遍历生成器对象

t = (x for x in range(10))

print(type(t))
for i in range(5):
    print(t.__next__())

print(tuple(t))
print(list(t))

运行结果

<class 'generator'>
0
1
2
3
4
(5, 6, 7, 8, 9)
[]

(二) 元组中元素计数

  • 元组长度 len()
  • 最大值 max()
  • 最小值 min()
  • 求和 sum()
  • 排序
    元组无sorted()方法,可使用内置函数sorted(元组对象),结果返回新的列表对象
t = 75,45,1,99,8
l = sorted(t)
print('sorted(t)的类型为 : ',type(l))
print('sorted(t) : ',l)
print('tuple(sorted(t)) :',tuple(l))

运行结果

sorted(t)的类型为 :  <class 'list'>
sorted(t)[1, 8, 45, 75, 99]
tuple(sorted(t)) : (1, 8, 45, 75, 99)
  • zip(列表1[,列表2,…,列表n]) 将多个列表对应位置的元素组合为元组,返回zip对象
a = [1,3,9]
b = [22,33,44]
c = [20,40,60]
d = zip(a, b, c)
print(type(d),tuple(d))

运行结果

<class 'zip'> ((1, 22, 20), (3, 33, 40), (9, 44, 60))

(三) 元组的删除

	del 元组对象名

二、字符串

(一)种创建方式

(二) 增

(三) 删

(四) 改

(五) 查

三、列表

(一) 4种创建方式

1. []

2. list()

3. list(range(start, stop[, step]))

4. 列表推导式

# 方式一
list1 = []
# 方式二
list2 = list()
# 方式三
list3 = list(range(10))
# 方式四
list4 = [ x+1 for x in range(100) if x % 9 == 0]

print('list1 : ',list1,type(list1))
print('list2 : ',list2,type(list2))
print('list3 : ',list3,type(list3))
print('list4 : ',list4,type(list4))

for x in range(100):
    if x % 9 == 0 :
        print(x + 1,end='\t')

print()

for x in range(100):
    y = x + 1
    if y % 9 == 0 :
        print(y,end='\t')

测试结果

list1 :  [] <class 'list'>
list2 :  [] <class 'list'>
list3 :  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
list4 :  [1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100] <class 'list'>
1	10	19	28	37	46	55	64	73	82	91	100	
9	18	27	36	45	54	63	72	81	90	99	

(二) 增 : 5

1. append()

append(对象)

	l = [10 , 20]
	l.append(30)
	print(l)				==> [10, 20, 30]
	l.append([40,50])
	print(l)				==> [10, 20, 30, [40, 50]]

2. extend()

extend(可迭代对象)

	l = [10,20]
	l.extend([30,40])
	print(l)				==> [10, 20, 30, 40]
	l.extend('abc')
	print(l)				==> [10, 20, 30, 40, 'a', 'b', 'c']

3. +

	l = [10,20]
	l2 = [30,40]
	address = id(l2)
	l2 = l + l2
	print(l2)					==> [10, 20, 30, 40]
	print(id(l2) == add1)		==> False  生成新的列表对象

4. *

	l = [10,20]
	print(l*3,id(l*3) == id(l))		==> [10, 20, 10, 20, 10, 20] False 生成新的列表对象

5. insert()

	l = [10, 20]
	l.insert(1,100)
	print(l) 				==> [10, 100, 20]

(三) 删

1. del 列表名[下标]

当前下标之后的所有元素依次前移

2. pop(下标)

删除并返回指定位置元素
无参时 默认删除 末位

3. remove()

删除首次出现的指定元素

(四) 改

(五) 查

1. [下标]

2. index(value,[start,[end]])

  • 获得指定元素在列表中首次出现的索引
  • 列表对象.index(30,5,7) 在索引位置区间[5,7]寻找首次出现30的位置

3.

四、字典

键 : 任意不可变数据[不可重复] ,如 : int 、float 、string 、tuple

(一) 4种创建方式

1. {}

a = {'chinese' : 60 , 'math' : 90 }
b = {}

print(a , type(a))
print(b , type(b))

运行结果

{'chinese': 60, 'math': 90} <class 'dict'>
{} <class 'dict'>

2. dict()

a = dict()
b = dict([ ('chinese' , 60) , ( 'math' , 99 )])
c = dict( chinese = 90 , math = 88)

print(b , type(b))
print(c , type(c))

运行结果

{} <class 'dict'>
{'chinese': 60, 'math': 99} <class 'dict'>
{'chinese': 90, 'math': 88} <class 'dict'>

3. zip()

d = [ 'name' , 'value']
e = [ 'grade' , [77 , 88]]
f = dict(zip(d , e))
print(f , type(f))

运行结果

{'name': 'grade', 'value': [77, 88]} <class 'dict'>

4. fromkeys 创建值为空的字典

参数为 可迭代对象

g = dict.fromkeys([ 'math' , 'Physics' , 88])
print(g, type(g))
{'math': None, 'Physics': None, 88: None} <class 'dict'>

(二) 增

1. 对象[键] = 值

键存在时 , 覆盖原来的值

键不存在时 , 增加键值对

(三) 删

1. del(字典对象[键])

2. 对象.clear() 删除全部

2. 对象.pop(键) 返回该键对应的值

3. 对象.popitem() 随机删除 , 返回该键值对

(四) 改

1.update()

a.update(b) 将b中键值对加入a, 若b中有与a同名的键,则b的值覆盖a中的值

dic1 = { 'a' : 1 , 'b' : 2 , 'c' : 3}
dic2 = { 'a' : 26 , 'z' : 25 , 'y' : 3}
dic1.update(dic2)
print(dic1)

{'a': 26, 'b': 2, 'c': 3, 'z': 25, 'y': 3}

2. 对象[键] = 值

仅当该键存在时,会将原来的值覆盖,否则是增加键值对

(五) 查

1. 对象[键名]

键不存在时 : 出现异常

2. 对象.get(键名)

键不存在时 : 默认返回 None [get()的第二个参数可指定 默认返回对象]

3. 对象.items()

列出所有键值对

4. 对象.keys()

列出所有键

5. 对象.values()

列出所有值

五、集合

集合是无序可变

集合底层实现 是 字典

集合的所有元素是字典中的键对象 ==> unique

(一) 创建

1. {}

s = {55 , 66 , 77}

print( s , type(s))
###########################################
{66, 77, 55} <class 'set'>

(二) 增

对象.add()

(三) 删

1. 对象.remove()

删除指定元素

2. 对象.clear()

(四) 集合相关操作

1. 并

|

union()

2. 交

&

intersection()

3. 差

-

difference()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值