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
    


  • 相关阅读:
    转 MySQL权限管理
    mysql 驱动问题
    mysql issue:
    (五)容器网络 -上
    idea 快速生成代码的快捷键
    (四)容器互联
    (三)将容器变成镜像
    (二)docker的部署安装,配置,基础命令
    (一)为什么要学习docker
    Centos7 安装docker ce
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3473571.html
Copyright © 2011-2022 走看看