zoukankan      html  css  js  c++  java
  • Python

    元组(tuple) 详解 及 代码


    本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967


    元组是存放任意元素集合,不能修改其内容;

    len()是求元组的长度, 可以使用下标标示符("[]")去访问元组的数据;

    元组内还可以包含元组, 可以通过新建一个元组, 扩充已有的元素;


    代码如下:

    # -*- coding: utf-8 -*-
    
    #====================
    #File: abop.py
    #Author: Wendy
    #Date: 2013-12-03
    #====================
    
    #eclipse pydev, python3.3
    
    #元组, 表示不能修改的一组的值
    
    zoo = ('python', 'elephant', 'penguin')
    print('Number of animals in the zoo is', len(zoo))
    
    new_zoo = ('monkey', 'camel', zoo) #新建元组, 不是修改
    print('Number of cages in the new zoo are', len(new_zoo))
    print('All animals in new zoo are', new_zoo)
    print('Animals brought from old zoo are', new_zoo[2]) #把zoo放入new_zoo的第2个位置(从0开始)
    print('Last animal brought from old zoo is', new_zoo[2][2]) #元组包含元组
    print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2])) #减去当前元素[2]和[2]的长度(3-1+3)


    输出:

    Number of animals in the zoo is 3
    Number of cages in the new zoo are 3
    All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
    Animals brought from old zoo are ('python', 'elephant', 'penguin')
    Last animal brought from old zoo is penguin
    Number of animals in the new zoo is 5
    


  • 相关阅读:
    第一章 002-JDK的安装
    第一章 001-初识Java
    计算2^4000内数字0到9的分布
    1027 大数乘法
    1005 大数加法
    哈夫曼编码
    new: Set up a window
    GLFW扩展库
    outdated: 3.Adding Color
    简单的图元
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3473571.html
Copyright © 2011-2022 走看看