What can be placed in Python lists, tuples, sets, and dictionaries?

What can be placed in Python lists, tuples, sets, and dictionaries?

As the final exam approaches , I will sort out the materials in the class for everyone to review.

>>>s='这是字符串'

>>> list1=[1,'这是列表,可以放数字和字符串']

>>> tuple1=(1,'这是元组','我在这里你动不了我')

>>> set1={
    
     '我是无序的集合’,我不能和左/右边说的一-样'}

>>> dict1={
    
    '一是一':1,'二':2}

----------------------------------分割线-----------------------------------

>>> list2=[('我可以,套娃元组'),['还可以','套娃列表'],{
    
    '还有集合'},{
    
    '字典也行' :True}]

>>> tuple2=(['我也可以'],('老套娃了',),{
    
    '这是集合'},{
    
    '这是字典' :True})
>
>"元组中若只有一个元素,记得加逗号(,)'

>>> set2={
    
     ("集合只能套元组" ,)}

>>> dict2={
    
    '键的名称可以是字符串':['还有两种'],2:{
    
    '这是第二种——数字'},('嗯这是第三种——元组'):3} 

>>> dict2={
    
     '右边倒是什么都能放呢':{
    
    '上一行悄悄在右边放了个列表和集合':'这次我来试试字典' }}
>
>“键”的名称不可改变

As mentioned earlier, only tuples can be nested in sets. If lists, sets and dictionaries are nested, errors will occur.
Insert image description here
Insert image description here
Later, I was too lazy to modify it, so I directly moved the original manuscript of the academic committee (danger)

1. String

As a member of the basic data type, string is mentioned in Chapter 4 because it has classic sequence operations.

As shown in the figure, we define the variable s as a string of characters, and use square brackets + index numbers to access the characters at the corresponding positions. When the index number does not have a corresponding character in the current variable, 'IndexError' will be returned, which is an index error.
Slicing is also a very important basic operation. As shown in the figure, the return result is still a string.
Insert image description here

As shown in the figure, slicing does not need to worry about the subscript going out of bounds.
In other words, if you want to use a larger subscript and expect it to return an index error, it is not feasible. Because he makes no mistakes.
Insert image description here

Here are some string manipulation methods from book P23
Insert image description here

2. Tuple

    作为不可变数据类型,元组应该是序列类型中最简单的一个了。

Insert image description here

    元组解包蛮好玩的

    元组里套元组好像叫二维元组,可以用索引访问

Insert image description here

    创建元组可以不打小括号,也称元组打包

    值得注意的是,元组+元组,返回的还是元组。与字符串类似,元组索引不能越界,切片可以,切片完返回的也是元组。(索引返回的就是元素,元素是什么类型就返回什么类型)

Insert image description here

That's it for today

Guess you like

Origin blog.csdn.net/A_No2Tang/article/details/111300675