Zero-based introductory learning Python (15)-sequence

What lists, tuples, and strings have in common

  • Can get every element by index
  • The default index value always starts from 0
  • You can get a set of elements in a range by sharding
  • There are many common operators: repetition operator, splicing operator, membership operator,
    so the list, tuple, and string are collectively called sequence

Sequence common BIF

1. list([iterable]): used to convert an iterable object into a list

Insert picture description here
Insert picture description here

  • The list() method is used to convert an iterable object into a list.
    What is iteration? ? ?
    The so-called iteration is the activity of repeating the feedback process. The purpose is usually to approach and achieve the desired goal or result. Each repetition of the process is called an "iteration", and the results obtained from each iteration will be used As the initial value for the next iteration. . . . . For now, iteration is a for loop, and iterators will be introduced in the future
  • The list() method either takes no parameters or takes an iterator as a parameter, and the sequence is inherently an iterable object
    Insert picture description hereInsert picture description here
Do it yourself to implement the list() method
sequence = input("请输入一个序列:")
sequence = str(sequence)
a = list()
for i in sequence:
    a.append(i)
print(a)

Insert picture description here

2. tuple([iterable]): Convert an iterable object into a tuple

Insert picture description here

3. str(obj): convert obj object to string

4.len(sbu): returns the length of the sub parameter

Insert picture description here

5. max(): returns the maximum value in the sequence or parameter set

Insert picture description here

6.min(): returns the minimum value in the sequence or parameter set

Insert picture description here

attention: Use the max() method and min() method to ensure that the data type of the sequence or parameter is uniform , otherwise an error will occur
Insert picture description here

How to achieve max(tuple1)
tuple1 = input("请输入一个元组:")
tuple1 = tuple(tuple1)
max1 = tuple1[0]
for i in tuple1:
    if i > max1:
        max1 = i
print(max1)

Insert picture description here

7. sum(iterable,[start]): returns the sum of iterable and optional parameter start

  • Optional parameter start, if you set this parameter, it means starting from this value, the default value is 0
    Insert picture description here
    attention: It is not a data type that cannot implement the sum operation
    Insert picture description here

8. sorted(iterable, key = None, reverse = False): returns a sorted list, sorted by default from small to large

  • Use the same method as list().sort()
    Insert picture description here
  • The built-in method sort() of the list is to sort the list in place, and sorted() is to return a sorted new list

9. reversed(sequence): returns the value of the reverse iteration sequence

  • Use the same method as list().reverse()
  • The built-in method reverse() of the list is to reverse the list in place, and reversed() returns a sortedIterator object
    Insert picture description here

10. enumerate(iterable): Generate an iterative object composed of two-tuples (a two-tuple is a tuple with a number of elements of 2), each two-tuple is composed of the index number of the iterable parameter and its corresponding element

Insert picture description here
Insert picture description here

zip(iter1[,iter2[…]]): returns a tuple composed of each iteration parameter

Insert picture description here
Insert picture description here

Task

0. According to the common characteristics of lists, tuples and strings, what do we call them collectively?
sequence

1. What BIF can be used to convert an iterable object into a list, tuple and string?
list()
tuple()
str()

2. Can you retell the concept of "iteration"?
Iteration is the activity of repeating the feedback process, and its purpose is usually to approach and achieve the desired goal or result

3. What do you think will be returned by calling max('I love FishC.com')? why?
'v', because the ASCALL code of v is the largest
4. Oops, the kid is too naughty now, the neighbor's kid is naughty, and drew a pattern on the code written by the little turtle, please restore the code~ ~
Insert picture description here
5. Imagine the implementation process of the min() BIF
6. In the video, we said that the sum() BIF has a flaw, that is, if there is a string type in the parameter, an error will be reported. Please write a new implementation process. Automatically "ignore" the string in the parameter and return the correct calculation result

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/113754237