【学习笔记】List和Tuple的使用
来源:互联网 发布:如何修改淘宝好评 编辑:程序博客网 时间:2024/05/18 12:36
List使用
支持中文设置 UTF8+BOM
代码
#!/usr/bin/env python3# -*- coding: utf-8 -*-# List的使用class_mates = ['王林', '北京',"宏卫","华治"]print("一共有 %d 个同学,分别为 %s " % (len(class_mates), class_mates))
运行结果
D:\PythonProject>python helloworld.py一共有 4 个同学,分别为 ['王林', '北京', '宏卫', '华治']
上面有一个len()的方法,求长度的
List的增删改查
#!/usr/bin/env python3# -*- coding: utf-8 -*-# List的使用class_mates = ['王林']print("开始 %d 个同学Python, 分别为 %s " % (len(class_mates), class_mates))# 追加一个同学class_mates.append('北京')print("现在 %d 个同学Python, 新同学 %s 是append进来的 " % (len(class_mates), class_mates[1]))# 插入class_mates.insert(1,"宏卫")print("现在 %d 个同学Python, 新同学 %s 是insert进来的 " % (len(class_mates), class_mates[1]))# 删除# class_mates.pop(index)# 可以是不同类型的变量class_mates.append(123)print("现在 %d 个同学Python, 新同学 %s 是append进来的 " % (len(class_mates), class_mates[3]))# 修改class_mates[3] = "华治"print("现在 %d 个同学Python, 新同学 %s 刚刚修改名字 " % (len(class_mates), class_mates[3]))print("现在 %d 个同学Python, 分别 %s" % (len(class_mates), class_mates))
运行结果
D:\PythonProject>python helloworld.py开始 1 个同学Python, 分别为 ['王林']现在 2 个同学Python, 新同学 北京 是append进来的现在 3 个同学Python, 新同学 宏卫 是insert进来的现在 4 个同学Python, 新同学 123 是append进来的现在 4 个同学Python, 新同学 华治 刚刚修改名字现在 4 个同学Python, 分别 ['王林', '宏卫', '北京', '华治']
让我最为佩服的是list可以放不同类型的变量,简直是跪了跪了
Tuple
这里我理解为java的数组
代码
#!/usr/bin/env python3# -*- coding: utf-8 -*-# tuple的使用# List使用[]围起来,tuple使用()围起来mTuple = ("王林","宏卫","北京","华治",[1,3])print(mTuple)mTuple[4][0] = 8mTuple[4][1] = 8print(mTuple)
运行结果
D:\PythonProject>python helloworld.py('王林', '宏卫', '北京', '华治', [1, 3])('王林', '宏卫', '北京', '华治', [8, 8])
写法区别
- mList = [item1, item2]
- mTuple = (item1, item2)
阅读全文
0 0
- 【学习笔记】List和Tuple的使用
- 【Python学习笔记】使用list和tuple
- python学习笔记使用list和tuple
- Python3学习笔记-使用list和tuple
- Python学习笔记—使用list和tuple
- 【Python】学习笔记——-2.3、使用List和Tuple
- list 和tuple的使用
- 【python学习笔记】6.list和tuple的操作
- Python学习笔记 - list和tuple
- Python学习笔记<list和tuple>
- Python学习笔记:list,tuple和str
- 01-python学习笔记:list和tuple
- 使用list和tuple
- 使用list和tuple
- 使用list和tuple
- 使用list和tuple
- 使用list和tuple
- 使用list和tuple
- Magento中save()操作机制(_beforesave,_aftersave)
- 软件测试面试必备网络知识要点
- 图文详解mongoDB win7x64安装教程
- 架构腐化之谜
- 虚拟机上配置静态ip访问外网(NAT模式)
- 【学习笔记】List和Tuple的使用
- Linux Bash入门:变量和数学运算
- Http 2.0 了解
- Caffe学习小结
- 171208-类与对象程序练习【连续第四十四天】
- 49.Group Anagrams
- 丑奴儿·书博山道中壁
- Ubuntu安装teamviewer报libqt5x11extras5依赖错误
- jzoj【NOIP2017提高A组集训10.28】图