3.9.1 - Lists in Python
2015-11-18 03:13
561 查看
# Python List Examples a = [] #Empty List b = [0, 1, 2, 3, 4] #List with 5 items c = [3.14, "abc", [1, 2, 3, 4]] #List with a nested list d = list('Python') # List of iterable items e = list(range(0, 10)) # List of integers from 0-9 len(b) len(c[2]) b + c c + b b * 4 3.14 in c b[2] = 9 # mutable temp_string = "Python" temp_string[2] = "a" # error inmutable c[2][0] c[2][-1] b[1:] b[:2] b[1:3]
运行结果如下:
a = [] #Empty List
b = [0, 1, 2, 3, 4] #List with 5 items
c = [3.14, "abc", [1, 2, 3, 4]] #List with a nested list
d = list('Python') # List of iterable items
d
// Result: ['P', 'y', 't', 'h', 'o', 'n'] //
e = list(range(0, 10)) # List of integers from 0-9
e
// Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] //
len(b)
// Result: 5 //
len(c[2])
// Result: 4 //
b + c
// Result: [0, 1, 2, 3, 4, 3.14, 'abc', [1, 2, 3, 4]] //
c + b
// Result: [3.14, 'abc', [1, 2, 3, 4], 0, 1, 2, 3, 4] //
b * 4
// Result: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] //
3.14 in c
// Result: True //
b[2] = 9 # mutable
b
// Result: [0, 1, 9, 3, 4] //
c[2][0]
// Result: 1 //
c[2][-1]
// Result: 4 //
b[1:]
// Result: [1, 9, 3, 4] //
b[:2]
// Result: [0, 1] //
b[1:3]
// Result: [1, 9] //
相关文章推荐
- python 实现从mysql 导出为csv
- Lesson 3.1 - Python Core Data Types
- python中的除法
- Python3.4+Django1.7+SQLite3实现增删改查
- Python操作文件目录
- python中的三个读read(),readline()和readlines()
- lesson 2.4 - Converting MEL Commands to Python
- wxpython学习3
- Python、Unicode和中文
- <LeetCode><Easy>299 Bulls and Cows
- 新手学编程?选python吧!
- 文章标题
- python编码问题(1)
- Python——变量命名规则以及打印操作
- 爬虫 第三篇 (语言选择python还是java)
- 开启Python的学习路程
- python_1
- python语言综述
- Python基础学习-爬虫入门知识
- 基于Python的机器学习实战:AadBoost