Skip to content

Commit ce21811

Browse files
committed
add excise py file
0 parents  commit ce21811

24 files changed

+613
-0
lines changed

arg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/python
2+
from sys import argv
3+
script,first,second,third = argv
4+
print "the script is called:", script
5+
print "your first variable is:", first
6+
print "your second variable is:", second
7+
print "your third variable is:", third

ex15.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/python
2+
from sys import argv
3+
script,filename=argv
4+
print "we're going to erase %r." % filename
5+
print "If you don't want that, hit CTRL-C(^C)."
6+
print "If you do want that, hit RETURN."
7+
raw_input("?")
8+
print "opening the file..."
9+
target = open(filename, 'w')
10+
print "Truncating the file. Goodbye!"
11+
target.truncate()
12+
print "Now I'm going to ask you for three lines."
13+
line1=raw_input("line 1:")
14+
line2=raw_input("line 2:")
15+
line3=raw_input("line 3:")
16+
print "I'm going to write these to the file."
17+
target.write(line1)
18+
target.write("\n")
19+
target.write(line2)
20+
target.write("\n")
21+
target.write(line3)
22+
target.write("\n")
23+
print "and finally, we close it."
24+
target.close()

ex15.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abc1
2+
def2
3+
ghk3

ex17.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
from sys import argv
3+
from os.path import exists
4+
script,from_file,to_file=argv
5+
print "copying from %s to %s" %(from_file, to_file)
6+
input =open (from_file)
7+
indata=input.read()
8+
print "the input file is %d bytes long" % len(indata)
9+
print "does the output file exist? %r" % exists(to_file)
10+
print "ready, hit RETURN to continue, CTRL-C to abort."
11+
raw_input()
12+
output=open(to_file,'w')
13+
output.write(indata)
14+
print "alright, all done"
15+
output.close()
16+
input.close()

ex17.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abc1
2+
def2
3+
ghk3

ex18.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
def print_two(*args):
3+
arg1,arg2=args
4+
print "arg1: %r, arg2: %r" % (arg1,arg2)
5+
def print_two_again(arg1,arg2):
6+
print "arg1: %r, arg2: %r" %(arg1, arg2)
7+
def print_one(arg1):
8+
print "arg1: %r" % arg1
9+
def print_none():
10+
print "I got nothing."
11+
12+
print_two("zed", "shaw")
13+
print_two_again("zed","shaw")
14+
print_one("first!")
15+
print_none()

ex19.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/python
2+
def cheese_and_crackers(cheese_count, boxes_of_crackers):
3+
print "you have %d cheeses!" % (cheese_count)
4+
print "you have %d boxes of crackers!" % boxes_of_crackers
5+
print "man that's enough for a party!"
6+
print "get a blanket.\n"
7+
print "we can just give the function numbers directly:"
8+
cheese_and_crackers(20,30)
9+
print "OR, we can use variables from our script:"
10+
amount_of_cheese=10
11+
amount_of_crackers=50
12+
cheese_and_crackers(amount_of_cheese,amount_of_crackers)
13+
print "we can even do math inside too:"
14+
cheese_and_crackers(10+20, 5+6)
15+
print "we can combine the two, variables and math:"
16+
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
17+
18+
19+
20+

ex20.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
from sys import argv
3+
script, input_file=argv
4+
def print_all(f):
5+
print f.read()
6+
def rewind(f):
7+
f.seek(0)
8+
def print_a_line(line_count, f):
9+
print line_count, f.readline()
10+
current_file=open(input_file)
11+
print "first let's print the whole file:\n"
12+
print_all(current_file)
13+
print "now let's rewind, kind of like a tape."
14+
rewind(current_file)
15+
print "let's print three lines:"
16+
current_line=1
17+
print_a_line(current_line, current_file)
18+
current_line = current_line +1
19+
print_a_line(current_line, current_file)
20+
current_line = current_line +1
21+
print_a_line(current_line, current_file)
22+

ex21.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
def add(a,b):
3+
print "ADDING %d + %d" %(a,b)
4+
return a+b
5+
def subtract(a,b):
6+
print "SUBTRACTING %d - %d" %(a,b)
7+
return a-b
8+
def multiply(a,b):
9+
print "MULTIPLYING %d * %d" %(a,b)
10+
return a*b
11+
def divide(a,b):
12+
print "DIVIDING %d / %d" %(a,b)
13+
return a/b
14+
print "Let's do some math with just functions!"
15+
age = add(30,5)
16+
height= subtract(78,4)
17+
weight= multiply(90,2)
18+
iq = divide(100,2)
19+
print "Age: %d, Height: %d, Weight: %d, IQ: %d" %(age, height,weight, iq)
20+
# A puzzle
21+
print "Here is a puzzle."
22+
what = add (age,subtract(height,multiply(weight,divide(iq,2))))
23+
print "that becames: %d, can you do it by hand?" %(what)
24+
25+

ex24.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python
2+
print "Let's practice everything."
3+
print 'you\'d need to know \'bout eccapes with \\ that do \n newlines and \t tabs.'
4+
poem = """
5+
\tthe lovely world
6+
with logic so firmly planted
7+
cannot discern \n the needs of love
8+
nor comprehend passion from intuition
9+
and requires an explanation
10+
\n\t\twhere there is none.
11+
"""
12+
13+
print "----------------------"
14+
print poem
15+
print "----------------------"
16+
five = 10 -2 +3 -6
17+
print "this should be five: %s" %five
18+
def secret_formula(started):
19+
jelly_beans=started * 500
20+
jars=jelly_beans / 100
21+
crates=jars / 100
22+
return jelly_beans, jars,crates
23+
start_point = 10000
24+
beans, jars, crates = secret_formula(start_point)
25+
print "with a starting point of: %d" % start_point
26+
print "we'd have %d beans, %d jars, and %d crates." %(beans, jars, crates)
27+
start_point = start_point / 10
28+
print "we can also do that this way:"
29+
print "we'd have %d beans, %d jars, and %d crates." %secret_formula(start_point)

0 commit comments

Comments
 (0)