python中的高级变量V

  • 定义一个元组(常规)
info_tuple = ("Tom", 18, 1.85)
print(type(info_tuple)) # 查看info_tuple的类型  <class 'tuple'>
print(info_tuple)   # 输出元祖中的所有元素  ('Tom', 18, 1.85)
print(info_tuple[0])    # 指定索引直输出指定数据  Tom
  • 定义一个空元组
info_tuple_01 = ()
print(type(info_tuple_01))  # 查看变量类型 <class 'tuple'>
  • 定义一个只有一个元素的元组
    info_tuple_02 = (5) # 错误示例
    print(type(info_tuple_02)) # 输出<class ‘int’>
info_tuple_03 = (5,)    # 要加上一个逗号程序才能识别位元祖
print(type(info_tuple_03))  # <class 'tuple'>
  • 元组的常用操作
    • 使用.count()方法统计同一个元素在元组中出现的次数
    info_tuple= ("Tom", 18, 1.78, 120, 120)
    print(info_tuple.count(120))    # 通过.count()方法获取指定元素在元组中出现的次数
    
    • 使用.index()方法查看指定元素的索引值,多个相同元素会输出第一次位置的索引值
    info_tuple= ("Tom", 18, 1.78, 120, 120)
    print(info_tuple.index(120))    # 使用.index()方法查看指定元素的索引值
    
    • 使用len()函数统计元组中包含元素的个数
    info_tuple= ("Tom", 18, 1.78, 120, 120)
    print(len(info_tuple))   # 使用len()函数统计元组中包含元素的个数
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值