Python list(List)
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, with the first index being 0, the second index being 1, and so on.
Python has 6 built-in types for sequences, but the most common ones are lists and tuples.
Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.
List is the most commonly used Python data type, which can appear as a comma-separated value within square brackets.
The data items of the list do not need to be of the same type
To create a list, just enclose the different data items separated by commas in square brackets. As shown below:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5];
list3 = ["a" , "b", "c", "d"];
The same as the index of the string, the list index starts from 0. Lists can be intercepted, combined, etc.
Access the values in the list
Use subscript index to access the values in the list. You can also use square brackets to intercept characters, as shown below:
#!/usr/bin/python
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 output result of the above example:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Update list
You can modify or update the data items in the list, you can also use append() method to add list items as follows:
#!/usr/bin/python
list = ['physics', 'chemistry', 1997, 2000];
print " Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];
Note: We will discuss the use of the append() method in the next chapter
The above example output result:
Value available at index 2:
1997
New value available at index 2:
2001
Delete list elements
You can use the del statement to delete elements of the list, as shown in the following example:
#!/usr/bin/python
list1 =[ 'physics', 'chemistry', 1997, 2000];
print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;
The output result of the above example:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Note: We will discuss the use of the remove() method in the next chapter
Python list script operators
The + and * operators for lists are similar to strings. The + sign is used for combined lists, and the * sign is used for repeated lists.
looks like this:
Python expression
Result
Description
len([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repeat
3 in [1, 2, 3] True Whether the element exists in the list
for x in [1, 2, 3]: print x, 1 2 3 Iteration
Python list interception
Python list interception and string The operation type is as follows:
L = ['spam', 'Spam', 'SPAM!']
Operation:
Python expression
result
Description
L[2] 'SPAM!' Read the third element in the list
L[-2] 'Spam' Read the second to last element in the list
L[1:] ['Spam', 'SPAM!'] Intercept the list starting from the second element
Python list functions & methods
Python includes the following functions:
Serial number
Function
1 cmp(list1, list2)
Compare the elements of two lists
2 len(list)
The number of list elements
3 max(list)
Return the maximum value of the list elements
4 min(list)
Return the list elements Minimum value
5 list(seq)
Convert tuple to list
Python contains the following methods:
serial number
method
1 list.append(obj)
Add a new object at the end of the list
2 list.count(obj)
Count the number of times an element appears in the list
3 list.extend(seq)
Append multiple values from another sequence at the end of the list at once (extend the original with a new list list)
4 list.index(obj)
Find the index position of the first matching item of a value from the list
5 list.insert(index, obj)
Insert the object into the list
6 list. pop([index])
Removes an element in the list (the last element by default) and returns the value of the element
7 list.remove(obj)
Removes the first match of a value in the list
8 list.reverse()
reverse the elements in the list
9 list.sort([func])
sort the original list

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.
