Skip to content

Commit 3aa2933

Browse files
author
kangxiaoyu
committed
函数的相关学习
1 parent 6459fc9 commit 3aa2933

13 files changed

+83
-0
lines changed

2017-04-21/for1list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
#! -*- coding:utf-8 -*-
3+
#这里可以写中文。
4+
print [x * x for x in range(1,11) if x %2 == 0]

2017-04-21/for2list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
#! -*- coding:utf-8 -*-
3+
#这里可以写中文。
4+
print [x*x for x in range(1,11) if x % 2 == 0]

2017-04-21/for3list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
#! -*- coding:utf-8 -*-
3+
#这里可以写中文。
4+
print [m + n for m in 'ABC' for n in 'XYZ']

2017-04-21/for4list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
import os
4+
print [d for d in os.listdir('.')]

2017-04-21/for5list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
d = {'x':'A','y':'B','z':'C'}
2+
for k,v in d.items():
3+
print(k, '=', v)
4+

2017-04-21/for6list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
d = {'x':'A','y':'B','z':'c'}
4+
print [k +'=' + v for k,v in d.items()]

2017-04-21/forlist.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/env python
2+
#! -*- coding:utf-8 -*-
3+
#这里可以写中文。
4+
L = []
5+
for x in range(1,11):
6+
L.append(x*x)
7+
8+
print L

2017-04-21/iterate.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
from collections import Iterable
4+
for i, value in enumerate(['A', 'B', 'C']):
5+
print(i,value)

2017-04-21/iterate.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
最后一个小问题,如果要对list实现类似Java那样的下标循环怎么办?
2+
Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身。
3+
for i,value in enumerate(['A','B','C']):
4+
print(i,value)
5+
6+
for x,y in [(1,1),(2,4),(3,9)]:
7+
print(x,y)
8+
9+
for x,y in [(1,1),(2,4),(3,9)]:
10+
... print(x,y)
11+
...
12+
(1, 1)
13+
(2, 4)
14+
(3, 9)
15+
16+
任何可迭代对象都可以作用于for循环,包括我们自定义的数据类型,只要符合迭代条件,就可以使用for循环。
17+

2017-04-21/str_low.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L = ['Hello','World','IBM','Apple']
4+
print [s.lower() for s in L]

2017-04-21/test1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=[]
4+
n=1
5+
while n <=99:
6+
L.append(n)
7+
n = n +2
8+
print L

2017-04-21/列表生成式.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的
2+
生成式。
3+
举个例子, 要生成list[1,2,3,--10],可以用list(range(1,11))\
4+
list(range(1,11))
5+
6+
list(range(1,11))
7+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2017-04-21/高阶特性。txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3。即索引0,1,2,正好是3个元素。
2+
3+
如果第一个索引是0,还可以省略:
4+
5+
>>> L[:3]
6+
['Michael', 'Sarah', 'Tracy']
7+
8+
检查是否可以迭代
9+
from collectons import Iterable
10+
isinstance('abc',Iterable)

0 commit comments

Comments
 (0)