# Section 4 Sequence Type Initial

💕💕💕 Blogger nickname: swing sun 💕💕💕
👩‍💻 Blogger research direction: web penetration testing, python Programming

-------- # 1. Introduction to Sequence

Numeric type: numeric value can be represented

int integer = integer

float floating point = decimal

bool Boolean = used for judgment / can also be used as a numeric value

Sequence type: A data type that stores multiple data

str string - > numeric letters, special symbols, Chinese and other data

List list list

Tuple tuple

A sequence is a continuous memory space in which multiple values can be stored, arranged in a certain order, and accessed by the subscript at the location of each value.

# Lists and tuples

A list is a python data type, and a list is an ordered container that can hold various data types.

list creation:

Enclosed directly with [], elements separated by English commas

```name_list = ['Swinging Yang', 'Never eat dinner',  True, 123, 1.1]

print(name_list)
```

range() function: an iterator that returns a sequence of integers based on a given parameter

Iterators need to be converted to lists to view

Specific syntax:

```range(start,stop) # start and stop represent two parameters
# start Start
# stop End
# Note: The header is not tail-wrapped, starting from start to stop [start, stop]
#  start <= x < stop , x ∈ z

# Generate lists from 1 to 1000
n = list(range(1, 1001))  # Because it's not tail-wrapped, 1001 can't be reached, it only gets 1000
print(n)
0
# Generate an even number between 0 and 100

range(start,stop,step=1)
# start Start
# stop End
# step size
# Generate an even number between 0 and 100
li = list(range(0, 104, 2))
print(li)

# Multiple of 7 in output 1 ~ 100
# Step 1 Identify the beginning and end
# Step 2
li2 = list(range(0, 101, 7))
print(li2)

# range can be used to generate any integer with a fixed difference

If you want to generate a descending sequence of integers, you can change the step size to a negative number
# 10~1 List
li = list(range(10, 0,-1))  # 10 heads and 0 tails
print(li)

# Generate List 8 6 4 2 0
s = list(range(8,0,-2))
print(s)
```

Tuples, like lists, can be used to store multiple data. The biggest difference is that lists can be modified, tuples can't, and tuples can't be modified.

Tuple creation

() English brackets, each element separated by commas

```111 = (1,2,3,4,"Swinging Yang")
print(111)
print(type(111))
```

Together with the range() function, the tuple type conversion is used

```t = tuple(range(1,101))
print(t)
```

# 3. Basic operation of sequence types

Sequence type: list, tuple, str

## 1. Subscripts

### 1.1 Downward Subscript

There will be a subscript for each element in the sequence type, which starts with 0 by default from left to right. Elements can be extracted by Subscripts

Grammar Format:

```# Extraction: Sequence [Subscript]

# Notice here is []

Only functions follow()

s = "Python"  # Subscript=Number-1

print(s)
print(s)
print(s)
print(s) # Maximum subscript must not be exceeded when accessing an element, and error will occur if exceeded

Extraction of Nested Lists,For example, extract pendulum
lis = [2, 4, 6, ["pendulum","rotten"], 8]
mj = lis  #  mj = ["pendulum", "rot"]
print(mj)
```

### 1.2 Reverse Subscript

The difference between the reverse subscript and the forward-down table is that the reverse subscript starts from right to left and the first right subscript has a value of -1

```s = "python3"
# Extract t at this time
# Forward 2 Reverse-5
print(s) # print(s[-5])
```

## 2. Slices

In Python, slices are an advanced index method for sequential objects such as lists, strings, tuples. Ordinary indexes only take out elements corresponding to one subscript in the sequence, while slices take out elements corresponding to a range in the sequence, where the range is not a narrowly consecutive fragment.

If you want to intercept a sequence in a sequence, you can use slices

For example:

```L = ["Draft", "Axe", "Spade", "AK47", "98K", "Hoe head"]
# Extract the categories of firearms to a new list
# Slice Grammar Sequence [start:stop:step]

# Similar to range, but also with no end

print(L[3:5])
s = "hello world!"

# hello
# 1. Determine the subscript 0 for h first
# 2.Re-determination o Subscript 4  # Untailed + 1
print(s[0:5])

# hlo
# 1. Determine header subscript 0 first
# 2. Determine the subscript 4+1 of the tail again
# 3. In determining Step 2
print(s[0:5:2])

s = "hello world!"

print(s[:])  # Header parameters, if not written, are cut from the beginning by default
# Tail parameter is cut to the end by default if not written

s = "hello world!"
# Reverse Sequence Type
print(s[::-1]) # If the step size is negative, the head is right and the tail is left
```

## 3. Member Operators

in: belongs to

not in: does not belong to

operatordescribeExample
inReturns True if a value is found in the specified sequence, otherwise False.X is in the y sequence, and returns True if x is found in the y sequence.
not inReturns True if no value is found in the specified sequence, otherwise returns False.X is not in the y sequence, and returns True if x is not in the y sequence.
```s = "Hello My friend"

print("H" in s)

print(" " in s)  # Spaces in a string count as one character

print("h" in s)  # False

print("ll" in s)  # True
print("fr " in s)
print("Hlo" in s) # Not Continuous

```

## 4. Sequence operations

Sequence types can only be added and multiplied

The addition of sequences is the splicing of two sides

Multiplication of sequences is repetition and self-stitching

```s1 = "Swinging Yang"
s2 = "Never eat dinner"

list1 = ["Swinging Yang"]
list2 = ["Never eat dinner"]

print(s1 + s2)  # Direct stitching
print(list1 + list2)  # Direct merge

print(list1 * 5) # ['Rotating Yang','Rotating Yang','Rotating Yang','Rotating Yang','Rotating Yang','Rotating Yang']
```

## 5. Common built-in functions for sequences

What are built-in functions

python officially offers good functionality that can be used directly, such as print,input

functionfunction
len()Calculates the length of a sequence, that is, how many elements are contained in the return sequence.
max()Find the largest element in the sequence.
min()Find the smallest element in the sequence.
sum()Calculates the sum of elements.
sorted()Sort elements.

Learning built-in functions focuses on these two points

1. Functions of functions
2. Parameters of the function

### 5.1 len()

len(seq): calculates the length of the sequence, that is, how many elements are contained in the return sequence

```seq: sequence

s = "hello,world"
print(len(s))

li = [1,2,3,4,"Swinging Yang"]
print(len(li))
```

### 5.2 max() and min()

max(seq) finds the largest element in the sequence.

min(seq) finds the smallest element in the sequence.

```seq:sequence
li = [2, 4, 0, 6, 3, 5]
print(max(li))
print(min(li))
```

### 5.3 sum()

sum(seq): sum the sequence elements

```seq:sequence

li = [2, 4, 0, 6, 3, 5]
print(sum(li))
```

### 5.4sorted()

sorted(seq) sorts elements.

Ascending sorted(seq)

Descending sorted(seq,reverse=True)

```li = [2, 4, 0, 6, 3, 5]

print(sorted(li))
print(sorted(li,reverse=))
```

# Summary

This is my previous note. Blogger Home Page Jump Link If there is something wrong with it, you are welcome to point out the problem. I am also a beginner in python. I hope I can learn and improve with your friends.