python——元组

# 元组用小括号, 元素不能修改(列表中括号, 元素可修改)
info_tuple = ("zhangsan", 18, 1.75, "zhangsan")    # 通常保存不同类型的数据(列表数据类型通常相同)
# 取值和取索引
print(info_tuple[1])
print(info_tuple.index(18))

# 统计计数
print(info_tuple.count("zhangsan"))
print(len(info_tuple))

# 迭代遍历元组
for my_info in info_tuple:
    print(my_info)

# 应用场景
"""
1.函数的参数和返回值
2.格式字符串
3.让列表不可以被修改
"""
info_tuple2 = ("小明", 21, 1.75)
print("%s 年龄是 %d 身高是 %.2f" % info_tuple2)

info_str = "%s 年龄是 %d 身高是 %.2f" % info_tuple2
print(info_str)

# 元组列表之间的转换
num_list = [1, 2, 3, 4]
num_tuple = tuple(num_list)    # 列表转换成元组
num2_list = list(num_tuple)    # 元组转换成列表

运行结果:
18
1
2
4
zhangsan
18
1.75
zhangsan
小明 年龄是 21 身高是 1.75
小明 年龄是 21 身高是 1.75

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值