+关注继续查看

# 一. 运算符

## 1.1 +

# 1. 字符串
str1 = 'aa'
str2 = 'bb'
str3 = str1 + str2
print(str3)  # aabb

# 2. 列表
list1 = [1, 2]
list2 = [10, 20]
list3 = list1 + list2
print(list3)  # [1, 2, 10, 20]

# 3. 元组
t1 = (1, 2)
t2 = (10, 20)
t3 = t1 + t2
print(t3)  # (10, 20, 100, 200)


## 1.2 *

# 1. 字符串
print('-' * 10)  # ----------

# 2. 列表
list1 = ['hello']
print(list1 * 4)  # ['hello', 'hello', 'hello', 'hello']

# 3. 元组
t1 = ('world',)
print(t1 * 4)  # ('world', 'world', 'world', 'world')


## 1.3 in或not in

# 1. 字符串
print('a' in 'abcd')  # True
print('a' not in 'abcd')  # False

# 2. 列表
list1 = ['a', 'b', 'c', 'd']
print('a' in list1)  # True
print('a' not in list1)  # False

# 3. 元组
t1 = ('a', 'b', 'c', 'd')
print('aa' in t1)  # False
print('aa' not in t1)  # True


# 二. 公共方法

## 2.1 len()

# 1. 字符串
str1 = 'abcdefg'
print(len(str1))  # 7

# 2. 列表
list1 = [10, 20, 30, 40]
print(len(list1))  # 4

# 3. 元组
t1 = (10, 20, 30, 40, 50)
print(len(t1))  # 5

# 4. 集合
s1 = {10, 20, 30}
print(len(s1))  # 3

# 5. 字典
dict1 = {'name': 'Rose', 'age': 18}
print(len(dict1))  # 2


## 2.2 del()

# 1. 字符串
str1 = 'abcdefg'
del str1
print(str1)

# 2. 列表
list1 = [10, 20, 30, 40]
del(list1[0])
print(list1)  # [20, 30, 40]


## 2.3 max()

# 1. 字符串
str1 = 'abcdefg'
print(max(str1))  # g
# 2. 列表
list1 = [10, 20, 30, 40]
print(max(list1))  # 40

## 2.4 min()

# 1. 字符串
str1 = 'abcdefg'
print(min(str1))  # a
# 2. 列表
list1 = [10, 20, 30, 40]
print(min(list1))  # 10

## 2.5 range()

# 1 2 3 4 5 6 7 8 9
for i in range(1, 10, 1):
print(i)

# 1 3 5 7 9
for i in range(1, 10, 2):
print(i)

# 0 1 2 3 4 5 6 7 8 9
for i in range(10):
print(i)


## 2.6 enumerate()

• 语法

enumerate(可遍历对象, start=0)

• 快速体验

list1 = ['a', 'b', 'c', 'd', 'e']

for i in enumerate(list1):
print(i)

for index, char in enumerate(list1, start=1):
print(f'下标是{index}, 对应的字符是{char}')


# 三. 容器类型转换

## 3.1 tuple()

list1 = [10, 20, 30, 40, 50, 20]
s1 = {100, 200, 300, 400, 500}

print(tuple(list1))
print(tuple(s1))


## 3.2 list()

t1 = ('a', 'b', 'c', 'd', 'e')
s1 = {100, 200, 300, 400, 500}
print(list(t1))
print(list(s1))

## 3.3 set()

list1 = [10, 20, 30, 40, 50, 20]
t1 = ('a', 'b', 'c', 'd', 'e')
print(set(list1))
print(set(t1))

1. 集合可以快速完成列表去重

1. 集合不支持下标

35 0

8 0
Python 与系统模块|学习笔记

8 0
Python编程：paramiko模块远程登录
Python编程：paramiko模块远程登录
54 0
Python编程：itertools模块
Python编程：itertools模块
44 0
+关注

csdn平台优质创作者，51cto TOP博主，360图书馆科技博主，燕山大学目前大三在读，日拱一卒，功不唐捐，加油！！！
1010

0