Pythin----------------元组

    科技2022-08-17  99

    1、元组

     

    可变序列与不可变序列

    '''可变序列(可以对序列执行增、删、改操作,对象地址不发生变化) 列表,字典''' lst=[10,20,45] print(id(lst)) lst.append(300) print(id(lst)) '''不可变序列(没有增删改的操作) 字符串、元组''' s='hello' print(id(s)) s=s+'world' print(id(s)) print(s) 1948973069568 1948973069568 1948973841136 1948973841328 helloworld

    2、元组的创建方式

    '''第一种创建方式,使用()''' t=('Python','World',98) print(t) print(type(t)) '''第二种创建方式,使用内置函数tuple()''' t1=tuple(('Python','world',98)) print(t1) t2='Python','world',98 #省略了小括号 print(t2) print(type(t2)) t3=('Python',) #当元组中只有一个元素时,需要加上逗号 print(t3) print(type(t3))

     

    ('Python', 'World', 98) <class 'tuple'> ('Python', 'world', 98) ('Python', 'world', 98) <class 'tuple'>

     

       空元组的创建

    lst=[] lst1=list() d={} d2=dict() t4=() t5=tuple() print('空列表',lst,lst1) print('空字典',d,d2) print('空元组', 空列表 [] [] 空字典 {} {} 空元组 () ()

     

    3、元组的遍历

    t1=('python','world',98) for item in t1: print(item) python world 98

     

    Processed: 0.046, SQL: 9