search
HomeBackend DevelopmentPython TutorialEverything you need to know about Python lists

1. Preface

In Python program development, lists are often used. Suppose there are 50 students in a class and you need to count the total score of each student. If you do not use a list, you need to define 50 variables to store the total score of each student. This is quite troublesome. The best way is Use lists. Next, the editor will take you to learn the knowledge of lists!

2. First acquaintance list

1. For readers who have studied C language or Java language, they all know that these two languages ​​support arrays, and Python does There is no concept of arrays, but there is the concept of lists. The list will store all elements in a pair of square brackets ([]), and adjacent elements are separated by commas, as shown below:

listName=[元素1,元素2,元素3,...元素n]

in the above The variable of the list is listName, and element 1 ~ element n represent the elements in the list.

In C language, arrays store the same type of data. Compared with lists in Python and C language arrays, the more powerful thing is that lists can store the same type of data, and they can also store different types. The data. As shown below:

listName=[1,'a']

2. There are two ways to create a list, as shown below:

1) Use square brackets ([]) to create a list, the syntax is as follows Shown:

listName=[元素1,元素2,元素3,...元素n]

Use square brackets ([]) to create a Python list. "=" means assigning a value to a variable name. Among them, listName is the variable name, and the elements 1 to n in the square brackets represent the elements in the list.

Next, let’s learn how to use square brackets ([]) to create a list through an example. The code is as follows:

a=[1,2,3,4,5]
b=["Python","Java","C语言"]

In the above code, the variable name is a The list of , stores values;

The list of variable name b, stores strings.

2) Use the list() method to create a list. The list() method converts tuples or strings into lists. The syntax is as follows:

listName=list(a)

listName is Variable name, list(a) where the parameter of a represents the string or tuple to be converted into a list.

Next, let’s use an example to understand the use of the list() method. The specific code is as follows:

a = ('Java', 10, 'Python', 'PHP',20)
list1 = list(a)
print("list1列表中元素有: ", list1)


b = "This is Python"
list2 = list(b)
print("list2列表中元素有: ", list2)

The rendering of the program running is as follows Display:

Everything you need to know about Python lists

3. How to access elements in the list

1. There are two ways to access elements in the list, As shown below:

1) Use the subscript value (index value) to access an element in the list. The syntax is as follows:

listName=['A','B','C','D']#定义一个列表
listName[i]#语法

Declare a listName variable A list of names, accessing an element in the list is based on the "variable name" and "subscript value". For example, when accessing the C element in the list, the subscript value starts from 0, so the subscript value of the element C is 2. To access the C elements in the list, use listName[2]

2) Use slicing to access the elements of the list. The syntax is as follows:

listName=['A','B','C','D']#定义一个列表
listName[start,end,step]#切片的语法

In the syntax of slicing, start Indicates the starting position, end indicates the ending position, and step indicates the step size.

Next, let’s learn about using slices to access elements of a list through an example. The specific code is as follows:

listName=['A','B','C','D','E','F','G']
print(listName[1:3])
print(listName[3:])
print(listName[1:6:2])
print(listName[-5:-2])

In the above code, listName[1:3] It means starting from the subscript value 1 to 3, because the left is closed and the right is open, the subscript value 3 cannot be obtained;

listName[3:] means starting from the subscript value 3 to the end;

listName[1:6:2] means starting from the subscript value 1 to 6, with a step size of 2;

listName[-5:-2] indicates that the subscript value starts from -5 to -2. Negative subscript values ​​need to be taken in reverse. -5 is for the C element.

The rendering of program operation is as follows:

Everything you need to know about Python lists


##4. Summary

1. This article mainly introduces what a list is and how to access elements in the list.

2. This article introduces that a list stores all elements in a pair of brackets ([]), and adjacent elements are separated by commas. The article also introduces two ways to create a list, namely creating a list with square brackets ([]) and creating a list with the list() method, and uses examples to help readers have a better understanding.

3. This article introduces two ways to access elements in the list, namely accessing elements in the list by subscript value and using slices to access elements in the list. The article also uses some examples to help readers understand these usages.

The above is the detailed content of Everything you need to know about Python lists. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete
Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor