[Python] Lesson 4 Data container notes

Source: Internet
Author: User

Key points of the fourth lesson of the itpub Python course:

Lesson 4 notes Python Common Data Structures

List:

Define ex: A = [1, 2, 3, 4, 5]
One element can be of multiple types
2 elements can also be of the list type, or multiple Nesting is also allowed.
Multiple Generation Methods A = [X for X in range (10)]
Common Operations:
A. append (x) append operation
A. merge two extend (x) lists
A. Count (x) counts the number of elements
A. insert (index, x) insert the corresponding element at the specified position
A. Pop () removes the last element
A. Remove (x) deletes an element. Each time the first element is deleted
A. Reverse () Reverse the list element order
A. Sort () sorts the list elements.
Slice operation
Tuple:

Define ex: B = (1, 2, 3, 4, 5)
Many operations are similar to list operations, but tuples are immutable objects. List is suitable for arrays that are frequently added, deleted, and modified. tuples are not suitable for set operations, such as passing values.
Slice operation

>>> A = ([2, 3], 33) >>> print a ([2, 3], 33) >>> print a = 4 file "<stdin> ", line 1 print a = 4 ^ syntaxerror: Invalid Syntax # The syntax in the video is wrong. It is not the current syntax that is wrong. The tuples can contain list.

In the "Python core programming" section, the tuples are not so immutable. We can find such a case. Later, the teacher corrected it.

>>> a([2, 3], 33)>>> a[0][1]=44>>> print a([2, 44], 33)

The list in the tuples can also change the value.

Dirt:

Definition: A = {:44; 2: '3 '}
Common Operations:
A. has_key (x) x exist in?
For o in A. iteritems ():
Print o # Return tuples
B = [X for X in A. itervalues ()]
A. Values ()
Sorted (a) # A build-in functon
Set:

Set (x) # repeated operations on a set

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.