Python 3 latest code crumbs
Runnable Python module
Feb 4, 2022 by erik
When we import a module, its name (stored in the variable __name__
) is equal to the module name. When a script is executed, its name is always set to the__main__
string. Hence, if we check for this name with `if...
Python modules
Jan 25, 2022 by erik
You can import a Python module by using the import
keyword. Module imports are often placed at the top of a script, but they don't have to be. E.g., sometimes you want to dynamically decide if you need to import...
Python Dictionary View Objects
Jan 21, 2022 by erik
Some built-in Python dictionary methods return a view object, offering a window on your dictionary's keys and values. There's an important concept you need to understand: values in a view object change as the content of the dictionary changes. In...
Default values and named parameters
Jan 16, 2022 by erik
A compelling Python feature is the ability to provide default values for the parameters. Because our parameters have a default value, you don't have to fill them in. If you don't, the default is used. If you do, you override...
While loop
Jan 16, 2022 by erik
While the for-loop in Python is a bit hard to understand, the while loop is actually much simpler! In this example, we see an expression that follows the while statement: i <= 4
. As long as this expression evaluates to...
Python for loop and lists
Jan 16, 2022 by erik
A Python list can contain zero or more objects. It's a frequently used data type in Python programming. In other programming languages, lists are often called arrays. Python does not have arrays, though, only lists. We can loop, or iterate,...