Merge remote-tracking branch 'youngjin/master'

This commit is contained in:
Nico Schottelius 2020-05-22 19:51:58 +02:00
commit 272be628cb
19 changed files with 297 additions and 1 deletions

View file

@ -1,3 +1,10 @@
* 2020-05-22
**** DONE Lecture content
CLOSED: [2020-05-23 토 00:21]
- Same structure as "Python #2"
- Exercises 15-22
**** Lecture material
- Available on https://cloud.ungleich.ch/s/435FyfrQyEq6oF3
* 2020-05-20
*** Python #2:
**** DONE Lecture content

View file

@ -0,0 +1,19 @@
from sys import argv # include argv
script, filename = argv # define a argument
txt = open(filename) # open the file
print(f"Here's your file {filename}:") # print a script
print(txt.read()) # read and print the text from the file
txt.close()
print("Type the filename again:") # print a script
file_again = input("> ") # input from stdin
txt_again = open(file_again) # open the file
print(txt_again.read()) # read and print the text from the file
txt_again.close()

View 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.

View file

@ -0,0 +1,41 @@
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")
target.write(f"""{line1}
{line2}
{line3}
"""
)
print("And finally, we close it.")
target.close()
txt = open(filename)
print(txt.read())

View file

@ -0,0 +1,3 @@
1234
5678
90-=

View file

@ -0,0 +1,3 @@
line1
line2
line3

View file

@ -0,0 +1,28 @@
from sys import argv
from os.path import exists
import shutil
script, from_file, to_file = argv
print(f"Copying from {from_file} to {to_file}")
#open(to_file, 'w').write(open(from_file).read())
shutil.copyfile(from_file, 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()

View file

@ -0,0 +1 @@
This is a test file.

View file

@ -0,0 +1 @@
This is a test file.

View file

@ -0,0 +1 @@
This is a test file.

View file

@ -0,0 +1 @@
This is a test file.

View file

@ -0,0 +1,23 @@
# 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()

View file

@ -0,0 +1,63 @@
def cheese_and_crackers(cheese_count, boxes_of_crackers): # define a function
print(f"You have {cheese_count} cheeses!") # print cheese_count
print(f"You have {boxes_of_crackers} boxes of crackers!") # print boxex_of_crackers
print("Man that's enough for a party!") # print a script
print("Get a blanket.\n") # print a script
def my_f(arg1, arg2):
total = arg1 + arg2
print(f"arg1: {arg1}, arg2: {arg2}, total: {total}")
print("We can just give the function numbers directly:") # print a script
cheese_and_crackers(20, 30) # call the function
print("OR, we can use variables from our script:") # print a script
amount_of_cheese = 10 # define a value
amount_of_crackers = 50 # define a value
cheese_and_crackers(amount_of_cheese, amount_of_crackers) # call the function
print("We can even do math inside too:") # print a script
cheese_and_crackers(10 + 20, 5 + 6) # call the function
print("And we can combine the two, variables and math:") # print a script
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) # call the function
my_f(1, 2)
my_f(2*4, 8/2)
my_f("2*4", "8/2")
my_f("not", "bad")
n_arg1 = 1
n_arg2 = 2
my_f(n_arg1, n_arg2)
s_arg1 = "good"
s_arg2 = "luck"
my_f(s_arg1, s_arg2)
t_arg1 = 8/2+3
t_arg2 = 8%2
my_f(t_arg1, t_arg2)
m_a1 = """line1
line2
line3
"""
m_a2 = """line4
line5
line6
"""
my_f(m_a1, m_a2)
my_f(open("test.txt").read(), open("test.txt").read())
f = "{} {}"
my_f(f.format(1, 2), f.format(3, 4))

View file

@ -0,0 +1,35 @@
from sys import argv # import argv
script, input_file = argv # define argv
def print_all(f): # define a function
print(f.read()) # print all
def rewind(f): # define a function
f.seek(0) # jump the starting point of a file
def print_a_line(line_count, f): # define a function
print(line_count, f.readline()) # pinrt a line on the file
current_file = open(input_file) # open a file
print("First let's print the whole file:\n") # print a script
print_all(current_file) # call the function
print("Now let's rewind, kind of like a tape.") # print a script
rewind(current_file) # call the function
print("Let's print three lines:") # print a script
current_line = 1 # define a value
print_a_line(current_line, current_file) # call the function
#current_line = current_line + 1 # define a value
current_line += 1
print_a_line(current_line, current_file) # call the function
#current_line = current_line + 1 # define a value
current_line += 1
print_a_line(current_line, current_file) # call the function

View file

@ -0,0 +1,3 @@
This is line 1
This is line 2
This is line 3

View file

@ -0,0 +1,33 @@
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 - 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?")

View file

@ -0,0 +1,3 @@
Mary had a little lamb
Its fleece was white as snow
It was also tasty

View file

@ -1,3 +1,32 @@
* 2020-05-22
** note
- ex15.py
- The argument method is better than using stdin. becouse bash-completion could be used.
- python3.8 shell
- >>> filename=input()
- ex15_sample.txt
- >>> txt = open(filename)
- >>> txt_script = txt.read()
- >>> print(txt_script)
- This is stuff I typed into a file.
- It is really cool stuff.
- Lots and lots of fun to have in here
- ex16.py
- 'w' is to open a file on write mode.
- If a file is openned with 'w', target.truncate() should not be need. the 'w' mode always overwrite a file.
- ex17.py
- cat is concatenate files to stdout
- If file.close() is not called, the file which is openned by python chould not be modified on orther programs.
- ex18.py
- none
- ex19.py
- none
- ex20.py
- file.seek is to move to new file posotion
- ex21.py
- none
- ex22.py
- none
* 2020-05-20
** note
- ex8.py
@ -15,7 +44,6 @@
- none
- ex14.py
- none
* 2020-05-18
** note
- ex1.py