Section 4 Sequence Type Initial

πŸ’•πŸ’•πŸ’• Blogger nickname: swing sun πŸ’•πŸ’•πŸ’•
πŸ₯°Click to jump to the blog Homepage
πŸ‘©‍πŸ’» Blogger research direction: web penetration testing, python Programming
πŸ“ƒ Blogger's message: I hope this article can help you, there are some deficiencies, I hope your friends give guidance


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]


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
# Generate an even number between 0 and 100

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

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

# 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

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

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")

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

t = tuple(range(1,101))

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[6]) # 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[3]  #  mj = ["pendulum", "rot"]

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[2]) # 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

L = ["Draft", "Axe", "Spade", "AK47", "98K", "Hoe head"]
s = "hello world!"

# hello
# 1. Determine the subscript 0 for h first
# 2.Re-determination o Subscript 4  # Untailed + 1

# hlo
# 1. Determine header subscript 0 first
# 2. Determine the subscript 4+1 of the tail again
# 3. In determining Step 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

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

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"

li = [1,2,3,4,"Swinging Yang"]

5.2 max() and min()

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

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

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

5.3 sum()

sum(seq): sum the sequence elements

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


sorted(seq) sorts elements.

Ascending sorted(seq)

Descending sorted(seq,reverse=True)

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



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.

Click to jump to the blogger python column:
If you have friends who like web security, check it in the web penetration column. Click to jump to Blogger web penetration column

Tags: Python programming language

Posted by chetanmadaan on Wed, 17 Aug 2022 00:40:28 +0530