Skip to content

Commit 4c51995

Browse files
committed
initial commit
1 parent a83d948 commit 4c51995

19 files changed

+196
-0
lines changed

add.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def add(a,b):
2+
return a+b
3+
add(3,'b')

add_assert.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def add(a,b):
2+
assert b==1, 'B is not one! b=%d' % b
3+
return a+b
4+
5+
print add(1,2)

add_debug.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pdb
2+
def add(a,b):
3+
if (__debug__ and b!=1):
4+
print 'B is not one! b=%d' % b
5+
pdb.set_trace()
6+
return a+b
7+
8+
print add(1,2)

add_except.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def add(a,b=0):
2+
try:
3+
return a+b
4+
except TypeError:
5+
print 'Do not know how to add different types'
6+
7+
add(3,'b')

break_else.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
for n in range(2, 10):
2+
for x in range(2, n):
3+
if n % x == 0:
4+
print(n, 'equals', x, '*', n//x)
5+
break
6+
else:
7+
# loop fell through without finding a factor
8+
print(n, 'is a prime number')

complaint.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
2+
while True:
3+
ok = raw_input(prompt)
4+
if ok in ('y', 'ye', 'yes'):
5+
return True
6+
if ok in ('n', 'no', 'nop', 'nope'):
7+
return False
8+
retries = retries - 1
9+
if retries < 0:
10+
raise OSError('uncooperative user')
11+
print(complaint)
12+
13+
14+
ask_ok('Do you really want to quit?')
15+
ask_ok('OK to overwrite the file?', 2)
16+
ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')
17+

continue.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
for num in range(2, 10):
2+
if num % 2 == 0:
3+
print("Found an even number", num)
4+
continue
5+
print("Found a number", num)

fib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def fib(n): # write Fibonacci series up to n
2+
"""Print a Fibonacci series up to n."""
3+
a, b = 0, 1
4+
while a < n:
5+
print a,
6+
a, b = b, a+b
7+
8+
print fib.__doc__
9+
fib(2000)

for.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
words = ['cat', 'window', 'defenestrate']
2+
for w in words:
3+
print(w, len(w))

fun.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
def fun(x):
3+
return x + 2
4+
5+
class MyTest(unittest.TestCase):
6+
def test(self):
7+
self.assertEqual(fun(3), 4)
8+
9+
if __name__ == '__main__':
10+
unittest.main()

0 commit comments

Comments
 (0)