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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值