Python Getting Started

A quick introduction to Python with some key areas explained at a high-level

Python Setup on Windows

First go to the Downloads section of python.org and download and install Python 3.x.x which will install Python onto your Windows environment and setup file associations.

Once installed, open Terminal and check the version

PS ~> python --version
Python 3.11.3
PS ~>

Python Interpreter

To open the interpreter, just run python in a Terminal.

Python language basics

  • Python is case sensitive
  • The end of a line marks the end of the statement; no need for semicolon
  • Comments start with #

Python source code

  • Source files use the .py extension
  • These are called modules
  • To run a module as-is use python module.py
  • To run a module with an argument use python module.py argumentvalue
  • A module can be run directly or called by some other module

An example module

#!/usr/bin/env python

# import modules used here -- sys is a very standard one
import sys

# Gather our code in a main() function
def main():
    print('Hello there', sys.argv[1])
    # Command line args are in sys.argv[1], sys.argv[2] ...
    # sys.argv[0] is the script name itself and can be ignored

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
    main()

When a module runs as-is or is called by another module

This code from the example above. This is a common Python idiom used to ensure that the main code block of a module is executed only when the module is run as a standalone program, rather than when it is imported as a module into another program.

The __name__ variable in Python is a special variable that gets assigned a value depending on how the code is being executed. When a Python module is executed as the main program, the __name__ variable is assigned the value __main__. On the other hand, when the module is imported into another program, __name__ is set to the name of the module.

So, the if __name__ == '__main__': statement checks whether the current module is being executed as the main program or not. If it is being executed as the main program, then the main() function is called. If the module is being imported into another program, the main() function is not called.

if __name__ == '__main__':
    main()

Indentation

  • White space indentation affects what the line means
  • A block will all use the same indentation
  • No curly braces are used {}
  • DO NOT USE tabs for indentation as the tab could be interpretted as a value
  • DO USE spaces, the official Python style guide says 4 spaces
  • If you use VSC the tab size can be configured; if you install the Python extension it will do it for you

Variable Names

  • Use meaningful names for variables.
    • name if singular
    • names if a list
    • tuples if a list of tuples

Tuples are just immutable lists; meaning the values cannot be changed

For example my_tuple = ("apple", "banana", "cherry")

  • Names should be lower case and use underscore
  • If camel case is used already when editing a module then use that instead

For example, variable_name

Camel case is variableName

Note the uppercase for the second word

Module namespaces

If you have a definition of def foo() in a module called example1.py you can call that by using module1.foo. This means that it’s proably unique given the module name. So you can use def foo() in multiple other module files as it will be called by the namesspace of {module}.{def}.

References

Python

Google’s Python Class

Last modified July 21, 2024: update (e2ae86c)