Flyttet python-kurset ut av samlerepoet

This commit is contained in:
2025-03-04 15:18:01 +01:00
parent c9cc068203
commit f9eb912b6a
38 changed files with 3 additions and 1164 deletions

32
notes/06_io.py Normal file
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+