Python Video Course Notes

10 Dec 2019

Python Course

LinkedIn Video Course Link


my_useful_variable = 0

global variables vs local variables

within a function, that function gets its own copy of that variable and by default does not affect the global variable

myvariable = 0

def main():
	global myvariable
	myvariable = "Hello"

Use of the global keyword means it will not alter the global variable value.

Remove a variable

using the del command we can remove a variable from that point forward.

myvariable = "Hello"

del myvariable



def main():
	print("I am a function")

main() # runs the main function
print(main()) # the function will run then the outer print will evaluate to None
print(main) # prints the value of the function

inputting into a function using arguments (args)

def main_args(arg1, arg2):
    print(arg1, " ", arg2)

main_args(10, 20)

setting a default value to a arg

def main_args(arg1, arg2=20)

returning using a function

def main_return(x):
    return x*x*x

main_return(3) # No value printed
print(main_return(3)) # Printing the value we can see the change

calling a function with specific args

def main_args(arg1, arg2):
    print(arg1, " ", arg2)

main_args(arg2=10, arg1=20)

variable amount of args

def multi_add(*args):

use of the * allows for a variable number of args to be inputted


IF and Conditional Statements

IF, ELIF and ELSE statement

if (x < y):
	print("X is less than Y")
elif (x == y):
	print("X is equal to Y")
	print("X is greater than Y")

Conditional statements

st = "x is less than y" if (x < y) else "x is greater than y"



while ( x < 5 ):
	x = x+1


for x in range(5, 10):

for loops can work over things not just numbers, for example an array.

days = ["mon", "tue", "wed", "thur", "fri", "sat", "sun"]
for d in days:


retreive an index ID for items

days = ["mon", "tue", "wed", "thur", "fri", "sat", "sun"]
for i,d in enumerate(days):
	print(i, d)

Break statement

the break statement causes the loop to break if a condition has been met.

for x in range(5, 10):
	if(x == 7): break

Continue statement

the continue statement skips the rest of the loop for that particular iteration

for x in range(5, 10):
	if(x % 2 == 0): continue


like an object constructor or a blueprint for creating objects, the init (initialion) function is always executed with a class and is used to assign object properties or other operations that the class needs.

class my_class:
	x = 5

The example below sets up a class called Person with 2 properties which are used and can be called seperately.

class Person:
	def __init__(self, name, age): = name
		self.age = age

person1 = Person("John", 36)


Date, Time and Datetime

using the date, time and datetime modules

from datetime import date
from datetime import time
from datetime import datetime

printing todays date

today =
print("Today's date is", today)

individual components


today =
print("The current date and time is", today)

current time


Formatting time output

now =

print(now.strftime("The current year is: %Y"))
  • Year
    • %y
    • %Y
  • Weekday
    • %a
    • %A
  • Month
    • %b
    • %B
  • Day of month
    • %d
  • locale’s date and time
    • %c
  • locale’s date
    • %x
  • locale’s time
    • %X

Formatting Time

  • 12/24 Hour
    • %I
    • %H
  • Minute
    • %M
  • Second
    • %S
  • Locale’s AM/PM
    • %p

Time Delta

A span of time

from datetime import timedelta

now =

print("One year from now will be:" + str(now + timedelta(days=365)))


import calendar

creating a plain text calendar

c = calendar.TextCalendar(calendar.MONDAY)
st = c.formatmonth(2019, 1)

creating HTML calendar output

hc = calendar.HTMLCalendar(calendar.MONDAY)
st = hc.formatmonth(2019, 1)


f = open("textfile.txt", "w+")

opens a file called textfile with write (overwrite) access, the + means to create the file if it doesn’t exist.

f.write("This is a line\n")

this will write to that file, overwritting any content then close the file. To append data to the file instead of opening with write access we open with append access

f = open("textfile.txt", "a")

read the file contents, use of a IF statement checks if the mode is read otherwise do not perform the action.

f = open("textfile.txt", "r")

if(f.mode == 'r'):

read per line


Working with OS path

Required modules are:

import os
from os import path

Using the OS module we can:

  • print the os name:
print("The operating system name is " +
  • check if an item exists:
print("Does the item exist: " + str(path.exists("")))
  • check if the item is a file:
print("Is this item a file? " + str(path.isfile("")))
  • check if the item is a directory:
print("Is this item a directory? " + str(path.isdir("")))
  • rename a file
  • find the real path:
print("The real path of this file is: " + str(path.realpath("")))
  • split the realpath and file name (printed as a tuple):
print("Split the path and file name\n" + str(path.split(path.realpath(""))))

System Shell Methods

To use the shell utilities we need to import shutil.

import shutil

Using shutil to copy a file, we can use the previous path.exists and path.realpath to check if the file exists, store its path and add a .bak to the end of the path. Then using the shutil.copy() we can copy its content into another file.

if (path.exists("")):
    src = path.realpath("")
    dst = src + ".bak"

    shutil.copy(src, dst)

The above will create another file called

To move over the metadata of the file we need to use the shutil.copystat() command after the inital copy.

Archive Files

We can archive using the make_archive module, which will allow us to create any type of archive file (zip, tar etc).

from shutil import make_archive

We create two variables to store the parts of the tuple generated by the path.split() command.

root_dir, tail = path.split(src)

The root_dir will contain the path and the tail will contain the file name, we can then use the shutil.make_archive().

shutil.make_archive("archiveName", "zip", root_dir)

Web Data

We can read website data using the urllib request module.

import urllib.request

To recieve a response object we can use the request url open and the getcode function.

webUrl = urllib.request.urlopen("")
print("Response Code: " + str(webUrl.getcode()))

We can read and print the content of the website by using the read function.

data =

Working with JSON data

We need to import the JSON module, building upon our previous reading web data script.

This will import the json module and if the website reports 200 it will pass the data to the function called printResults.

The json.loads() function, this will load the string data into a python object.

import json
import urllib.request

def printResults(data):
    theJSON = json.loads(data)

urlData = ""
webUrl = urllib.request.urlopen(urlData)

if(webUrl.getcode() == 200):
    data =
	print("Error: Response Code: " + str(webUrl.getcode()) + "\n")

We can then access the object like a dictionary.

def printResults(data):
    theJSON = json.loads(data)

    if "title" in theJSON["metadata"]:

Using this method we can keep expanding the data

# the number of earthquakes
count = theJSON["metadata"]["count"]
print(str(count) + " events recorded.")

# loop through the location names
for i in theJSON["features"]:

Using the data to filter. This example filters for quakes with a mag above 4.0

# loop through the location names
    for i in theJSON["features"]:
        if(i["properties"]["mag"] >= 4.0):
            # %2.1f string format to 1 decimal place
            print("%2.1f" % i["properties"]["mag"], i["properties"]["place"])


Back to Top