The meaning and usage of lists in python

Example

A list is a collection of elements, stored in a variable. There are no restrictions on the types of elements stored in the list. The following is a simple example of the list.

students =['bernice','arron','cody']for student in students:print("Hello, "+ student.title()+"!")

Naming and Definition List

Since lists are collections of objects, it is good practice to give them a plural name. If every item in the list is a car, name the list as'cars'. This gives you a direct way to represent the list ('cars'), and ('dog') refers to the list item.

In Python, use square brackets to define a list. As follows:

dogs =['border collie','australian cattle dog','labrador retriever']

Access list elements

The elements in the list are identified by position, starting from zero. Visit the first element in the list as follows:

dogs =['border collie','australian cattle dog','labrador retriever']

dog = dogs[0]print(dog.title())

The number in brackets is the index of the list. Because the list index starts from 0, the index of the list element is always smaller than its position. Therefore Python is called a zero-indexed language (such as C, Java).

So to access the second element, we need to use index 1, and so on.

dog = dogs[1]print(dog.title())

Visit the last element in the list

To access the last element in the list, use index -1.

dog = dogs[-1]print(dog.title())

This syntax can also be used to access the second to last and third to last.

dog = dogs[-2]print(dog.title())

But you cannot use a negative number whose absolute value is greater than the length of the list.

dog = dogs[-4]print(dog.title()

So far, this article on the meaning and usage of lists in python has been introduced. For more information about the contents of lists in python, please search ZaLou.Cn

Recommended Posts

The meaning and usage of lists in python
The usage of wheel in python
The usage of tuples in python
Understanding the meaning of rb in python
The usage of Ajax in Python3 crawler
Detailed usage of dictionary in Python
Usage of os package in python
What is the function of adb in python
The difference between the syntax of java and python
The usage of several regular expressions in Linux
Python implements the shuffling of the cards in Doudizhu
The usage of several regular expressions in Linux
Detailed explanation of the usage of Python decimal module
The usage of several regular expressions in Linux
The consequences of uninstalling python in ubuntu, very
MongoDB usage in Python
Solve the problem of python compiling and installing ssl
How to understand the introduction of packages in Python
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Generators and iterators in Python
Consolidate the foundation of Python (3)
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
Summary of logarithm method in Python
Analysis of usage examples of Python yield
Magic methods and uses of Python
Python handles the 4 wheels of Chinese
Common errors and solutions in python
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Mongodb and python interaction of python crawler
​What are the numbers in Python?
Simple usage of python definition class
Talking about the modules in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
End the method of running python
Python learning os module and usage
Description of in parameterization in python mysql
Common exceptions and solutions in the use and development of Ubuntu system
Detailed explanation of the installation and use of SSH in the Ubuntu environment
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
How to use and and or in Python
Implementation of JWT user authentication in python
Logistic regression at the bottom of python
Python solves the Tower of Hanoi game
Python implementation of intersection and IOU tutorial
Solve the conflict of multiple versions of python
Detailed explanation of the use of pip in Python | summary of third-party library installation
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Installation and deployment of Nginx in Ubuntu