前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python列表知识补充

Python列表知识补充

作者头像
用户1173509
发布2018-01-17 14:55:21
5960
发布2018-01-17 14:55:21
举报
文章被收录于专栏:CaiRui

1、import this  Python之禅,圣经。

代码语言:javascript
复制
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2、title()  使字符串第一个字母大写

代码语言:javascript
复制
>>> s = "cairui"
>>> print(s.title())
Cairui

3、列表的负索引

-1代表最后一个元素,-2代表倒数第二元素

代码语言:javascript
复制
>>> s = ['cairui',123,456,789,'lei']
>>> print(s[-1])
lei
>>> print(s[-2])
789
>>> 

4、range

代码语言:javascript
复制
>>> range(1,5)
[1, 2, 3, 4]
代码语言:javascript
复制
>>> numbers = list(range(1,5))
>>> print(numbers)
[1, 2, 3, 4]
>>> 
代码语言:javascript
复制
>>> i1 = list(range(2,11,2))
>>> print i1
[2, 4, 6, 8, 10]

5、min(列表)  取最小值  max(列表) 取最大值  sum(列表) 求和

6.用切片打印整个列表

代码语言:javascript
复制
>>> players = [1,2,3,4,5]
>>> print(players[:])
[1, 2, 3, 4, 5]
>>> 

7、append  在列表末尾添加元素  insert(索引,内容)在第几个索引位置添加元素  

代码语言:javascript
复制
>>> s = ['a',123,456,789]
>>> s.append('rui')
>>> print(s)
['a', 123, 456, 789, 'rui']

['a', 123, 456, 789, 'rui'] >>> s.insert(0,'rui') >>> print(s) ['rui', 'a', 123, 456, 789, 'rui'] >>> 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、import this  Python之禅,圣经。
  • 2、title()  使字符串第一个字母大写
  • 3、列表的负索引
  • 4、range
  • 5、min(列表)  取最小值  max(列表) 取最大值  sum(列表) 求和
  • 6.用切片打印整个列表
  • 7、append  在列表末尾添加元素  insert(索引,内容)在第几个索引位置添加元素  
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档