Python- use regular data types (lists, dictionaries, tuples, Boolean)

Copyright: Without permission, please do not reprint without permission! ! ! https://blog.csdn.net/weixin_44755148/article/details/89949138

Regular data types (lists, dictionaries, Ganso, Boolean)

List list [] - wrapped in brackets

name = ['小红','小米','小莉',5,6,{'王磊':125}]
print(type(name))
print(name)

#结果展现,可看到列表可以包含各种类型不一致的数据
<class 'list'>
['小红', '小米', '小莉', 5, 6, {'王磊': 125}]

Extracting a list of elements - shift amount extracted by

name = ['小红','小米','小莉',5,6,{'王磊':125}]
print(len(name))    #查看name变量的元素个数,每个元素依据逗号进行分割
6

#提取一个元素,我们按照偏移量进行位置索引
print(name[0])     #偏移量从0开始计数,此处我们只提取一个元素
小红

#提取多个元素,则进行切片操作,由下可知:左右空,取所有,有数字则左取右不取,且得到的结果为列表
print(name[:])         #提取全部的元素
['小红', '小米', '小莉', 5, 6, {'王磊': 125}]
print(name[2:])       #从偏移量为2位置开始提取全部元素
['小莉', 5, 6, {'王磊': 125}]
print(name[:2])       #提取偏移量为2的前面的数据,不包含为2位置的数据
['小红', '小米']
print(name[1:3])     #提取偏移量为1,2位置的数据
['小米', '小莉']
print(name[2:4])     #提取偏移量为2,3位置的数据
['小莉', 5]

-Append increase and extend the list of elements

#append()给列表增加元素,每次只能增加一个元素
list3 = [1,2]
list3.append(3)
print(list3)
[1, 2, 3]

list3.append([4,5])
print(list3)
[1, 2, 3, [4, 5]]

#因为append只能接受一个参数,所以下面这句代码肯定会报错,它是两个元素
list3.append(4,5)
#extend  extend方法只能接收list
list1 = [1,2,5,6,9]
list2 = [25,6,9,542,32]
list1.append(list2)
print(list1)
[1, 2, 5, 6, 9, [25, 6, 9, 542, 32]]

list3 = list1
list3.extend(list2)
print(list3)
[1, 2, 5, 6, 9, [25, 6, 9, 542, 32], 25, 6, 9, 542, 32]

-Del delete list elements

Here Insert Picture Description

-Sort sorted list of elements ()

list3 = [1, 2, 5, 6, 9, 25, 6, 9, 542, 32]
list3.sort()
print(list3)
[1, 2, 5, 6, 6, 9, 9, 25, 32, 542]

Dictionary dict {} - surrounding braces

Dictionary key unique, not repeated; the value can be repeated
Here Insert Picture Description

Dictionary extracted elements

#通过字典的键进行索引提取
scores = {'小明': 95, '小红': 90, '小刚': 90}
print(scores['小明'])
95

Del delete elements and adding dictionary

#删除字典中的某个数据依旧是通过键进行索引查找,然后del删除
scores = {'小明':95,'小红':90,'小刚':90}
del scores['小刚']
print(scores)
{'小明': 95, '小红': 90}

#增加元素则是直接写赋值语句即可
scores['小刚'] = 92
scores['小美'] = 85
print(scores)
{'小明': 95, '小红': 90, '小刚': 92, '小美': 85}

The same as the difference between lists and dictionaries

相同点
1、在列表和字典中,如果要修改元素,都可用赋值语句来完成
2、支持任意嵌套,列表可嵌套其他列表和字典,字典也可嵌套其他字典和列表
差异
1、列表中的元素是有自己明确的“位置”的,而字典中的数据是随机排列的
2、两者数据读取方法会不同的原因:列表有序,要用偏移量定位;字典无序,便通过唯一的键来取值

#举例说明如下:
students = {
    '第一组':['小明','小红','小刚','小美'],
    '第二组':['小强','小兰','小伟','小芳']
    }
scores = [
    {'小明':95,'小红':90,'小刚':100,'小美':85},
    {'小强':99,'小兰':89,'小伟':93,'小芳':88}
    ]
print(students['第一组'][2])
小刚
print(scores[0]['小刚'])    
100

Tuples tuple () - not totally immutable, is variable in some cases

Sequence of tuples and lists are extracted is offset manner;
tuple also supports nested any
type of tuples not entirely immutable, when there are lists or tuples of variables, it can vary ancestral

a=(1)
print(type(a))
<class 'int'>

b=(1,)
print(type(b))
<class 'tuple'>

Ancestral - in some cases elements are variable

def change_tuple(x,y):
    if x>y:
        a_list=[2,3]
    else:
        a_list=[5,6]
    a_tuple = (('a',1),('b',2),a_list)
    print(a_list)
    print(a_tuple)

change_tuple(1,2)
change_tuple(2,1)
#结果如下
[5, 6]
(('a', 1), ('b', 2), [5, 6])
[2, 3]
(('a', 1), ('b', 2), [2, 3])

Extracting elements - Ganso

#元祖也支持嵌套,元素的提取和列表一样,依据偏移量
tuple1 = ('A','B',[('A','B'),('C','D'),('E','f')])
print(type(tuple1))
<class 'tuple'>

print(tuple1[0])
A

print(tuple1[2][1])
('C', 'D')

print(tuple1[2][1][1])
D

print(tuple1[1:])
('B', [('A', 'B'), ('C', 'D'), ('E', 'f')])

Boolean values ​​-True or False

#print()括号内的计算就是【布尔运算】,结果True or False是【布尔值】
print(3>5)
False
print('猫' == '看见')
False
print('猫' != '看见')
True

Boolean operations - Comparison Operators

Here Insert Picture Description

BOOL Boolean values ​​true and false judgment ()

Here Insert Picture Description

Boolean value - logical operators (and, or, in, not in)

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44755148/article/details/89949138