Intro to Python

Python is a language which prides itself on simplicity of reading and understanding. If you'd like to read more about python go to https://en.wikipedia.org/wiki/Python_(programming_language) !

If you'd like to read further than this, or a more basic guide I would recommend: https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3

Here's an example python program to print something on the screen:

print('Hello, World!')
# This is a comment, it isn't run as code, but often they are helpful
Hello, World!

1. Arithmetic

Like every programming language, Python is a good calculator. Run the block of code below to make sure the answer is right!

1 + 1
2

The order of operations you learned in school applies, BEDMAS (brackets, exponents, division, multiplication, addition, subtraction):

8 + 6*2*3 - (15 - 13)
42

Numbers are valid Python code as are the common operators, +, /, * and -. You can write different types of numbers including integers, real numbers (floating point) and negative integers.

42 + 3.149 + -1
44.149

Since 42 is literally 42, we call these numbers literals. You are literally writing number in your Python code.

2. Variables

In python, variables are similar to C, Java and other languages - they store a certain value. Specifically, in python variables do not have a type. So, a string can also become an integer, and then even into a function! Literals are given types of string, integer, float, lists, objects, etc. But the variable's type can change.

x = 200.00
print("x =", x, "and is of type", type(x))
x = "Hello World!"
print("x =", x, "and is of type", type(x))
x = 200.0 and is of type <class 'float'>
x = Hello World! and is of type <class 'str'>

3. Exceptions in Python

Python only understands certain code. When you write something Python doesn't understand it throws an exception and tries to explain what went wrong, but it can only speak in a broken Pythonesque english. Let's see some examples by running these code blocks

gibberish
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-9bdd8ea4da78> in <module>()
----> 1 gibberish

NameError: name 'gibberish' is not defined
*adsflf_
  File "<ipython-input-7-e23e056f3c48>", line 1
    *adsflf_
            ^
