Content Summary: Literate Programming in Google Colab
This lesson introduces the concept of literate programming and explains how it works within the Google Colab environment. Literate programming is a style of software development with a long history, dating back to the 1960s. It was created to address the problem of increasingly large and complex programs becoming difficult to understand, by combining the program's code with its documentation in an integrated way.
The traditional methods of documentation had significant drawbacks. Writing documentation in a separate file often meant it would become outdated or forgotten. On the other hand, placing too many comments directly within the code can make the code itself difficult to read and follow, distracting from the program's logic. Literate programming aims to solve this by keeping code and documentation together, yet cleanly separated.
Google Colab notebooks are a modern implementation of this idea. They allow you to structure your work using two distinct types of cells: Code Blocks, which contain executable Python code, and Text Blocks, which contain formatted text, explanations, and notes. This allows you to build a program piece by piece, interspersing descriptive text between functional blocks of code, resulting in a program that is well-structured and easy for others (and your future self) to understand.
A defining feature of this environment is that variables are shared between all code blocks within a single, active session. This means you can define a variable in one code block and then use or modify it in another block further down in the notebook. This allows you to build your program incrementally, with each block representing a logical step in your process.
This is demonstrated with a simple example. A variable greeting
is created and assigned a value in one code block. Then, in a completely separate code block that follows, the program is able to access and print the value of that same variable, showing that its state persists across the blocks.
# First code block
greeting = "hello world"
print(greeting)
# Second code block, executed later
print(greeting)
It is crucial to understand how the underlying system, the Python "instance," works. Your code is not running on your own computer but on a temporary virtual machine on Google's servers. To manage resources, Google will automatically stop your instance after a period of inactivity. While your code in the notebook is always saved, the state of the running program, including the values of all your variables, is lost when the session terminates.
The consequence of a terminated session is that if you try to run a code block that depends on a variable defined in a previous block, you will get an error because that variable no longer exists in the new, fresh session. To fix this, you must re-run your notebook from the beginning to re-establish the state of all your variables. Colab provides a convenient "Run all" function in the menu to execute all code blocks in order from top to bottom.
In summary, working with multiple code blocks is a powerful feature for creating clear, documented programs. You simply need to be aware that the variables only persist within a single active session. If your session is terminated by Google or by closing your browser, you will need to run your code again from the start to continue where you left off.
Thanks for reading and watching! Leave any questions or comments here and I will answer them! — Andy