Skip to content

Commit 8c9fb85

Browse files
committed
python基础教程(第2版)-书上练习(python3)
0 parents  commit 8c9fb85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1910
-0
lines changed

#2-my-1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#2-my1.py
2+
wei=['tian wei',24]
3+
hong=['li hong',48]
4+
guang=['tian guang zhi',49]
5+
database=[wei,hong,guang]
6+
print(database)
7+
while True:
8+
name=input('input your full name:')
9+
find=False
10+
for firstname in database:
11+
if firstname[0]==name:
12+
print('your name is :'+name,'and your age is',str(firstname[1]))
13+
find=True
14+
if find==False:
15+
print('you are not in the list')
16+
17+
18+
19+

#7-my-3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#7-my-3.py
2+
#super class

#qt1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#qt1.py
2+
class A:
3+
def __init__(self, *args):
4+
self.contained = args
5+
def __iter__(self):
6+
for elem in self.contained:
7+
yield elem + 1
8+
9+
a=A(1,2,3)
10+
print(a.next())
11+
print(a.next())
12+
print(a.next())

1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#2-3.py
2+
#to print a sentense in the midlle of a box
3+
sentense=input("Sentence:")
4+
screen_width=80
5+
text_width=len(sentense)
6+
box_width=text_width+6
7+
left_margin=(screen_width-box_width)//2
8+
print
9+
print(' '*left_margin+'+'+'-'*(box_width-2)+'+')
10+
print(' '*(left_margin+4)+'|'+' '*text_width+'|')
11+
print(' '*(left_margin+4)+'|'+sentense+'|')
12+
print(' '*(left_margin+4)+'|'+' '*text_width+'|')
13+
print(' '*left_margin+'+'+'-'*(box_width-2)+'+')
14+
print
15+

10-my-1.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#10-my-1.py
2+
#import module
3+
4+
5+
6+
#import sys
7+
#sys.path.append('f:\python')
8+
import foo
9+
print(dir(foo))
10+
print()
11+
print()
12+
import copy
13+
#print(copy.__all__)
14+
#print(copy.__doc__)
15+
#from copy import dispatch_table
16+
#copy.dispatch_table
17+
18+
#public interface
19+
#print(copy.__all__)
20+
print()
21+
#print(copy.__doc__)
22+
print()
23+
#print(copy.__file__)
24+
print()
25+
#print(dir(copy))
26+
print()
27+
#print(help(copy))
28+
print()
29+
#print([n for n in dir(copy) if not n.startswith('_')])

11-1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#11-1.py
2+
#pipeline
3+
#somescript.py
4+
#sys.stdin
5+
#sys.stdout
6+
#sys.stderror
7+
import sys
8+
sys.path.append('..')
9+
import foo2
10+
import sys
11+
text=sys.stdin.read()
12+
words=text.split()
13+
count=len(words)
14+
print('contain %d words'%count)
15+

11-11.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#11-11.py
2+
#fileinput.input
3+
def process(line):
4+
print(line)
5+
import fileinput
6+
for line in fileinput.input('somefile.txt'):
7+
process(line)
8+

11-12.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#11-12.py
2+
#file iteration
3+
f=open(r'somefile.txt')
4+
for line in f:
5+
print(line)
6+
f.close()

11-13.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#11-13.py
2+
from __future__ import with_statement
3+
for line in open(r'somefile.txt'):
4+
print(line)
5+
6+
print(list(open(r'somefile.txt')))
7+
print(open(r'somefile.txt'))
8+
a,b,c=open(r'somefile.txt')
9+
10+
print(a)
11+
print(b)
12+
print(c)

11-3.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#11-3.py
2+
#basic file functions
3+
f=open(r'somefile.txt')
4+
print(f.read(7))
5+
print(f.read(4))
6+
f.close()
7+
8+
9+
f=open(r'somefile.txt')
10+
print(f.read())
11+
f.close()
12+
13+
14+
15+
f=open(r'somefile.txt')
16+
for i in range(3):
17+
print(f.readline())
18+
f.close()
19+
20+
import pprint
21+
pprint.pprint(open(r'somefile.txt').readlines())
22+
23+
24+
f=open(r'somefile.txt','w')
25+
f.write('this\nis no\n haiku')
26+
f.close()
27+
f=open(r'somefile.txt','r')
28+
lines=f.readlines()
29+
print(lines)
30+
f.close()
31+
lines[1]="isn't a\n"
32+
33+
f=open(r'somefile.txt','w')
34+
f.writelines(lines)
35+
f.close()
36+
pprint.pprint(open(r'somefile.txt').readlines())

0 commit comments

Comments
 (0)