Python composite data types--tuples, dictionaries

Source: Internet
Author: User

Lists List

Used to store multiple data

Check and delete and change

list ()/list ()

Set Set

Used to store multiple data

Check and delete and change

set ([])/set ()

1. Combination data Type--tuple (tuple)

The meaning of appearance:

In order to show real life, more fixed data, the concept of tuples appeared

The characteristics of the tuple:

You can also store multiple data, but once you declare and assign a value, you are not allowed to modify the data

Tuple's declaration:

Declaration by a pair of parentheses

t = ("Spring", "Summer", "Autumn", "Winter")

Functions that appear in tuples:

COUNT (): Query the number of occurrences of the specified data

Index (): query where the specified data appears

Note: Declare a tuple that contains only one integer and an empty tuple

L Declare a tuple that contains only one integer, remember x = (12,)

L Declare an empty tuple that can be used directly (), such as: x = ()

L tuple () can be used to declare a tuple and also to convert other combinations of types to infinitesimal groups

Interview question: The data in the tuple is not to be changed. I can change the data, please try to explain

>>> a = ([1,2], [1,2])

>>> A

([1, 2], [1, 2])

>>> A[0][0] = 10

>>> A

([10, 2], [1, 2])

2. Combination data Type--dictionary

Typically, in a list of data, multiple data is present, and multiple data (each of which is not very clear) must have a good convention to use this data without confusion.

Pets = [100, 12]# pet life, pet type

It's a very unfriendly way.

At this time, the dictionary was born

In a dictionary, you can also represent more than one data, but you can add a variable name to each data, and you can use the corresponding variable value by using the variable name

Pets = {"MaxAge": +, "PetType": 12}

Dictionary additions and deletions to check operations

Add Data:

p["Name" = value

modifying data

p["Name" = value

Querying data

x = p["name"]

x = P.get ("name")

x = P.get ("name", info)

Delete data

D.pop (Key)

The characteristics of the dictionary:

The data that is stored is a key value pair {Key:value}

Key cannot be repeated

Value can be repeated

{"Henan": "Region", "Yu": "Region"}

Dictionary in other languages, there is a special name: Map (map)

Dictionary--User management system

The user management system in the list implementation is not the user, but the user name.

Using a dictionary to implement a user management system "is the user information to be managed in the list"

User Information "account number, password, nickname, age, sex, personal introduction"

{"username": "admin", "userpass": "Admin", "nickname": "Rhubarb", "sex": "Male", "Intro": "My Big Face"}

• When new users are added

Account + password + nickname, must be entered

Gender + personal Introduction, you can not enter, data update to "pending"

L User Perfect Personal data

Show user Information

Require the user to enter the name to be modified "sex"

And make changes to the data.

L Delete Users

Require user to enter account + password, correct case, delete user

Query users, traverse the list, query and user input account has been the user's dictionary, display user data

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.