Python basics - 2nd slide deck

Module 0

Let’s Do Digital Team

OK, so you can create variables, change them and compare them. What next?

Control flow - if statements

if_statement.py
patient_allergy = "amoxicillin"

if patient_allergy == "amoxicillin":
    allergy_group = "penicillins"
elif patient_allergy == "tazocin":
    allergy_group = "penicillins"
else:
    allergy_group = "other antibiotics"

print("Patient is allergic to", allergy_group)

Output

    Patient is allergic to penicillins

Control flow - if statements

if_statement.py
patient_allergy = "tazocin"

if patient_allergy == "amoxicillin":
    allergy_group = "penicillins"
elif patient_allergy == "tazocin":
    allergy_group = "penicillins"
else:
    allergy_group = "other antibiotics"

print("Patient is allergic to", allergy_group)

Output

    Patient is allergic to penicillins

Control flow - if statements

if_statement.py
patient_allergy = "clarithromycin"

if patient_allergy == "amoxicillin":
    allergy_group = "penicillins"
elif patient_allergy == "tazocin":
    allergy_group = "penicillins"
else:
    allergy_group = "other antibiotics"

print("Patient is allergic to", allergy_group)

Output

    Patient is allergic to other antibiotics

Control flow - for loops

for_loop.py
list_of_numbers = [1, 2, 3, 4, 5]

for number in list_of_numbers:
    print(number)

Output

    1
    2
    3
    4
    5

Control flow - for loops and breaks

  • You can also stop the loop when a condition is met.
for_loop_break.py
list_of_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in list_of_numbers:
    print(number)
    if number == 6:
        print("End")
        break

Output

    1
    2
    3
    4
    5
    6
    End

Control flow - while loops

while_loop.py
count = 1

while count < 6:
    print(count)
    count = count + 1

Output

    1
    2
    3
    4
    5

Colons, look out for the colons

  • All if, while and for statements must end with a colon
colon.py
count = 1

while count <= 5:
    print(count)
    count += 1

Colons

  • The same goes for functions.
more_colons.py
def i_am_a_function():
    print("hello")
    return

Indexing lists

  • Indexing in python starts at 0.
indexing.py
a_list = ["a", "b", "c"]

print(a_list[0])

Output

    a

Dictionary lookup

  • You can get a value from a dictionary by providing it with the key you want to look up.
indexing.py
a_dictionary = {"key 1": 11, "key 2": 76}

print(a_dictionary["key 1"])

Output

    11

Functions

  • Reusable organised code. DRY
  • A function:
    1. Takes zero or more input values,
    2. Undertakes some process, and
    3. Returns zero or more output values.

Anatomy of a function

functions.py

def name_of_function(argument_1, argument_2):

    print(argument_1 + argument_2)

    return "Finished"

Anatomy of a function

functions.py

def name_of_function(argument_1, argument_2):

    print(argument_1 + argument_2)

    return "Finished"

Anatomy of a function

functions.py

def name_of_function(argument_1, argument_2):

    print(argument_1 + argument_2)

    return "Finished"

Inbuilt functions

  • There are some functions that are already built into python, eg
built_in_functions.py
print("hello world")
print(len([0,1,2,3,4]))

Output

    hello world
    5

You got all that?

  • Now it is your turn.
  • Time for some hands on coding in Lesson 3.
  • You need to change directory
$ cd ../lesson_3
  • Then run the first exercise:
$ python exercise_1.py

Computers are pedantic!

  • Remember that computers think in True and False, e.g. 1 and 0s. They are literal thinkers.
  • Even one character being out of place can break your code.
  • So watch out for that unpaired quotation mark, look out for that space that should not be there, and make sure you match your indentations to your if statements.
  • Now go have some fun in your tutor groups with hands-on coding and debugging.
  • Come back to these slides to remind yourself of key concepts - https://letsdodigital.org/learn/learn-python/module-0/