python实例 列表
#! /usr/bin/python
# -*- coding: utf8 -*-
#列表类似Javascript的数组,方便易用
#定义元组
word=['a','b','c','d','e','f','g']
#如何通过索引访问元组里的元素
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: ")
print (b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: ")
print (c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: ")
print (d) # All elements of word.
#元组可以合并
e=word[:2]+word[2:]
print ("e is: ")
print (e) # All elements of word.
f=word[-1]
print ("f is: ")
print (f) # The last elements of word.
g=word[-4:-2]
print ("g is: ")
print (g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: ")
print (h) # The last two elements.
i=word[:-2]
print ("i is: ")
print (i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))
print ("Adds new element")
word.append('h')
print (word)
#删除元素
del word[0]
print (word)
del word[1:3]
print (word)
'''
知识点:
* 列表长度是动态的,可任意添加删除元素.
* 用索引可以很方便访问元素,甚至返回一个子列表
* 更多方法请参考Python的文档
'''
最新文章
- UVALive 4329 Ping pong
- soap发送报文请求和dom4j解析XML并且获得指定名称的节点信息
- C#编写简单的聊天程序
- Python-S13-day2-之购物车
- IE6中常见兼容性问题及浏览器显示难题
- nohup 程序名 &; (使程序推到后台运行,即使终端关闭,该程序依然运行)
- Android 画图类View与SurfaceView之学习
- jQuery siblings()用法与实例。
- 如何安装CocoaPods
- asp.net DropDownList实现ToolTip功能
- Android开发知识体系总结
- JavaScript 第一课
- 醒醒吧!互联网的真正未来不是AI,更不是VR,AR,而是区块链
- Nginx+Tomcat 配置负载均衡集群
- maven环境搭建及创建maven项目
- 关于8.0.15版本的mysql下载与安装
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习6
- Linux_磁盘分布_以及分区
- 后端必备的Linux知识
- win10传奇手册CHM打开无法阅读解决
热门文章
- System.Web.Mvc.FileResultc.sc
- Java笔记 - 输入输出流
- 由VMnet引起的browser-sync故障解决方案
- 2019牛客暑期多校赛(第三场)B-求01串中的最长01数量相等的子串和子序列
- adb命令总结
- Lp- Linux必学的60个命令
- C++ 静态对象
- hdu1693 Eat the Trees [插头DP经典例题]
- IOS开发之基础oc语法
- SignalR 2.0 入门与提高 转载https://www.cnblogs.com/vance/p/SignalR.html