Getting Started with Python Programming (2): complex data types (lists, dictionaries)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_37925422/article/details/102763664

All of the following examples are based on the latest version of Python, in order to facilitate digestion, each one as much as possible dapper, I hope you can try to master the Python programming "concept", if you can go hands-on try these examples (even if currently not fully get to know ), to deepen understanding.

In Python, common basic data types are:

Digital: integer int, float float
string: str
Boolean values: bool

The common complex data types are:

List: list
the dictionary: dict

In nature, things can be complicated by a combination of simple things together.
In programming, all of the complex data types can be formed by a combination of basic data types.

List list

Also known as the array (the easiest) list is the basic data type "element (element)" in accordance with complex data "index (index)" line up type. (Remember that indexes start at zero rather than starting from 1)

Imagine a series of columns into a list of items you need to buy, put your items from No. 0 to No. N and marked, then you can easily find the mark in accordance with the N items. Here is an index mark, items that element.

index 0 1 2
element 5 1 4

This list to create a new (in the brackets):

arr = [5, 1, 4]

The list has three elements.
Get a list of the first (index 0) element values (using the syntax in parentheses):

# arr[0] 可以获取元素
# 打印出来,值为 5
print(arr[0])

The second modification (index 1) is 2 elements:

arr[1] = 2
# 打印修改后的列表
# 从[5, 1, 4] 改为 [5, 2, 4]
print(arr)

To the end of the list to add an element 3:

arr.append(3)
# 从[5, 2, 4] 改为 [5, 2, 4, 3]
print(arr)

Dictionary dict

Dictionary "key (key)" corresponding to the dictionary "value (value)", (the easiest) dictionary is based on the basic data type as a "key-value pair (key-value)" storage of complex data types. (Note that the dictionary does not store the order, the key can not be repeated)

Similar to the reality of the dictionary, the word corresponding to the meaning of words and the meaning of the composition of key-value pairs, you can find meaning in accordance with the word.

key “A” “B”
value 1 2

New to this dictionary (with braces):

d = {
    "A": 1,
    "B": 2
}

The dictionary contains two pairs.
Gets a dictionary of the keys for the "A" value (also in parentheses syntax):

# d["A"] 可以获取元素
# 打印出来,值为 1
print(d["A"])

Modify key dictionary as "B" is 3:

d["B"] = 3
# 打印修改后的字典
# 从 {"A": 1, "B": 2} 改为 {"A": 1, "B": 3}
print(d)

To add a new dictionary of key-value pairs:

d["C"] = 3
# 从 {"A": 1, "B": 3} 改为 {"A": 1, "B": 3, "C": 3}
print(d)

On the first stop here, next time another theme, speaking expressions.

Guess you like

Origin blog.csdn.net/qq_37925422/article/details/102763664