zoukankan      html  css  js  c++  java
  • [Python笔记]元组

    元组(tuple):

    tuplename = (element1, element2, element3...)

     元组是不可变序列,其实就是不可变的列表。

    通常情况下,元组用于保存程序中不可修改的内容。

    一般情况下,一组小括号内的数据称为元组,不过小括号不是必需的,只要将一组数值用逗号分开,Python均可将其视作为元组

    >>> turtle = "123", "445", "544545"
    >>> print(turtle)
    ('123', '445', '544545')

    ## 元组的定义中,"()"不是不是必须的【空元组的定义除外】,而逗号","是必须的,比如如下代码:

     1 >>> tup1 = ("define a turple",)        #表示定义一个元组
     2 >>> print(tup1)
     3 ('define a turple',)
     4 >>> print(type(tup1))              #使用type函数测试变量的类型
     5 <class 'tuple'>
     6 >>> tup1 = ("define a turple")         #这里定义的是一个字符串
     7 >>> print(tup1)
     8 define a turple
     9 >>> print(type(tup1))              #测试变量类型
    10 <class 'str'>
    11 >>> 

     ##空元组的创建,需要使用“()”

    >>> tup2 = ()
    >>> type(tup2)
    <class 'tuple'>
    博客园:http://www.cnblogs.com/linux-farmer/
  • 相关阅读:
    HDU3085 Nightmare Ⅱ (双向BFS)
    LuoguP2523 [HAOI2011]Problem c(概率DP)
    BZOJ4569 [Scoi2016]萌萌哒(并查集,倍增)
    CF360E Levko and Game(贪心)
    总结-小技巧
    总结-二分
    总结-莫队
    $P1821 [USACO07FEB]银牛派对Silver Cow Party$
    $P2126 Mzc家中的男家丁$
    $P5017 摆渡车$
  • 原文地址:https://www.cnblogs.com/linux-farmer/p/15044413.html
Copyright © 2011-2022 走看看