“This is the fourth day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

1. Public methods

  • +
    • Addition applies to all underlying data types (int float bool)
    • You have the same data type on both sides of the addition operation
    • Addition is a concatenation in container types, not adding values
The # + method can be used between data types
# int float bool can definitely be used for addition
print(1 + 12.3)  # 13.3

# STR can be added? can
str1 = 'hello'
str2 = ' python'
# String add, a quick way to concatenate strings
print(str1 + str2)
# After adding str1 and STR2, we can infer that the + method has generated new data, and the source data is unchanged
print(str1, str2, sep='\n')

Can # list be added? can
list1 = [1.2.3]
list2 = [4.5.6]
# List addition concatenates lists, similar to extend
print(list1 + list2)  # [1, 2, 3, 4, 5, 6]
# list # list # list # list # list # list
print(list1)  # [1, 2, 3]
print(list2)  # [4, 5, 6]

Can # tuples be added? can
tuple1 = (1.2.3)
tuple2 = (4.5.6)
print(tuple1 + tuple2)  # (1, 2, 3, 4, 5, 6)
# Since the elements inside a tuple cannot be modified, the addition of the indexes certainly does not affect the source data, but generates new data

# set
# set1 = {1, 2, 3}
# set2 = {4, 5, 6}
# # TypeError: unsupported operand type(s) for +: 'set' and 'set'
# # Sets cannot be added to each other
# print(set1 + set2)

# dict
# TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
No addition between dict types
Dict1 = {'name': 'xiao Ming '}
# dict2 = {'age':18}
# print(dict1 + dict2)

# Conclusion: All basic data types can be added. Only lists, tuples and strings can be added between container types


Can different container types be added?
list1 = [1.2.3]
tuple1 = (1.2.3)
set1 = {1.2.3}
dict1 = {'name': 'xiaoming'}

# TypeError: can only concatenate list (not "tuple") to list
# print(list1 + tuple1)
# TypeError: can only concatenate list (not "set") to list
# print(list1 + set1)
# TypeError: can only concatenate tuple (not "set") to tuple
print(tuple1 + set1)

# Conclusion: data type muppets cannot be added
Copy the code
  • *
    • Basic data types (int float bool) can be multiplied
    • Container types can only be multiplied with int data
    • Container type multiplication is to copy the container a specified number of times and concatenate
# * effect is set?
What container types can be used with *

Int float; bool bool; int float; bool
print(12.1 * 2)

# Container type multiplication
# format: container type * int
The result of the multiplication operation is that the container types are copied a specified number of times and then spliced together

# list # list # list # list can
list1 = [1.2.3]
# copy list1 3 times and splice it
print(list1 * 3)  # [1, 2, 3, 1, 2, 3]
# Can I multiply list by float?
# TypeError: can't multiply sequence by non-int of type 'float'
# Multiplication cannot multiply containers with non-int types
# print (list1 * 1.3)
Can I multiply # list by a negative number? You can multiply, but the result is an empty list
print(list1 * -3)  # []
# can be multiplied by 0, resulting in an empty list
print(list1 * 0)  # []

Can # tuple be calculated using *? can
tuple1 = (1.2.3.4)
print(tuple1 * 2)  # (1, 2, 3, 4, 1, 2, 3, 4)
# tuple can only be multiplied by an int


Can I use * for # set? Can not be
set1 = {1.2.3}
# TypeError: unsupported operand type(s) for *: 'set' and 'int'
# Collection data cannot be multiplied
# print(set1 * 3)

Can # dict use * arithmetic? Can not be
dict1 = {'name': 'jack'}
# TypeError: unsupported operand type(s) for *: 'dict' and 'int'
# Dictionaries cannot do multiplication
# print(dict1 * 3)

Can the data on the left and right sides of the # multiplication sign be interchanged? can
print(3 * list1)  # [1, 2, 3, 1, 2, 3]
Copy the code
  • inandnot in
    • Determines whether the element is in the data sequence
    • Strings, lists, tuples, collections, dictionaries can all be used
# in Determines whether an element exists in the container
list1 = [1.2.3]
tuple1 = (1.2.3)
set1 = {1.2.3}

print(3 in list1)  # True
print(3 in tuple1)  # True
print(3 in set1)  # True
If you want to determine whether the element is in a set, you must be able to determine whether the element is in a set. If you want to determine whether the element is in a set, you must be able to determine whether the element is in a set
# print([1, 2] in list1) # False
# print([1, 2] in set1) # TypeError: unhashable type: 'list


Can I use in with type # STR? can
str1 = '123'
# TypeError: 'in <string>' requires string as left operand, not int
The left element must be a string type, otherwise an error will be reported
# print(1 in str1)
print('1' in str1)  # True

