python笔记(2)

从今天起,做一个简单的人,踏实务实。不沉溺幻想。不庸人自扰。要快乐,要开朗,要坚韧,要温暖,对人要真诚。要诚恳,要坦然,要慷慨,要宽容,要有平常心。
继续List: 删除元素:
 
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]

清空list
 
a[ : ] = []
del a[:]

list作为栈使用(后入先出):
 
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5

用负数索引:
 
b=[1, 2, 3, 4]
b[-2] #3

"+"组合list:
 
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']

查出某元素在list中的数量:
 
lst.('hello') # hello 的数量

list排序:
 
sort()
#对链表中的元素进行适当的排序。 reverse()
#倒排链表中的元素

函数指针的问题:
 
def f2(a, L=[])
L.append(a)
return L print(f2(1)) # 1
print(f2(2)) # 1, 2 L在这次函数调用时是[1]
print(f2(3)) # 1, 2, 3

函数中的参数中有:   *参数名 :表示任意个数的参数   **  :表示dictionary参数
控制语句:  IF:
 
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'

FOR:
 
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)  

WHILE:
 
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

pass :空操作语句
 
while True:
pass

dictionary: 键值对的数据结构 用list来构造dictionary:
 
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}

有趣的比较:
 
x = [] #list
x[2] = 'foo' #出错
x = {} #dictionary
x[2] = 'foo' #正确

内容比较杂,学到什么就记下来。完全利用工作中的空闲和业余时间来完成,更加充实了。

到此这篇关于python笔记(2)就介绍到这了。苦海有涯。而学无涯,志者战高考,惰者畏高考。更多相关python笔记(2)内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: python