Skip to content

Commit 991d8ce

Browse files
committed
First commit of all the raw code for Learn Python3 THW.
1 parent d2e7640 commit 991d8ce

File tree

166 files changed

+2738
-0
lines changed

Some content is hidden

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

166 files changed

+2738
-0
lines changed

ex.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty exercise

ex.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python ex.py

ex1.err

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$ python3.6 python/ex1.py
2+
File "python/ex1.py", line 3
3+
print("I like typing this.
4+
^
5+
SyntaxError: EOL while scanning string literal
6+

ex1.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print("Hello World!")
2+
print("Hello Again")
3+
print("I like typing this.")
4+
print("This is fun.")
5+
print('Yay! Printing.')
6+
print("I'd much rather you 'not'.")
7+
print('I "said" do not touch this.')

ex1.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex1.py

ex10.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tabby_cat = "\tI'm tabbed in."
2+
persian_cat = "I'm split\non a line."
3+
backslash_cat = "I'm \\ a \\ cat."
4+
5+
fat_cat = """
6+
I'll do a list:
7+
\t* Cat food
8+
\t* Fishies
9+
\t* Catnip\n\t* Grass
10+
"""
11+
12+
print(tabby_cat)
13+
print(persian_cat)
14+
print(backslash_cat)
15+
print(fat_cat)

ex10.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex10.py

ex11.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### @export "fake"
2+
import fake_input
3+
input, input = fake_input.create(['38', '6\'2"', '180lbs'])
4+
5+
### @export "code"
6+
print("How old are you?", end=' ')
7+
age = input()
8+
print("How tall are you?", end=' ')
9+
height = input()
10+
print("How much do you weigh?", end=' ')
11+
weight = input()
12+
13+
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
14+

ex11.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### @export "run"
2+
python3.6 ex11.py

ex12.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### @export "fake"
2+
import fake_input
3+
input, input = fake_input.create(['38', '6\'2"', '180lbs'])
4+
5+
### @export "code"
6+
age = input("How old are you? ")
7+
height = input("How tall are you? ")
8+
weight = input("How much do you weigh? ")
9+
10+
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
11+

ex12.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex12.py

ex13.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from sys import argv
2+
# read the WYSS section for how to run this
3+
script, first, second, third = argv
4+
5+
print("The script is called:", script)
6+
print("Your first variable is:", first)
7+
print("Your second variable is:", second)
8+
print("Your third variable is:", third)
9+

ex13.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### @export "first"
2+
python3.6 ex13.py first 2nd 3rd
3+
### @export "others"
4+
python3.6 ex13.py stuff things that
5+
6+
python3.6 ex13.py apple orange grapefruit
7+
8+
### @export "error"
9+
python3.6 ex13.py first 2nd

ex14.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### @export "setup"
2+
import fake_input
3+
input, input = fake_input.create(['Yes',
4+
"San Francisco",
5+
'Tandy 1000'])
6+
7+
### @export "code"
8+
from sys import argv
9+
10+
script, user_name = argv
11+
prompt = '> '
12+
13+
print(f"Hi {user_name}, I'm the {script} script.")
14+
print("I'd like to ask you a few questions.")
15+
print(f"Do you like me {user_name}?")
16+
likes = input(prompt)
17+
18+
print(f"Where do you live {user_name}?")
19+
lives = input(prompt)
20+
21+
print("What kind of computer do you have?")
22+
computer = input(prompt)
23+
24+
print(f"""
25+
Alright, so you said {likes} about liking me.
26+
You live in {lives}. Not sure where that is.
27+
And you have a {computer} computer. Nice.
28+
""")
29+

ex14.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex14.py zed

ex15.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### @export "setup"
2+
import fake_input
3+
input, input = fake_input.create(['ex15_sample.txt'])
4+
5+
### @export "code"
6+
from sys import argv
7+
8+
script, filename = argv
9+
10+
txt = open(filename)
11+
12+
print(f"Here's your file {filename}:")
13+
print(txt.read())
14+
15+
print("Type the filename again:")
16+
file_again = input("> ")
17+
18+
txt_again = open(file_again)
19+
20+
print(txt_again.read())
21+
22+

ex15.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex15.py ex15_sample.txt

ex15_sample.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is stuff I typed into a file.
2+
It is really cool stuff.
3+
Lots and lots of fun to have in here.
4+

ex16.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### @export "setup"
2+
import fake_input
3+
input, input = fake_input.create(['', 'Mary had a little lamb',
4+
'Its fleece was white as snow',
5+
'It was also tasty'])
6+
7+
### @export "code"
8+
from sys import argv
9+
10+
script, filename = argv
11+
12+
print(f"We're going to erase {filename}.")
13+
print("If you don't want that, hit CTRL-C (^C).")
14+
print("If you do want that, hit RETURN.")
15+
16+
input("?")
17+
18+
print("Opening the file...")
19+
target = open(filename, 'w')
20+
21+
print("Truncating the file. Goodbye!")
22+
target.truncate()
23+
24+
print("Now I'm going to ask you for three lines.")
25+
26+
line1 = input("line 1: ")
27+
line2 = input("line 2: ")
28+
line3 = input("line 3: ")
29+
30+
print("I'm going to write these to the file.")
31+
32+
target.write(line1)
33+
target.write("\n")
34+
target.write(line2)
35+
target.write("\n")
36+
target.write(line3)
37+
target.write("\n")
38+
39+
print("And finally, we close it.")
40+
target.close()
41+
42+

ex16.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex16.py test.txt

ex17.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### @export "fake"
2+
import fake_input
3+
input, input = fake_input.create([''])
4+
5+
### @export "code"
6+
from sys import argv
7+
from os.path import exists
8+
9+
script, from_file, to_file = argv
10+
11+
print(f"Copying from {from_file} to {to_file}")
12+
13+
# we could do these two on one line, how?
14+
in_file = open(from_file)
15+
indata = in_file.read()
16+
17+
print(f"The input file is {len(indata)} bytes long")
18+
19+
print(f"Does the output file exist? {exists(to_file)}")
20+
print("Ready, hit RETURN to continue, CTRL-C to abort.")
21+
input()
22+
23+
out_file = open(to_file, 'w')
24+
out_file.write(indata)
25+
26+
print("Alright, all done.")
27+
28+
out_file.close()
29+
in_file.close()

ex17.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# first make a sample file
2+
echo "This is a test file." > test.txt
3+
# then look at it
4+
cat test.txt
5+
# now run our script on it
6+
python3.6 ex17.py test.txt new_file.txt

ex18.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# this one is like your scripts with argv
3+
def print_two(*args):
4+
arg1, arg2 = args
5+
print(f"arg1: {arg1}, arg2: {arg2}")
6+
7+
# ok, that *args is actually pointless, we can just do this
8+
def print_two_again(arg1, arg2):
9+
print(f"arg1: {arg1}, arg2: {arg2}")
10+
11+
# this just takes one argument
12+
def print_one(arg1):
13+
print(f"arg1: {arg1}")
14+
15+
# this one takes no arguments
16+
def print_none():
17+
print("I got nothin'.")
18+
19+
20+
print_two("Zed","Shaw")
21+
print_two_again("Zed","Shaw")
22+
print_one("First!")
23+
print_none()
24+
25+

ex18.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex18.py

ex19.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
def cheese_and_crackers(cheese_count, boxes_of_crackers):
3+
print(f"You have {cheese_count} cheeses!")
4+
print(f"You have {boxes_of_crackers} boxes of crackers!")
5+
print("Man that's enough for a party!")
6+
print("Get a blanket.\n")
7+
8+
9+
print("We can just give the function numbers directly:")
10+
cheese_and_crackers(20, 30)
11+
12+
13+
print("OR, we can use variables from our script:")
14+
amount_of_cheese = 10
15+
amount_of_crackers = 50
16+
17+
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
18+
19+
20+
print("We can even do math inside too:")
21+
cheese_and_crackers(10 + 20, 5 + 6)
22+
23+
24+
print("And we can combine the two, variables and math:")
25+
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
26+

ex19.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex19.py

ex2.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# A comment, this is so you can read your program later.
2+
# Anything after the # is ignored by python.
3+
4+
print("I could have code like this.") # and the comment after is ignored
5+
6+
# You can also use a comment to "disable" or comment out a piece of code:
7+
# print("This won't run.")
8+
9+
print("This will run.")
10+

ex2.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex2.py

ex20.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from sys import argv
2+
3+
script, input_file = argv
4+
5+
def print_all(f):
6+
print(f.read())
7+
8+
def rewind(f):
9+
f.seek(0)
10+
11+
def print_a_line(line_count, f):
12+
print(line_count, f.readline())
13+
14+
current_file = open(input_file)
15+
16+
print("First let's print the whole file:\n")
17+
18+
print_all(current_file)
19+
20+
print("Now let's rewind, kind of like a tape.")
21+
22+
rewind(current_file)
23+
24+
print("Let's print three lines:")
25+
26+
current_line = 1
27+
print_a_line(current_line, current_file)
28+
29+
current_line = current_line + 1
30+
print_a_line(current_line, current_file)
31+
32+
current_line = current_line + 1
33+
print_a_line(current_line, current_file)
34+
35+

ex20.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### @export "setup"
2+
echo "This is line 1" > test.txt
3+
echo "This is line 2" >> test.txt
4+
echo "This is line 3" >> test.txt
5+
### @export "run"
6+
python3.6 ex20.py test.txt

ex21.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
def add(a, b):
3+
print(f"ADDING {a} + {b}")
4+
return a + b
5+
6+
def subtract(a, b):
7+
print(f"SUBTRACTING {a} - {b}")
8+
return a - b
9+
10+
def multiply(a, b):
11+
print(f"MULTIPLYING {a} * {b}")
12+
return a * b
13+
14+
def divide(a, b):
15+
print(f"DIVIDING {a} / {b}")
16+
return a / b
17+
18+
19+
print("Let's do some math with just functions!")
20+
21+
age = add(30, 5)
22+
height = subtract(78, 4)
23+
weight = multiply(90, 2)
24+
iq = divide(100, 2)
25+
26+
print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")
27+
28+
29+
# A puzzle for the extra credit, type it in anyway.
30+
print("Here is a puzzle.")
31+
32+
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
33+
34+
print("That becomes: ", what, "Can you do it by hand?")
35+
36+

ex21.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex21.py

ex22.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty exercise

ex22.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex22.py

ex23.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty exercise

ex23.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 ex23.py

0 commit comments

Comments
 (0)