diff --git a/your-first-python-program.html b/your-first-python-program.html index b3a3d08..32bf5b3 100644 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -60,7 +60,13 @@ if __name__ == "__main__": you@localhost:~$ python3 humansize.py 1.0 TB 931.3 GiB -
FIXME: this would be a good place to explain what the program, you know, actually does. + +
What just happened? You executed your first Python program. You called the Python intepreter on the command line, and you passed the name of the script you wanted Python to execute. The script defines a single function, the approximate_size() function, which takes an exact file size in bytes and calculates a “pretty” (but approximate) size. (You’ve probably seen this in Windows Explorer, or the Mac OS X Finder, or Nautilus or Dolphin or Thunar on Linux. If you display a folder of documents as a multi-column list, it will display a table with the document icon, the document name, the size, type, last-modified date, and so on. If the folder contains a 1093-byte file named TODO, your file manager won’t display TODO 1093 bytes; it’ll say something like TODO 1 KB instead. That’s what the approximate_size() function does.)
+
+
Look at the bottom of the script, and you’ll see two calls to print(approximate_size(arguments)). These are function calls — first calling the approximate_size() function and passing a number of arguments, then taking the return value and passing it straight on to the print() function. The print() function is built-in; you’ll never see an explicit declaration of it. You can just use it, anytime, anywhere. (There are lots of built-in functions, and lots more functions that are separated into modules. Patience, grasshopper.)
+
+
So why does running the script on the command line give you the same output every time? We’ll get to that. First, let’s look at that approximate_size() function.
+
Python has functions like most other languages, but it does not have separate header files like C++ or interface/implementation sections like Pascal. When you need a function, just declare it, like this:
def approximate_size(size, a_kilobyte_is_1024_bytes=True):
@@ -256,6 +262,7 @@ if __name__ == "__main__":
c:\home\diveintopython3> c:\python30\python.exe humansize.py
1.0 TB
931.3 GiB
+And that’s your first Python program!
docstring from a great docstring.