Skip to content

Commit

Permalink
First commit of all the raw code for Learn Python3 THW.
Browse files Browse the repository at this point in the history
  • Loading branch information
zedshaw committed Feb 25, 2017
1 parent d2e7640 commit 991d8ce
Show file tree
Hide file tree
Showing 166 changed files with 2,738 additions and 0 deletions.
1 change: 1 addition & 0 deletions ex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty exercise
1 change: 1 addition & 0 deletions ex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python ex.py
6 changes: 6 additions & 0 deletions ex1.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$ python3.6 python/ex1.py
File "python/ex1.py", line 3
print("I like typing this.
^
SyntaxError: EOL while scanning string literal

7 changes: 7 additions & 0 deletions ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
print("Hello World!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print('Yay! Printing.')
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
1 change: 1 addition & 0 deletions ex1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex1.py
15 changes: 15 additions & 0 deletions ex10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
1 change: 1 addition & 0 deletions ex10.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex10.py
14 changes: 14 additions & 0 deletions ex11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### @export "fake"
import fake_input

This comment has been minimized.

Copy link
@gitro65

gitro65 Mar 30, 2023

python 3.11 showed syntax error traced back at first line" fake_input"

input, input = fake_input.create(['38', '6\'2"', '180lbs'])

### @export "code"
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weigh?", end=' ')
weight = input()

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

2 changes: 2 additions & 0 deletions ex11.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### @export "run"
python3.6 ex11.py
11 changes: 11 additions & 0 deletions ex12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### @export "fake"
import fake_input
input, input = fake_input.create(['38', '6\'2"', '180lbs'])

### @export "code"
age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weigh? ")

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

1 change: 1 addition & 0 deletions ex12.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex12.py
9 changes: 9 additions & 0 deletions ex13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

9 changes: 9 additions & 0 deletions ex13.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### @export "first"
python3.6 ex13.py first 2nd 3rd
### @export "others"
python3.6 ex13.py stuff things that

python3.6 ex13.py apple orange grapefruit

### @export "error"
python3.6 ex13.py first 2nd
29 changes: 29 additions & 0 deletions ex14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### @export "setup"
import fake_input
input, input = fake_input.create(['Yes',
"San Francisco",
'Tandy 1000'])

### @export "code"
from sys import argv

script, user_name = argv
prompt = '> '

print(f"Hi {user_name}, I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)

print(f"Where do you live {user_name}?")
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
""")

1 change: 1 addition & 0 deletions ex14.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex14.py zed
22 changes: 22 additions & 0 deletions ex15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### @export "setup"
import fake_input
input, input = fake_input.create(['ex15_sample.txt'])

### @export "code"
from sys import argv

script, filename = argv

txt = open(filename)

print(f"Here's your file {filename}:")
print(txt.read())

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())


1 change: 1 addition & 0 deletions ex15.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex15.py ex15_sample.txt
4 changes: 4 additions & 0 deletions ex15_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

42 changes: 42 additions & 0 deletions ex16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
### @export "setup"
import fake_input
input, input = fake_input.create(['', 'Mary had a little lamb',
'Its fleece was white as snow',
'It was also tasty'])

### @export "code"
from sys import argv

script, filename = argv

print(f"We're going to erase {filename}.")
print("If you don't want that, hit CTRL-C (^C).")
print("If you do want that, hit RETURN.")

input("?")

print("Opening the file...")
target = open(filename, 'w')

print("Truncating the file. Goodbye!")
target.truncate()

print("Now I'm going to ask you for three lines.")

line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")

print("I'm going to write these to the file.")

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("And finally, we close it.")
target.close()


1 change: 1 addition & 0 deletions ex16.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex16.py test.txt
29 changes: 29 additions & 0 deletions ex17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### @export "fake"
import fake_input
input, input = fake_input.create([''])

### @export "code"
from sys import argv
from os.path import exists

script, from_file, to_file = argv

print(f"Copying from {from_file} to {to_file}")

# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()

print(f"The input file is {len(indata)} bytes long")

print(f"Does the output file exist? {exists(to_file)}")
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()

out_file = open(to_file, 'w')
out_file.write(indata)

print("Alright, all done.")

out_file.close()
in_file.close()
6 changes: 6 additions & 0 deletions ex17.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# first make a sample file
echo "This is a test file." > test.txt
# then look at it
cat test.txt
# now run our script on it
python3.6 ex17.py test.txt new_file.txt
25 changes: 25 additions & 0 deletions ex18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# this one is like your scripts with argv
def print_two(*args):
arg1, arg2 = args
print(f"arg1: {arg1}, arg2: {arg2}")

# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
print(f"arg1: {arg1}, arg2: {arg2}")

# this just takes one argument
def print_one(arg1):
print(f"arg1: {arg1}")

# this one takes no arguments
def print_none():
print("I got nothin'.")


print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()


1 change: 1 addition & 0 deletions ex18.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex18.py
26 changes: 26 additions & 0 deletions ex19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

def cheese_and_crackers(cheese_count, boxes_of_crackers):
print(f"You have {cheese_count} cheeses!")
print(f"You have {boxes_of_crackers} boxes of crackers!")
print("Man that's enough for a party!")
print("Get a blanket.\n")


print("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)


print("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)


print("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)


print("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

1 change: 1 addition & 0 deletions ex19.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex19.py
10 changes: 10 additions & 0 deletions ex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.

print("I could have code like this.") # and the comment after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
# print("This won't run.")

print("This will run.")

1 change: 1 addition & 0 deletions ex2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex2.py
35 changes: 35 additions & 0 deletions ex20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from sys import argv

script, input_file = argv

def print_all(f):
print(f.read())

def rewind(f):
f.seek(0)

def print_a_line(line_count, f):
print(line_count, f.readline())

current_file = open(input_file)

print("First let's print the whole file:\n")

print_all(current_file)

print("Now let's rewind, kind of like a tape.")

rewind(current_file)

print("Let's print three lines:")

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)


6 changes: 6 additions & 0 deletions ex20.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### @export "setup"
echo "This is line 1" > test.txt
echo "This is line 2" >> test.txt
echo "This is line 3" >> test.txt
### @export "run"
python3.6 ex20.py test.txt
36 changes: 36 additions & 0 deletions ex21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

def add(a, b):
print(f"ADDING {a} + {b}")
return a + b

def subtract(a, b):
print(f"SUBTRACTING {a} - {b}")
return a - b

def multiply(a, b):
print(f"MULTIPLYING {a} * {b}")
return a * b

def divide(a, b):
print(f"DIVIDING {a} / {b}")
return a / b


print("Let's do some math with just functions!")

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")


# A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print("That becomes: ", what, "Can you do it by hand?")


1 change: 1 addition & 0 deletions ex21.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex21.py
1 change: 1 addition & 0 deletions ex22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty exercise
1 change: 1 addition & 0 deletions ex22.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex22.py
1 change: 1 addition & 0 deletions ex23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty exercise
1 change: 1 addition & 0 deletions ex23.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3.6 ex23.py
Loading

0 comments on commit 991d8ce

Please sign in to comment.