Python Self-Study notes (iv) Python primitive data type tuples, collections, dictionaries

Source: Internet
Author: User

One, tuple tuple

Characteristics

1. Ordered set

2. Take data by offset

3, immutable objects, can not modify memory in situ, no sorting, modification and other operations

The immutable benefits of tuples: ensuring data security, such as we pass on to an unfamiliar method to ensure that we do not change our data and cause procedural problems.

Set: A collection is a concept without order, so you cannot use slices and indexes to manipulate

1. Create the Set: Variable set (), Immutable Frozenset ()

2. Add operation (random insert) update (split insert)

3. Delete Remove

4. member relationship in, not in

5. Intersection (&), set (|), Difference set (-)

For example: a = set (' ABCD ')

b = Set (' Bcdef ')

Intersection: Both A & B-->set ([' B ', ' C ', ' d ') appear

The same set: repeated occurrences, taking only one a | b-->set ([' A ', ' B ', ' C ', ' d ', ' e ', ' f '])

Difference set: An element A-b-->set ([' a ']) appearing in set a, but not appearing in B

6. Set Deduplication (list content elements repeat)

For example: a = [1,2,3,1,3]

Set (a)-->set ([+])

Collection to list: Lists (set (a))-->[1,2,3]

Third, the dictionary

Characteristics

1. Disorderly

2, can not use the offset to access, only through the key to access

3, can be nested, convenient for us to organize a variety of data structures

4, belong to the mutable type, support in-situ modification of internal content (like list)

Dictionary = {' key ': value}

Key: Like the key to our reality, and value is a lock, a key to open a lock, the key to make up a dictionary must be immutable data types, such as numbers, strings, tuples and so on.

info = {' A ': 1, ' B ': 2}

info[' A ']-->1

1. Definition method:a = {' name ': ' Niuniu ', age ': 20}

2. Built-in method:B = dict (name = ' Niuniu ', age = 22)

The 3.update parameter is a dictionary type, it overrides the same key value, and the random insertion

info = {' City ': ' Beijing ', ' phone ': ' HTC '}

Info.update ({' City ': ' Tianjin '})

Print info-->{' city ': ' Tianjin ', ' phone ': ' HTC '}

4. Delete del, clear, pop

del info[' phone '] Delete an element

del Info Delete reference

info.clear () Delete all the elements of the dictionary

info.pop (' name ') returns the value corresponding to the key name (list also has the pop method n = [' ['] ', ' ""] n.pop (0) is the subscript, the return is the value, the subscript is not passed Default last one)

Info.pop (' 333 ', ' ha ') can return a pre-set value if no corresponding key is found

5. Member relationship operations in, Has_key () the latter is a dictionary-specific method

Info.has_key (' AA ') returns TRUE or False

keys () Returns a list of all the keys in the dictionary

Values () Returns a list of all values in the dictionary

items () the container that generates a dictionary is a tuple [(' Age ', '), (' Name ', ' Niuniu ')]

get has return value

Info.get (' name ')

b = Info.get (' 2 ') if not found returns a type Nonetype type

c = Info.get (' 2 ', ' BCD ') can be preset return value

Python Self-Study notes (iv) Python primitive data type tuples, collections, dictionaries

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.