这系列博文是博主学习’python编程:从入门到实践’这本书的学习笔记

1.python3 ide推荐geany

py注释用’#’

import this显示py编程原则

2.字符串

用title()使得字符串可以首字母大写,upper()全大写,lower()全小写;字符串连接用+;字符串删除空白用strip,删除左空白lstrip,删除右空白rstrip;数字转字符串str(int)

3.列表基础

Python为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1 ,可让Python返回最后一个列表元素,这种约定也适用于其他负数索引,例如,索引-2 返回倒数第二个列表元素,索引-3 返回倒数第三个列表元素,以此类推。

#coding:utf-8
#允许汉语注释
#define list
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
#append item
bicycles.append("t1")
print(bicycles)
#insert item
bicycles.insert(2,"t2")
print(bicycles)
#del item, you could not find this item anymore
del bicycles[-1]
print(bicycles)
del bicycles[2]
print(bicycles)
#pop item , you can use this item later
pt=["a","b","c","d"]
a=pt.pop() #index_default=-1
print(pt)
print(a)
#remove by value,just remove the first one
pt.remove("b")
print(pt)

#sort by char永久性排序
pt=["a1","cc","bb","ff"]
pt.sort()
print(pt)
#按与字母顺序相反的顺序排列列表元素
pt.sort(reverse=True)
print(pt)
#使用函数sorted() 对列表进行临时排序
pt=["a","c","b"]
print(sorted(pt))
print(sorted(pt,reverse=True))
print(pt)
#方法reverse() 永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此只需对列表再次调用reverse() 即可
pt=["a","c","b","hah"]
pt.reverse()
print(pt)
pt.reverse()
print(pt)
#确定列表的长度
print(len(pt))