-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit of all the raw code for Learn Python3 THW.
- Loading branch information
Showing
166 changed files
with
2,738 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# empty exercise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python ex.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex1.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex10.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### @export "fake" | ||
import fake_input | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### @export "run" | ||
python3.6 ex11.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex12.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex14.py zed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex15.py ex15_sample.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex16.py test.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex18.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex19.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex2.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex21.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# empty exercise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex22.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# empty exercise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3.6 ex23.py |
Oops, something went wrong.
python 3.11 showed syntax error traced back at first line" fake_input"