Is it possible to use in dict?
dict1 = {'name': 123}
# dict uses in to determine whether the current element is a dict key
print(123 in dict1)  # False
print('name' in dict1)  # True

# If you use this method, you cannot determine the collection of dictionary lists
# TypeError: unhashable type: 'list'
# print ([1, 2] in dict1)

# not in: Not in, not in, not in

Conclusion # :
# 1. Use in and not in to determine the element to the left of the keyword and the data sequence to the right of the keyword
# 2. If you want to determine whether an element is in the container, you must be able to store the element in the container. For example, a collection cannot store a list, a dictionary, a collection, so you cannot determine whether an element of its type is in the collection
# 3. The dictionary determines whether an element is in the keys
Copy the code
  • slice
    • Slices are used to obtain a subset of elements in a data sequence according to rules
    • Tuple list STR can be sliced
    • Dict sets cannot be sliced
Slice: Slice a part of the container data according to certain rules

# STR slice
str1 = 'abcde'
# Format :[start position: end position: step size]
# does not change the original string, but produces a new string
print(str1[2:)# cde

Can # list be sliced?
list1 = [1.2.3.4]
# list slice method is exactly the same as STR
# List Slicing does not change the original data, but generates a new list
print(list1[1:3:1])  # [2, 3]
print(list1)

Can # tuple be sliced?
tuple1 = (1.2.3.4)
# tuple1 slicing method is exactly the same as STR
# Slicing does not change the original data, but generates a new list
print(tuple1[1:3:1])  # (2, 3)
# Can cut over the index to extract the desired element
print(tuple1[1:3] [0]) # 2
print(tuple1)

Can # dict be sliced? Definitely not, because you can't use indexes to get data
Can # set slice? Definitely not, because you can't use indexes to get data

Conclusion # :
# 1. List STR tuple can be sliced. The format is :[start position: end position: step size]
# 2. All slices do not modify the original data, but create a new data sequence
# 3. Collections and dictionaries cannot be sliced because indexes cannot be used to get data elements
Copy the code

2. Public functions

  • Len: Gets the number of elements in the container
  • Del: deletes elements in the container
  • Max: Obtains the maximum value of the data in the container
  • Min: Gets the minimum value of the elements in the container
  • Enumerate: A sequence number can be carried when retrieving elements in a container
  • Range: obtains a sequence of integers according to certain rules
# len = number of elements of the container type
str1 = '123'
list1 = [1.2.3]
tuple1 = (1.2.3)
set1 = {1.2.3}
dict1 = {'name': 123.'age': 18}
Select * from list where len = len; select * from list where len = len
print(len(str1))
print(len(list1))
print(len(tuple1))
print(len(set1))
Use len to get the number of key-value pairs in a dict
print(len(dict1))

# len() can be written as a container.__len__()
print(list1.__len__())

# del
# Remove the element specified in the container
# list
# del list1[0]
# print(list1)

# tuple
# del tuple1[0]
# TypeError: 'tuple' object doesn't support item deletion
Tuple elements cannot be deleted
# print(tuple1)

# set
# for i in set1:
# del i

# dict
# del dict1['name']
# del Deletes key-value pairs in dict
print(dict1)

# str
# TypeError: 'str' object doesn't support item deletion
# STR cannot delete inner elements using del
# Note: Elements inside STR are also unmodifiable, similar to tuples
# del str1[0]
# print(str1)

Conclusion # :
List, dictionary can use del to delete the inner element, but in the list is to delete the element, in the dictionary is to delete the key value pair
# 2. Using del, you can't loop through the set to delete elements, because of the reference mechanism
# 3. Elements inside a STR tuple are immutable, so del cannot be used to remove elements


# max min
# list tuple set STR = Max min
print(max(list1))
print(max(tuple1))
print(max(set1))
print(max(str1))
Dict is the maximum and minimum values of keys using Max and min
print(max(dict1))

Enumerate function: add a sequence number when retrieving data from a container (default sequence number from 0 can be used as an index)

list2 = [1.2.3.4.5.6.7.8]

for i in list2:
    print(i)

Can I get both the element value and the element index? You can use enumerate

# for i in enumerate(list2):
Print (index, value); print (index, value)
# print(i)

# list
for index, value in enumerate(list2):
    print(index, value, sep=':')

# tuple
for index, value in enumerate(tuple1):
    print(index, value, sep=':')

# set
for index, value in enumerate(set1):
    print(index, value, sep=':')

# str
for index, value in enumerate(str1):
    print(index, value, sep=':')

# dict  
for index, value in enumerate(dict1):
    print(index, value, sep=':')
    
# Conclusion: All container and class iteration types can use enumerate and generate sequence numbers. This sequence number is not an index value, but starts at 0 by default when generating sequence numbers. It happens to be used as an index in lists, STR and tuples
Copy the code