Python Programming Basics

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Faculty of Computer Science

University of Sunderland

Python Programming Basics


Introduction
Python is a modern language which is not only easy to learn and is object orientated, but it has
become the language of choice for AI, Data Science and Machine learning. There is an enormous
community and libraries for everything (see https://pypi.python.org/pypi ).

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:

 Overview of Python http://www.tutorialspoint.com/python/python_quick_guide.htm


 Good starting course https://www.tutorialspoint.com/python/python_overview.htm
 Python Online Interactive http://www.learnpython.org/en/Hello%2C_World%21
 Python Hard Way http://learnpythonthehardway.org/book/ex20.html
 Python http://anh.cs.luc.edu/331/notes/PythonBasics.pdf
 Python http://learnpythonthehardway.org/book/ex20.html

The 2 courses in bold will be particularly useful if you are new to this coding environment.

Installation Python
For PC and Mac

Download Anaconda https://www.anaconda.com/download


Launch Anaconda | Click Jupiter Notebook
Faculty of Computer Science
University of Sunderland

Then click  New | Python 3

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

Enter the code below and then run it Like so:

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 <=

Basic control structures in python:


IF …. THEN
WHILE
FOR

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy