Python learning notes (2) and python learning notes

Source: Internet
Author: User

Python learning notes (2) and python learning notes
I. Basic list definition

A list is a sequential data type. You can use subscript to access an element or element in the list. It can save any number of Flexible containers of any object type.

alist = [123, 1.23, 'abc', [1, a]]

This is a list. You can see integer, decimal, string, and other data types can be put in. Even a list can also include other lists.
name = ["Avata", "Titannic", "The Dark Knight Rises", "Jurassic Park", "Dances with Wolves"]year = [2009, 1997, 2012, 1997, 1990]directors = ["James Cameron",             "James Cameron",             "Christopher Nolan",             "Steven Spielberg",             "Kevin Costner"]print name[0]print name[1]print name[-1]print name[-3]print name[-1][0]print name[1:3]print name[:2]print name[1:]avata = ["Avata", 2009, "James Cameron"]titannic = ["Titannic", 1997, "James Cameron"]theDarkKnightRises = ["The Dark Knight Rises", 2012, "Christopher Nolan"]jurassicPark  = ["Jurassic Park", 1997, "Steven Spielberg"]dancesWithWolves = ["Dances with Wolves", 1990, "Kevin Costner"]

● Brackets must be added to the beginning and end of the list.
● Separate items in the list with commas
● Assign a variable to the entire list using the "=" Operator
● Any data type can be placed in the list. If it is a string, quotation marks are required (remember? Single quotation marks and double quotation marks are acceptable)

Like an array, the index of the first element in the list starts from 0. That is to say, the sequence number of the first element is 0, the second element is 1, and the third element is 2. But what you may not know is: it can also be counted backwards. The last sequence number is-1, and the last and second sequence numbers are-2.

Use: slice the content. The meaning of the front and back ends is [), that is, [A: B], starting from A (including A) to B (excluding B );[: b] refers to the content before B, excluding B; [A:] refers to the content after A, including.
Ii. increase, decrease, delete, and add a list
Append adds an element at the end, extend adds a list at the end, and insert inserts an element at a certain position.
name = ["Lili",            "A3",            "Tom",            "Meta",            "Jack"]name.append("Joy")print nameothername = ["Frank",              "Jim"]print nameprint othernamename.extend(othername)print namename.insert(1,"Bob")print name

Subtraction and Deletion
Delete the element at a certain position or the entire list by del. remove the element from the specific content.
name = ["Lili",            "A3",            "Tom",            "Meta",            "Jack"]del name[2]print namename.remove("Jack")print namedel nameprint name

Change
name[2] = "Tim"

List operators + and *
+ Is the join operator.
* Repeated Operators
list1 = [1, 2, 3]list2 = ["a", "b", "c"]list3 = list1 + list2print list3list1.extend(list2)print list1list1 = list1 * 2print list1list2 = list2 * 3print list2a = "@"h = "_"h *= 3print a,h,a
3. List iteration list loop and list Parsing
names = ["Lili",            "A3",            "Tom",            "Meta",            "Jack"]for name in names:    print nameprint [name for name in names]print [letter for name in names for letter in name[0]]


Here, the FOR loop does not need to know the specific size of the list, which is applicable to lists of any size ~, The print [name for name in names] clause is list parsing, which is very useful, let's take a look at the meaning of the next print [letter for name in names for letter in name [0 ~
Yes
for name in names:for letter in name[0]:print letter
Doesn't it feel very different? It is frequently used in Python ~~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.