(Home)


SUPAPYT - Introduction to

python logo


Welcome to this introductory course in the Python programming language!

This course will take you through the basics of Python and hopefully demonstrate it to be a powerful, intuitive, and generally useful means of programming.

It's intended to be suitable for novice programmers while still being useful for those with previous experience of other languages, or even of python itself.

This course is presented in the form of a Jupyter notebook, which gives full access to the interactive python interpreter. We'll actually be running real python code as we go through the lectures.

All the material is available online, in static form. You can also download the source. If you then follow these Jupyter installation instructions you can use the interactive form of the notes. I encourage you to do so, and, if you have a laptop, follow the material interactively during the lectures, play about, and ask questions as we go.

Schedule

There will be 4 lectures and 2 hands-on sessions:

The lectures are webcast and recordings are accessible through MySUPA.

The lab sessions allow you to work through examples, experiment with Python, and discuss any queries with me and fellow students.

You can also get help setting up python and Jupyter on your laptop/desktop.

Assessment

Contents

Introduction

What is Python?

What does Object Oriented mean?

What are the benefits of python?

Which python?

The Python Interpreter

In a jupyter notebook, an interpreter cell looks like this:

Simple Calculations

All the standard operators are available:

+    add

-    subtract

*    multiply

/    divide

//   divide and round down

%    remainder

**   exponentiate

As said, the interactive prompt is handy as a simple calculator:

Assignment to variables

This allows you to create named variables with given values rather than just using fixed values.

You then use the name of the variable in place of the value in calculations and can assign new variables from the values of those calculations.

This is particularly relevant when writing functions for repeated operations where the input values are provided by the user.

Functions are defined with the def keyword and the return value with the return keyword:

Then call the function similarly to print by passing arguments in brackets and assign a variable from its return value:

More on functions later.


harry potter python

Basic Data Types

There're a few basic types of object in python (and most programming languages) on top of which arbitrarily complex algorithms can be built.

Numbers

Python can interpret integers in any basis. There're a few standard ones with special syntax:

Numbers with a fractional part are known as "floating point" numbers, or floats.

Python also has a builtin complex number class.

Booleans

Other comparison operations include:

More info here.

Strings

None

None is the null type, and is useful for comparisons, as a default value for something that may be assigned later, a return value in the event of a failure, or various other applications.

Casting between types

Flow Control

from __future__ import braces

and

import this

Functions


xkcd python

Sequences

Lists

Tuples

Dictionaries

d = {key1 : element1, key2 : element2, ...}.

Sets

Looping

while Statements

for Loops

for element in sequence :

Eg:

Introspection

dir

>>> dir()

['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

help

Type checking

Classes

The Empty Class

A Basic Class

Inheritance

Using __slots__ to specify attribute names

Class attributes


ancient code

Modules

Importing Modules

Writing Your Own Modules

Importing or Executing as Main

Packages

Files, Input & Output

Reading & Writing Files

The with Statement

File Parsing

Data Persistency

Pickling

Commandline Arguments

python my_script.py arg1 arg2 arg3 ...

new pet

Exceptions

More Useful Builtin Functionality

Lambda Methods

Sequence Manipulation

More Ways to Iterate.

OS Interface

A Few Tips

Further Reading

The End

Happy Pythonising!

(Home)