Python Programming Basics
Python Programming Basics
Python Programming Basics
University of Sunderland
This document is a basic guide to Python for those who have learned programming before. One
feature that can cause problems in the early stages is that indentation is used to indicate code
(rather than semicolons in C#).
So if you get an error it may be an extra space or tab is causing a problem. Here are some useful
links for python:
The 2 courses in bold will be particularly useful if you are new to this coding environment.
Installation Python
For PC and Mac
This will give you a new environment to start coding in python. We will be using this environment for
the time being.
Python Coding
Next we are going to look at fragments of working code which will introduce you quickly to Python
coding. There are comments between examples. Comments in code are marked by # as in the
example below, they provide useful reminders of what you are doing with the line or chunk of code
you are developing. It is good practice to include comments in your code, regardless of the language
or environment.
# Comments are after a hash
Variables are not typed and are assigned with the = sign
You will get 4 as the output when you click run. Try adding the line - print (name) and then running it
again.
Faculty of Computer Science
University of Sunderland
In Python, there is no need to set types if you set variables carefully, like so:
Type and run the code above. Try changing the values to get different outputs.
It is possible for variables to contain lists of items, separated by a comma. Enter and run the code
below:
Lists are indexed from 0, in Python. Enter and run the code below:
Arrays are available to enable complex data structures. Enter and run the code below:
It is possible to sort arrays using the sorting mechanism. Enter and run the code below:
Faculty of Computer Science
University of Sunderland
Lists can be changed. In the example below we pick out the odd number in a list (every 2nd one).
Enter and run the code below:
Tuples are like lists but contain items in round brackets and cannot be changed. Enter and run the
code below:
Dictionaries enable complex records to be created, such as marks for students. Enter and run the
code below:
Counters are short cuts to make new dictionaries. Enter and run the code below:
Sets are further short cuts to enable to find words in list-like data structures called sets. Enter and
run the code below:
Faculty of Computer Science
University of Sunderland
Functions are created in Python with def and you may see short cut functions called lambdas. Enter
and run the code below:
Operands in python:
Modulus %
Exponent **
Identity ==
Not equal !=
Greater, Less >= or <=
Below there are three snippets of code below for you to enter and run:
Faculty of Computer Science
University of Sunderland
Object orientated development is possible in Python. Enter and run the code below:
You have now looked at a broad range of coding techniques in python. Hopefully you haven’t had
too many problems with the code (but just enough to help you learn to debug python a little). It’s
time for you to try generating your own code:
Exercise 1:
Create Python code to convert temperature between Fahrenheit and Centigrade. The temperature
conversion formulae are:
Centigrade = (Farenheit-32)/1.8
Fahrenheit = (Centigrade * 1.8) + 32
Exercise 2:
Create a program to average any number of numbers. You will need to use a loop like: