Home › Python with Chai › Lesson 11
Files: save the bill
Python with Chai, lesson 11 of 20. A short Hinglish video, simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
A variable lives only in memory (RAM). When the program closes, its value is gone.
A file stays on the disk, like the shop's register book: it is still there tomorrow.
open("bill.txt", "w") opens a file for writing, and with closes it for you automatically.
The code
total = 50
with open("bill.txt", "w") as f:
f.write("Total: Rs " + str(total))
print("Bill saved!")
Output:
Bill saved! (bill.txt now contains: Total: Rs 50)
Try it yourself
- Run it, then open
bill.txtin Notepad. What's inside? - Write 3 lines into the file (hint: add
\nat the end of each line).
Remember
A variable forgets; a file remembers.
← while True: keep the shop open · Files: read the bill back →
All lessons: Python with Chai