python基础之公共操作——运算符+函数


参考于
https://blog.csdn.net/qq_46126118/article/details/107371782

【1】运算符

在这里插入图片描述

加法

str1='aa'
str2='bb'

list1=[1,2]
list2=[10,20]

t1=(1,2)
t2=(10,20)

print(str1+str2)     # aabb
print(list1+list2)   # [1, 2, 10, 20]
print(t1+t2)         # (1, 2, 10, 20)

存在

str1='a'
list1=['hello']
t1=('world',)
dict1={
    
    'name':'lyh','age':19}
# in
print('a' in str1)        # True
print('2a' not in str1)   # True
print('word' in t1)       # False
print('word' not in t1)   # True
print('name' in dict1)    # True
print('name' in dict1.keys())   # True
print('python' in dict1)        # False
print('python' in dict1.values())  # True

复制

str1='a'
list1=['hello']
t1=('world',)

print(str1 *5)  # aaaaa
print(list1*10)
# ['hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello']
print(t1*3)  # ('world', 'world', 'world')

【2】函数

在这里插入图片描述
len()函数
适用于:字典 ,列表,元组,字符串,集合

str1='1234567'
list1=[1,2,3,4,5]
t1=(1,2,3,4,5)
s1={
    
    1,2,3,4,5}
dict1={
    
    'name':'LYH','age':19}

print(len(str1))  # 7
print(len(list1)) # 5
print(len(t1))    # 5
print(len(s1))    # 5
print(len(dict1)) # 2

del()函数
适用于:列表,字典

list1=[1,2,3,4,5]
dict1={
    
    'name':'LYH','age':19}
del(list1[2])
del(dict1['name'])
print(list1)
print(dict1)
'''
[1, 2, 4, 5]
{'age': 19}
'''

max()/min()函数
适用于:字典,字符串,元组,列表,集合

str1='1234567'
list1=[1,2,3,4,5]
t1=(1,2,3,4,5)
s1={
    
    1,2,3,4,5}
dict1={
    
    'name':'LYH','age':19}

print(max(str1))    # 7
print(max(list1))   # 5
print(min(t1))      # 1
print(min(s1))      # 1
print(min(dict1))   #age

range(start,end,step)函数
步长省略默认为1 ,不写开始位,则默认0开始

for i in range(10):
    print(i,end='  ')
#  0  1  2  3  4  5  6  7  8  9  
squares = [value**2 for value in range(1,10) ]
#squares = [1, 4, 9, 16, 25, 36, 49, 64, 81]

enumerate()函数
返回结果是元组,元组第一个数据是原迭代对象的数据对应的,元组第二个数据是原迭代的数据

list1=['a','b','c','d','e']
for i in enumerate(list1):#默认为0开始
    print(i,end='  ')
#  (0, 'a')  (1, 'b')  (2, 'c')  (3, 'd')  (4, 'e')  
for i in enumerate(list1,2):
    print(i,end='  ')
#  (2, 'a')  (3, 'b')  (4, 'c')  (5, 'd')  (6, 'e') 

猜你喜欢

转载自blog.csdn.net/qq_46126258/article/details/107451162