Python Learning notes (ii)

Source: Internet
Author: User

First, the basic definition of the list

A list is a serialized data type that can be used to access an element or elements in a list. It can hold a very flexible container of any number, arbitrary object type.

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

This is a list of data types, such as integers, decimals, and strings, that can be put in, and even a list can put other lists in.
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", "james Cameron"]titannic = ["Titannic", 1997, "James Cameron"]thedarkknightrises = ["The D Ark Knight Rises ",", "Christopher Nolan"]jurassicpark  = ["Jurassic Park", 1997, "Steven Spielberg"] Danceswithwolves = ["Dances with Wolves", 1990, "Kevin Costner"]

The start and end of the list are added with brackets
Use commas to separate items in the list from items
Use the "=" operator to assign the entire list to a variable
Any data type can be placed in the list, and if it is a string, it needs to be quoted (remember?). Single quotes, double quotes are all possible)

As well as arrays, the index of the first element of a list starts at 0. This means that the first element is ordinal 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 ordinal number is-1, and the penultimate number is-2.

Use: The content is sliced, the meaning of its front and back is [), that is [A:B], starting from a (including a) to B (not including b); [: b] is the content before the interception of B, excluding B;[a:] is the content from the start of a interception, including a.
Second, the list of increase or decrease to delete
Append adds an element at the end, extend adds a list at the end, where insert inserts the element
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

Reduction and deletion
Del's way to remove elements from a location or the entire list, remove elements that remove 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
* is a repeating operator
List1 = [1, 2, 3]list2 = ["A", "B", "C"]list3 = List1 + list2print list3list1.extend (list2) Print List1list1 = List1 * 2PRI NT List1list2 = list2 * 3print LIST2A = "@" H = "_" H *= 3print a,h,a
Third, the list of the iteration list of the loop and list resolution
names = ["Lili",            "A3",            "Tom",            "Meta", "            Jack"]for name in Names:    print Nameprint [name of name in Nam Es]print [Letter for name in names for letter in Name[0]]


Here for loop can not need to know the specific size of the list, for any size of the list ~, and print [name for the name in names] This sentence is the list of parsing, it has a very powerful use to see the next print [letter for the name in Names in name[0]] meaning ~
It is
For name in names:for letter in Name[0]:print
Does it feel very different, it uses very high frequency in Python ~ ~ ~

Python Learning notes (ii)

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.