What are Lists in Python

A sequence is a basic data structure in Python. Each element in the sequence is assigned a number, which represents its position or index in the sequence, starting with an index of 0, the second index is 1, and so on.There are 6 built-in types of sequences in Python, but the most common are lists and tuples. Operations that can be performed on sequences include indexing, slicing, concatenation, multiplication, and membership testing.

Additionally, Python has built-in methods to determine the length of a sequence and to find the maximum and minimum elements. Lists are a commonly used Python data type that stores a collection of elements in an ordered manner, enclosed in square brackets, with elements separated by commas. The items in a list do not need to be of the same type.

1. Creating a List

To create a list, simply enclose a comma-separated sequence of different data items in square brackets. For example:

list1 = [‘physics’, ‘chemistry’, 1997, 2000]

list2 = [1, 2, 3, 4, 5]

list3 = [“a”, “b”, “c”, “d”]

2. Accessing Values in a List

Values in a list can be accessed using index notation, and you can also slice characters using square brackets. For example:

list1 = [‘physics’, ‘chemistry’, 1997, 2000]

list2 = [1, 2, 3, 4, 5, 6, 7]

print(“list1[0]: “, list1[0])

print(“list2[1:5]: “, list2[1:5])

The console will output:

list1[0]: physics

list2[1:5]: [2, 3, 4, 5]

3. Updating a List

To modify or update items in a list, you can use the append() method to add items to the list. For example:

list1 = [] # Empty list

list1.append(‘Google’) # Use append() to add elements

list1.append(‘baidu’)

print(list1)

The console will output:

[‘Google’, ‘baidu’]

4. Deleting List Elements

Use the del statement to delete elements from a list. For example:

list1 = [‘Google’, ‘Runoob’, 1997, 2000]

print(“Original list: “, list1)

del list1[2]

print(“Deleted third element: “, list1)

The console will output:

Original list: [‘Google’, ‘Runoob’, 1997, 2000]

Deleted third element: [‘Google’, ‘Runoob’, 2000]

Recommended Books of the Day

This book is divided into 4 parts, targeting beginners in Python, systematically explaining how to develop web scraping programs using Python.

The 1st part is a quick start: It mainly introduces the setup of the Python environment and basic syntax knowledge, introductory knowledge of web scraping, basic usage methods, analysis and capture of Ajax data, scraping dynamically rendered page data, setting up and using website proxies, recognizing and cracking verification codes, as well as capturing data from Apps, and methods for data storage.

The 2nd part is skill advancement: It mainly introduces the basic usage of two commonly used web scraping frameworks, PySpider and Scrapy, deployment methods for web scrapers, as well as the usage of common libraries for data analysis and data cleaning.

The 3rd part is project practice: It details two comprehensive practical projects, explaining the start and practical application of Python data scraping. This part summarizes the content of the entire book, reinforcing the reader’s practical skills.

The 4th part is skill expansion: It introduces practical techniques of commonly used AI technologies from the perspectives of data scraping, data cleaning, and data analysis. By using these techniques, readers can improve the speed of writing web scraping programs and the efficiency of data analysis.

Related Book Recommendations

Previous Content Review

What Data Types and Variables are Included in Python

Page setup in Word is also very important, and it’s not too late to learn now!

What You Should Know About Python Comments

Leave a Comment