Python base (13) Public method (a)

1. Operators

Operators Python expression result description Supported data types
+ [1, 2] + [3, 4] [1, 2, 3, 4] merge Strings, lists, tuples
* [“Hi!”] * 4 [ 'Hi', 'Hi', 'Hi', 'Hi!'] repeat Strings, lists, tuples
in 3 in (1, 2, 3) True Element exists Strings, lists, tuples, dictionaries
not in 4 not in (1, 2,3) True Whether the element does not exist Strings, lists, tuples, dictionaries

The combined symbol +

name_str1 = 'faker'
name_str2 = 'tom'
name_list1 = ['ss','kk']
name_list2 = ['ee','dd']
name_tuple1 =(1,)
name_tuple2 = (3,4,5)
print(name_str1 + name_str2)  # fakertom
print(name_list1 + name_list2)  # ['ss', 'kk', 'ee', 'dd']
print(name_tuple1 + name_tuple2)  # (1, 3, 4, 5)

Repeat operator

# *
name_str1 = 'faker'
name_list1 = ['ss','kk']
name_tuple1 =(1,)
print(name_str1*5)  # fakerfakerfakerfakerfaker
print(name_list1*2)  # ['ss', 'kk', 'ss', 'kk']
print(name_tuple1*3)  # (1, 1, 1)

Determine the presence / absence
in / not in

name_str1 = 'faker'
name_list1 = ['ss','kk']
name_tuple1 =(1,)
name_dict ={'name':'tom','id':100,'gender':True}
print('f' in name_str1)  # True
print('ss' in name_list1)  # True
print(1 in name_tuple1)  # True
print('name' in name_dict)  # True 判断key值是否存在字典中
print('tom' in name_dict.values()) # 判断value中的值需要使用values()函数 True

2.Python built-in functions

function description Remark
Only (item) Count the number of elements in the container
del(item) Delete variable del There are two ways
max(item) Returns the maximum container element If the dictionary, comparing only for key
min(item) Returns the minimum container element If the dictionary, comparing only for key
range(start, end,step) It is generated from start to end digital steps of STEP, for use for loop
enumerate (can traverse the object, start = 0) Function for the data object may be a traversal (such as lists, tuples or string) into an index sequence, while the data lists and data subscript

len () Statistical Functions

str1 = 'abcdefg'
print(len(str1))  # 7

del () function deleted

str1 = 'abcdefg'
del(str1)
print(str1) # 报错str1字符串已经被删除
list1 =[1,2,3,4]
del list1[1]
print(list1)  # [1,3,4]

max / min returns the maximum / minimum function

list1 = [1,2,3,4,5,6,7]
a = max(list1)
b = min(list1)
print(a,b)  # 7 1

range () function
generates a digital sequence does not comprise the end bit

for i in range(0,10):
    print(i,end='\t') # 0	1	2	3	4	5	6	7	8	9	

the enumerate () function is
the default is not specified start 0:00

list1 = [22,33,66,44,55,99]
for i in enumerate(list1):
    print(i,end='\t')  # (0, 22) (1, 33) (2, 66) (3, 44) (4, 55) (5, 99)	
print('')
for j in enumerate(list1,start=1):
    print(j,end='\t')  # (1, 22)	(2, 33)	(3, 66)	(4, 44)	(5, 55)	(6, 99)	

Container type converter 3

1.tuple () Conversion tuple

list1 =[1,2,3]
tuple1 = (3,4,5,6)
set1 = {1,2,3,4,5}
print(tuple(list1))  # (1, 2, 3)
print(tuple(set1))  # (1, 2, 3, 4, 5)

2.list () List Conversion

list1 =[1,2,3]
tuple1 = (3,4,5,6)
set1 = {1,2,3,4,5}
print(list(tuple1))  # [3, 4, 5, 6]
print(list(set1))  # [1, 2, 3, 4, 5]

3.Set () converts the set of
the set of features with a de-emphasis, and disordered collection is not supported by the subscript

list1 =[1,2,3]
tuple1 = (3,4,5,6)
set1 = {1,2,3,4,5}
print(set(list1))  # {1, 2, 3}
print(set(tuple1))  # {3, 4, 5, 6}
Published 27 original articles · won praise 5 · Views 434

Guess you like

Origin blog.csdn.net/qq_43685242/article/details/104744932