Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
eb35717bd9
21 changed files with 324 additions and 21 deletions
16
kjg/python-the-hard-way/e10.py
Normal file
16
kjg/python-the-hard-way/e10.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
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)
|
||||||
|
print("\u1234")
|
8
kjg/python-the-hard-way/e11.py
Normal file
8
kjg/python-the-hard-way/e11.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
print("How old are you?", end=' ')
|
||||||
|
age = input()
|
||||||
|
print("How tall are you?", end=' ')
|
||||||
|
height = input()
|
||||||
|
print("How much do you weight?", end=' ')
|
||||||
|
weight = input()
|
||||||
|
|
||||||
|
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
|
5
kjg/python-the-hard-way/e12.py
Normal file
5
kjg/python-the-hard-way/e12.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
age = input("How old are you? ")
|
||||||
|
height = input("How tall are you? ")
|
||||||
|
weight = input("How much do you weight? ")
|
||||||
|
|
||||||
|
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
|
8
kjg/python-the-hard-way/e13.py
Normal file
8
kjg/python-the-hard-way/e13.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
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)
|
23
kjg/python-the-hard-way/e14.py
Normal file
23
kjg/python-the-hard-way/e14.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
from sys import argv
|
||||||
|
|
||||||
|
script, user_name, first, second = 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(f"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 suer where that is.
|
||||||
|
And you have a {computer} computer. Nice.
|
||||||
|
{first} and {second}
|
||||||
|
""")
|
||||||
|
|
19
kjg/python-the-hard-way/e15.py
Normal file
19
kjg/python-the-hard-way/e15.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
from sys import argv
|
||||||
|
|
||||||
|
script, filename = argv
|
||||||
|
|
||||||
|
txt = open(filename)
|
||||||
|
|
||||||
|
|
||||||
|
print(f"Here's your file {filename}: ")
|
||||||
|
print(txt.read())
|
||||||
|
|
||||||
|
txt.close()
|
||||||
|
|
||||||
|
print("Type the filename again : ")
|
||||||
|
file_again = input("> ")
|
||||||
|
|
||||||
|
txt_again = open(file_again)
|
||||||
|
|
||||||
|
print(txt_again.read())
|
||||||
|
|
33
kjg/python-the-hard-way/e16.py
Normal file
33
kjg/python-the-hard-way/e16.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
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 th 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()
|
24
kjg/python-the-hard-way/e17.py
Normal file
24
kjg/python-the-hard-way/e17.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
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()
|
21
kjg/python-the-hard-way/e18.py
Normal file
21
kjg/python-the-hard-way/e18.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# this one is like your scripts wih argv
|
||||||
|
def print_two(*args):
|
||||||
|
arg1, arg2 = args
|
||||||
|
print(f"arg1: {arg1}, arg2; {arg2}")
|
||||||
|
|
||||||
|
# ok, that *arg is actually pintless, 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 argument
|
||||||
|
def print_none():
|
||||||
|
print("I got nothing.")
|
||||||
|
|
||||||
|
print_two("Zed","Shaw")
|
||||||
|
print_two_again("Zed","Shaw")
|
||||||
|
print_one("First!")
|
||||||
|
print_none()
|
20
kjg/python-the-hard-way/e19.py
Normal file
20
kjg/python-the-hard-way/e19.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
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)
|
35
kjg/python-the-hard-way/e20.py
Normal file
35
kjg/python-the-hard-way/e20.py
Normal file
|
@ -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
|
||||||
|
current_line += 1
|
||||||
|
print_a_line(current_line, current_file)
|
||||||
|
|
||||||
|
#current_line = current_line + 1
|
||||||
|
current_line += 1
|
||||||
|
print_a_line(current_line, current_file)
|
40
kjg/python-the-hard-way/e21.py
Normal file
40
kjg/python-the-hard-way/e21.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
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 multiplay(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 = multiplay(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, multiplay(weight, divide(iq, 2))))
|
||||||
|
|
||||||
|
print("That becomes: ", what, "Can you do it by hand?")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
11
kjg/python-the-hard-way/e8.py
Normal file
11
kjg/python-the-hard-way/e8.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
formatter = "{} {} {} {}"
|
||||||
|
print(formatter.format(1, 2, 3, 4))
|
||||||
|
print(formatter.format("one", "two", "three", "four"))
|
||||||
|
print(formatter.format(True, False, False, True))
|
||||||
|
print(formatter.format(formatter, formatter, formatter, formatter))
|
||||||
|
print(formatter.format(
|
||||||
|
"Try your",
|
||||||
|
"Own text here",
|
||||||
|
"Maybe a poem",
|
||||||
|
"Or a song about fear"
|
||||||
|
))
|
14
kjg/python-the-hard-way/e9.py
Normal file
14
kjg/python-the-hard-way/e9.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
# Here's some new strange stuff, remember type it exactly
|
||||||
|
|
||||||
|
days = "Mon Tue Wed Thu Fri Sat Sun"
|
||||||
|
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
|
||||||
|
|
||||||
|
print("Here are the days: ", days)
|
||||||
|
print("Here are the months: ", months)
|
||||||
|
|
||||||
|
print("""
|
||||||
|
There's something going on here.
|
||||||
|
with the three double-quotes.
|
||||||
|
We'll be able to type as much as we like.
|
||||||
|
Even 4 lines if we want, or 5 or 6
|
||||||
|
""")
|
3
kjg/python-the-hard-way/ex15_sample.txt
Normal file
3
kjg/python-the-hard-way/ex15_sample.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
This is stuff I typed into a file.
|
||||||
|
It is really cool stuff .
|
||||||
|
Lots and lots of fun to have in here.
|
1
kjg/python-the-hard-way/new_file.txt
Normal file
1
kjg/python-the-hard-way/new_file.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
This is a test file.
|
3
kjg/python-the-hard-way/new_file2.txt
Normal file
3
kjg/python-the-hard-way/new_file2.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
test line1
|
||||||
|
test line2
|
||||||
|
test line3
|
3
kjg/python-the-hard-way/test.txt
Normal file
3
kjg/python-the-hard-way/test.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
test line1
|
||||||
|
test line2
|
||||||
|
test line3
|
|
@ -66,3 +66,38 @@ Its fleece was white as snow.
|
||||||
And everywhere that Mary went.
|
And everywhere that Mary went.
|
||||||
..........
|
..........
|
||||||
Cheese Burger
|
Cheese Burger
|
||||||
|
*** Python #2:
|
||||||
|
**** ex8
|
||||||
|
learn about a more complicated formatting of a string. {}
|
||||||
|
**** ex9
|
||||||
|
how to print for multiline or nextline
|
||||||
|
**** ex10
|
||||||
|
how to use \ (using special chracter)
|
||||||
|
**** ex11
|
||||||
|
how to use key input
|
||||||
|
**** ex12
|
||||||
|
how to use key input with print
|
||||||
|
**** ex13
|
||||||
|
how to use an argument when you run script
|
||||||
|
**** ex14
|
||||||
|
how to use an argument with input
|
||||||
|
*** Python #3:
|
||||||
|
**** ex15
|
||||||
|
How to use read function from file
|
||||||
|
**** ex16
|
||||||
|
How to write file
|
||||||
|
**** ex17
|
||||||
|
How to copy file
|
||||||
|
**** ex18
|
||||||
|
How to use function
|
||||||
|
**** ex19
|
||||||
|
How to use function and variables
|
||||||
|
**** ex20
|
||||||
|
How to use function with file
|
||||||
|
**** ex21
|
||||||
|
How to use fucntion with return
|
||||||
|
**** ex22
|
||||||
|
repeat from ex1 to ex21
|
||||||
|
" # () ' + . < ? , = / % - * {} \n \t
|
||||||
|
open read close truncate def return print
|
||||||
|
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
*python
|
|
||||||
|
|
||||||
*** Python #1:
|
|
||||||
**** ex0
|
|
||||||
**** ex1
|
|
||||||
>>> 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.')
|
|
||||||
Hello World!
|
|
||||||
Hello Again
|
|
||||||
I like typing this.
|
|
||||||
This is fun.
|
|
||||||
Yay! Printing.
|
|
||||||
I'd much rather you 'not'.
|
|
||||||
I "said" do not touch this.
|
|
Loading…
Reference in a new issue