SyntaxError: can use starred expression only as assignment target
print('Hello'
  File "<ipython-input-8-49c0f052bb79>", line 1
    print('Hello'
                 ^
SyntaxError: unexpected EOF while parsing
2000 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-9-d14d67f6c4a6> in <module>()
----> 1 2000 / 0

ZeroDivisionError: division by zero

Python tries to tell you where it stopped understanding, but in the above examples, each program is only 1 line long.

It also tries to show you where on the line the problem happened with caret ("^").

Finally it tells you the type of thing that went wrong, (NameError, SyntaxError, ZeroDivisionError) and a bit more information like "name 'gibberish' is not defined" or "unexpected EOF while parsing".

Unfortunately you might not find "unexpected EOF while parsing" too helpful. EOF stands for End of File, but what file? What is parsing? Python does it's best, but it does take a bit of time to develop a knack for what these messages mean. If you run into an error you don't understand please ask.

4. Strings

We have already seen a string literal in Python, "Hello, World!"

"Hello, World!"
'Hello, World!'

Text literals are surrounded by quotes. Without the quotes Hello by itself would be viewed as a variable name. You can use either double quotes (") or single quotes (') for text literals. Note that in python a character is just a string with 1 character and isn't a different data type.

Let's use strings:

print("Hello " * 5)
print("Hello" + " " + "World")
Hello Hello Hello Hello Hello 
Hello World

Strings in Python are a bit more complicated because the operations on them aren't just + and *, they have their own operations we can call on them to change them:

print("Uppercase:", "Hello world".upper())
print("Lowercase:", "Hello world".lower())
print("String formatting with %s:", "Hello %s" % "world")  # C like string formatting
s = "Hello {name}";
s= s.format(name="world")
print("String formatting with .format():", s)  # We can have template strings where parts are specific on the fly
Uppercase: HELLO WORLD
Lowercase: hello world
String formatting with %s: Hello world
String formatting with .format(): Hello world

Indexed by Zero

For better or worse, everything in Python is index by 0 like C/C++ or Java. We will see this over and over again but for now if you call format like this:

"{0} like {1}".format("I", 'Python')
'I like Python'

We would call "I" the 0th string passed into format and 'Python' the 1st.

Multi line strings

Frequently you will have a string which spans multiple lines. There are 3 ways to handle this in python:

  • Use a backslash
  • Triple quotes
  • String concatenation
print("this is \
a multiline string")
print("this is "
      "a multiline string")
print("""this is
a multiline string""")
this is a multiline string
this is a multiline string
this is
a multiline string

5. If Else

Like all languages, Python allows us to conditionally run code.

To have an if condition we need the idea of something being true and something being false. We have True or False as "boolean" values. True would represent OK where as false would represent No or Cancel.

False is False
True
True is True
True
True is false
False
true is False
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-16525f6953e7> in <module>()
----> 1 true is False

NameError: name 'true' is not defined

We can write expressions with operations too.

1 > 2
False
"Cool".startswith("C")
True
"Cool".endswith("C")
False
"oo" in "Cool"
True
42 == 1 # note the double equals sign for equality
False

In order to write an "if" statement we need code that spans multiple lines

if condition:
    print("Condition is True")
else:
    print("Condition is False")

Some things to notice. The if condition ends in a colon (":"). In Python blocks of code are indicated with a colon (":") and are grouped by white space. Notice the else also ends with a colon (":"), "else:". Let's try changing the condition and see what happens.

condition = 1 > 2
if condition:
    print("Condition is True")
else:
    print("Condition is False")
Condition is False

About that white space, consider the following code:

if condition:
    print("Condition is True")
else:
    print("Condition is False")
print("Condition is True or False, either way this is outputted")

Since the last print statement isn't indented it gets run after the if block or the else block.

You can play with this. Try indenting the last print statement below and see what happens.

condition = True
if condition:
    print("Condition is True")
else:
    print("Condition is False")
print("Condition is True or False, either way this is outputted")
Condition is True
Condition is True or False, either way this is outputted

You can also use "and", "or", "not" to combine conditions (No ugly && and || here):

True and True is True
True and False is False
False and True is False
False and False is False
not True is False

6. Lists

So far we have numbers, strings, and conditional if statements. Now for our first container - a list.

A list in Python is just like an array or a linked list. They have a defined order and you can add to it or remove from it. Let's take a look at some simple lists.

# The empty list
[]
[]
["bread", "butter", "jam"]
['bread', 'butter', 'jam']
[1,2,3]
[1, 2, 3]

List literals are all about square brackets ("[ ]") and commas (","). You can create a list of literals by wrapping them in square brackets and separating them with commas.

As python doesn't care too much about data types, you can even mix different types of things into the same list; numbers, strings, booleans (unlike arrays)

[True, 0, "String"]
[True, 0, 'String']

We can put variables into a list and set a variable to a list.

wikipedia = "wikipedia.org"
wiki_sites = ["wikidata.org", wikipedia]
print(wiki_sites)
['wikidata.org', 'wikipedia.org']

Like strings, lists have their own operations. "append" is an interesting one. "append" lets you add an item to the end of a list.

wiki_sites = ["wikidata.org", "wikipedia.org"]
wiki_sites.append("wikisource.org")
print(wiki_sites)
['wikidata.org', 'wikipedia.org', 'wikisource.org']
wiki_sites[0]
'wikidata.org'

There is that 0 indexing again. The first element of the list is given index value 0.

print("These are some of the wikimedia sites: {0}, here is {1}, {2}".format(wiki_sites[0], wiki_sites[1], wiki_sites[2]))
These are some of the wikimedia sites: wikidata.org, here is wikipedia.org, wikisource.org

7. Loops

Indexes are useful, but lists really shine when you start looping.

Loops let you do something for each item in a list. They look like this:

for item in my_list:
    print(item)  # Do any action per item in the list

"for" and "in" are required. "my_list" can be any variable or literal which is like a list. "item" is the name you want to give each item of the list in the indented block as you iterate through. We call each step where item has a new value an iteration.

for num in [1, 2, 3]:
    print(num)
1
2
3

In python, we don't use constructs like for ( int i = 0; i < maximum; i++ ) which are just confusing, rather we create a "range" of integers from 0 to maximum and loop over that:

maximum = 10
print("This is the range object:", range(0, maximum))
print("This is what it creates when it's used in a loop or other functions:", list(range(0, maximum)))
This is the range object: range(0, 10)
This is what it creates when it's used in a loop or other functions: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

And then we can use that with a loop to print a list of squares.

for number in (i*2 for i in range(1, 10)):
    print("{0} squared is {1}".format(number, number*number))
2 squared is 4
4 squared is 16
6 squared is 36
8 squared is 64
10 squared is 100
12 squared is 144
14 squared is 196
16 squared is 256
18 squared is 324

8. Dictionaries

We have come a long way! Just one more section. Dictionaries are another container like lists, but instead of being index by a number like 0 or 1 it is indexed by a key which can be almost anything. The name comes from being able to use it to represent a dictionary.

List literals use square brackets ("[]") but dictionaries use braces ("{}").

{"wikipedia.org": "The free encyclopedia", 
 "wikisource.org": "The free library"}

In a dictionary the key comes first followed by a colon (":") than the value then a comma (",") then another key and so on. This is one situation where a colon doesn't start a block.

wiki_sites = {
    "wikipedia.org": "The free encyclopedia", 
    "wikisource.org": "The free library"
}
wiki_sites["wikipedia.org"]
'The free encyclopedia'

We can loop over the keys in a dictionary to list all of our definitions...

for key in wiki_sites:
    print('The Key is "{0}" and the value is "{1}"'.format(key, wiki_sites[key]))
The Key is "wikisource.org" and the value is "The free library"
The Key is "wikipedia.org" and the value is "The free encyclopedia"

In fact, dictionaries can contain any type of values. Hence, you can have a list as the value of a dictionary:

wiki_language_sites = {
    "wikipedia.org": ["en.wikipedia.org", "ml.wikipedia.org", "ta.wikipedia.org"], 
    "wikisource.org": ["en.wikisource.org", "ml.wikisource.org", "ta.wikisource.org"]
}
wiki_language_sites["wikipedia.org"]
['en.wikipedia.org', 'ml.wikipedia.org', 'ta.wikipedia.org']

And dictionaries can contain other dictionaries too!

9. Functions

To avoid code repetition and to break code into smaller intelligible blocks, functions are useful. A function takes in certain variables (arguments) and gives out a return value. The arguments and return value do not need to define a data type again, as python isn't too worried about data types.

A function is defined using the def keyword:

def <function name>(<arg1>, <arg2>, <arg3>, ..., <argN>):
    <function body>
    return <value to return>
def sum2(x1, x2):
    return x1 + x2

print("2 + 3 =", sum2(2, 3))
print('"a" + "b" =', sum2("a", "b"))
print('[1] + [2,3] =', sum2([1], [2, 3]))
2 + 3 = 5
"a" + "b" = ab
[1] + [2,3] = [1, 2, 3]

Because there's no data type for the arguments and return type, we can give strings, ints, floats, lists, etc. Anything that supports the + operator in our example. But if the arguments given cannot be operated on by + it gives an error. And it is the job of the developer of the function to ensure that the arguments given are sane:

def sum2(x1, x2):
    return x1 + x2

print('1 + "s" =', sum2(1, "s"))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-45-8cc7b8e2b21d> in <module>()
      2     return x1 + x2
      3 
----> 4 print('1 + "s" =', sum2(1, "s"))

<ipython-input-45-8cc7b8e2b21d> in sum2(x1, x2)
      1 def sum2(x1, x2):
----> 2     return x1 + x2
      3 
      4 print('1 + "s" =', sum2(1, "s"))

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python also supports assigning values using the argument name. When using argument names (also called keyword arguments - kwargs) the order of the arguments do not matter as the name of the argument is used. This gives flexibility, as you don't have to remember which argument comes first and which comes second! Let's see it in action:

def difference2(x1, x2):
    return x1 - x2

print("2 - 3 =", difference2(2, 3))
print("2 - 3 =", difference2(x1=2, x2=3))
print("2 - 3 =", difference2(x2=3, x1=2))  # We give x2 before x1
2 - 3 = -1
2 - 3 = -1
2 - 3 = -1

Not all functions need to return a value. And by default if a return value is not given, it returns None.

Functions can also have default values for arguments:

def power(base, exponent):
    exponent = 2
    return base ** exponent

print('5 power -1 is', power(5, -1))
print('power(5) uses exponent 2 by default:', power(5))
5 power -1 is 25
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-ac887f35d958> in <module>()
      4 
      5 print('5 power -1 is', power(5, -1))
----> 6 print('power(5) uses exponent 2 by default:', power(5))

TypeError: power() missing 1 required positional argument: 'exponent'

10. Classes

Python also has Object Oriented Programming (OOP) in it's structure. A class is a template which holds class variables and functions (methods) and operates on the class itself.

To use a class, objects which conform to the class template need to be created:

class WebSite:
    def __init__(self):
        self.url = ""
        self.description = ""

w1 = WebSite()  # w1 is an object of class WebSite
print("Type of w1:", type(w1))
w1.description = "The free encyclopedia"
w1.url = "wikipedia.org"
Type of w1: <class '__main__.WebSite'>

The __init__() function is the class constructor and can only return None. Other than the variables created in the class, python can dynamically add more variables in the class:

w1.visits = 1000
w1.subsites = ["en.wikipedia.org", "ml.wikipedia.org", "ta.wikipedia.org"]

Classes can also define functions which can be used to perform functions using the object variables.

Object methods start with the keyword self normally. When w1.function() is used, the object before the period (w1) is passed to function()'s first argument. Hence, function() would be defined as def function(self): where self is a reference to the object itself.

Here's an example:

class WebSite:
    def __init__(self, _description="", _url=""):
        self.url = _url
        self.description = _description

    def subsite(obj, subname):
        return subname + "." + obj.url

w1 = WebSite("The free encyclopedia", "wikipedia.org")  # w1 is an object of class WebSite
w1.subsites = [w1.subsite('en'), w1.subsite('ml'), w1.subsite('ta')]
print(w1.subsites)
['en.wikipedia.org', 'ml.wikipedia.org', 'ta.wikipedia.org']

11. Exercises

Exercise 1 - Boolean values

Below change the values of the three variables to make the entire "if condition" true.

# Edit the values of these 3 variables
boolean_literal = True
number = 12
string_literal = "I like to count cows before bed."

# Leave this code the same please
if number > 10 and boolean_literal and "cows" in string_literal:
    print("Success!")
else:
    print("Try again!")
Success!

Exercise 2 - Create a WebSite object for each item in the wiki_sites dictionary

Loop over every (key, value) pair in the dictionary wiki_sites and create a list of WebSite objects using the WebSite class. The key of the dictionary should be stored in the url of the object, while the value of the dictionary should be the description.

# Leave the definitions of the class and dictionary as is
class WebSite:
    def __init__(self, _description="", _url=""):
        self.url = _url
        self.description = _description

wiki_sites = {
    "wikipedia.org": "The free encyclopedia", 
    "wikisource.org": "The free library"
}

# Write below. Loop over every item in wiki_sites and create a list of WebSite objects.

obj = []
for item in wiki_sites:
    w1 = WebSite()
    w1.url = item
    w1.description = wiki_sites[item]
    obj.append(w1)

for item in obj:
    print (item.url)
    
wikisource.org
wikipedia.org

Exercise 3 - Create a class to store a mediawiki page

Create a class which can store a mediawiki page (Has content and name of page) and create functions to:

  • Check if the page is empty
  • Generate the URL of the page using a base url (Example: wikipedia.org/wiki/<page name>)

    Below you will find some example code to test your class.

class WikiPage:
    def __init__(self, name, content):
        self.name = name
        self.content = content
        self.url = ""

    def url(self):
            return "wikipedia.org/wiki/" + self.name
    
    def empty(self):
        if (self.content==""):
            return True
        else:
            return False
        
# The following should work:
page = WikiPage(name="MyPage", content="Hello there ! This is a small example page.")
print("The name of the page is:", page.name)
print("The page has the folowing content:")
print(page.content)
print("The url of the page is:", page.url())
if page.empty():
    print("The page is empty")
else:
    print("The page is not empty")
The name of the page is: MyPage
The page has the folowing content:
Hello there ! This is a small example page.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-73-639f78c4233c> in <module>()
     19 print("The page has the folowing content:")
     20 print(page.content)
---> 21 print("The url of the page is:", page.url())
     22 if page.empty():
     23     print("The page is empty")

TypeError: 'str' object is not callable