Introduction to Coding with Python

Introduction to Coding with Python#

Prerequisites#

What is Python#

Python is a high-level programming language. This means it is close to English, and a lot of the work being done to run the code is hidden from you as the programmer. This makes it very easy to use compared to some other languages.

Syntax#

  • Colours. The majority of Python IDEs automatically highlight different kinds of Python objects for readability. Keywords, words that cannot be used as variable names (such as True, False, import, not, etc.), are highlighted in certain colours, while variable names and variable types are highlighted differently, and so on. This helps easily identify spelling errors and signpost what the code does. The specific colours depends on the IDE you use. Some of the different highlighting is shown below (in no usable order). Throughout the lessons you will learn what each word written below means and how they can be used.

    False, True
    if, for, while, with, 
    and, or, not 
    import, def
    print(), range(), enumerate(), zip()
    float(), str(), int()
    variable_1 = 23.5
    variable_2 = "this string"
    variable_3 = [1, 2, 3]
    

    Your IDE might highlight differently.

  • Python is case sensitive, with the default being lowercase. Built-in Python functions are given in lowercase. For example, writing PRINT("hello") instead of print("hello") would return an error, as the function print() is case-sensitive, and must be given in lowercase.

  • Python uses new lines to complete a command. Two print statements on two separate lines will execute one after another.

  • Indented text is an important distinction. When we move onto conditional statements and loops, the indentation defines which code belongs to which statement or loop. Therefore it is important to ensure your text is left-aligned. If you indent where Python is not expecting it, or do not indent where Python is expecting it, you will get an error message. The standard indentation is achieved using the <tab> key, equivalent to four spaces.

  • Speech marks are used to define strings " " and ' '. Double and single speech marks are equivalent. For readability, choose one and use them consistently.

Comments#

Python will attempt to execute any written text in the file. The exception to this are comments, which is used to provide context and description for any code that comes immediately after it. You indicate that a line is a comment using a hash #.

# This is a comment line

# This is a print statement
print("hello world")

You can also add comments at the end of a line. Use these sparingly, but they are very useful to define units.

pressure = 12 # Pa

If you need to give a little more description, you can use a block comment, defined using speech marks above and below the comment.

"""
Calculate the mean. 
Calculate the standard deviation.
Output a Gaussian curve.
"""

File management#

When managing your Python files, here is some general advice:

  • Python files are plain text files with the extension .py on the end of the name. If you use Jupyter Notebook, you might also see .ipynb files, which can also run Python cells.

  • When naming your files, keep the names short but descriptive. Try not to name multiple files the same thing.

  • Group your Python files in sub-directories (folders on your computer) to keep track of them better.

  • Regularly save your work.

  • Version control is your friend