Python intro notes

This commit is contained in:
2019-09-27 00:06:03 +02:00
parent ea7fd79d11
commit c9cc068203
17 changed files with 471 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
"""
IO is boring but useful, here is how.
"""
# Input and output from the user is easy.
a = input("Some prompt here: ")
print(a)
# Input from files are a bit more tricky
# The way taught in ITGK
f = open("file.txt")
f.close()
# The correct way
with open("file.txt", "r") as f:
lines = f.readlines()
for line in lines:
print(line)
# Or even better
with open("file.txt", "r") as f:
for line in f:
print(line)
# Even better
with open("newFile.txt", 'w') as f:
f.write("This is a new file\nI like it.\n")
# There are four modes of reading, r, w, a and r+