Python Basic Tutorial Notes-Chapter 2nd: Lists and tuples

Source: Internet
Author: User

The Python shell repeats the last command: alt+p

2.3 List: Python's drudgery

(1) List function

(2) Assign a value to a list without jumping to a location where an element does not exist.

(3) Delete element, Del name[1]

(4) Shard assignment, name[2:]=list (' ar ')

List methods: Objects. Methods (Parameters)

(1) A.append (6) Add a new object at the end of the list

(2) Number of A.count ("DH") statistics elements appearing in the list

(3) a.extend (b) Multiple values can be appended at the end of the list at once

(4) A.index find the index value of the first occurrence of a value from the list

(5) Insert A.insert (3, ' four ') inserts the string four into the front of the index value 3

(6) A.pop () Delete the element I, the default is to delete the last one, the element that is deleted is returned

(7) A.remove ("Sih") removes the first matching value of a value in the list, no return value

(8) A.reserve () reverse the elements in the array

(9) A.sort () sort the list in the original location

(ten) sorted (a) will return a sequenced queue

There is also an advanced sort of generic template programming similar to C + +

Numbers.sort (CMP) is sorted according to a comparison function CMP, CMP (x, y) x>y 1, x<y -1,x=y 0

X.sort (reverse=true) here indicates whether to reverse sort

X.sort (Key=len) here means that the keyword is len, which is sorted by the length of each element

2.4 Tuples: Immutable sequences, and string literals are immutable.

(a) A general form of a tuple

Truple ([+]) convert a list to a tuple

Tuples can be used as keys in mappings and members of collections, but not in lists.

Python Basic Tutorial Notes-Chapter 2nd: Lists and tuples

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.