A gentle intro
to coding

Continued!

view these slides here: 
goo.gl/KWWMT

welcome back!

Introductions

Because I always forget names too

MAËlys

Serena

MAdo

Quinn

territorial acknowledgement

last week i spoke of how we were on  the traditional unceded territory of the Algonquin Anishnaabeg people

I want to continue the discussion this week by talking about history

for thousands of years indigenous people lived on these lands

rich histories largely ignored by our school's textbooks that focus on after colonization by europeans

while saying next to nothing of the brutality of colonialism

with colonialism, indigenous peoples' LAnds were taken, and there was a concerted effort to destroy their languages, religions, and cultures

The indian act made it illegal for indigenous peoples to raise funds for, or hire, a lawyer to address land claims

indigenous celebrations were outlawed under the indian act until 1951

indigenous people in canada did not get the vote until 1960

children were forcibly ripped from their parents and sent to residential schools

where they were beaten for speaking their own language

“When ... the child lives with its parents, who are savages, and though he may learn to read and write, his habits and training mode of thought are Indian. ... Indian children should be withdrawn as much as possible from the parental influence ... put them in central training industrial schools where they will acquire the habits and modes of thought of white men."

- Sir John A MacDonald

residential schools did not stop operating until 1996

between 1981 and 2012 over 1,100 indigenous women were murdered or went missing

some estimates put that number at over 4,000

this history isn't just in the past

IT CONTINUES IN THE PRESENT

AND we live in a culture that actively opposes
Truth & reconciliation

by living on this land our histories are intertwined

and we, as settlers, have agency over what future historians will say of our timeS

if this oppression isn't to be perpetuated into the future

then we must listen to
indigenous voices

And help implement the changes asked of us

Review of
last week

last week we covered

Software development

programming languages

a bit of python programming

We learned about

functions

variables

comments

def simple_example():

    a = 20
    b = 4
    print(a / b)

variables



    a = 20
    b = 4

using A function



    a = 20
    b = 4
    print(a / b)

defining A function

def simple_example():
    # Simple math example
    a = 20
    b = 4
    print(a / b)

comment

It sounds like a lot
I know that

WE'RE GOING TO TAKE IT A
STEP AT A TIME

Please launch
visual studio code

it should open to the project from last week

and the terminal should be
open on the bottom

create a new file called "Review.py"

result = 8 / 2
print(result)

write this code in

test that this works by running the program in the terminal

python review.py
py.exe review.py

(        )

# Call the function. 
result = divide_by_two(8)
print(result)

now replace your code with this

the                             
function isn't created yet

divide_by_two()

I want you to
Create it

Try to figure it out on your own or with people around you

def multiply(a, b):
    return a * b

result = multiply(3, 4)

example from last week to refresh your memory

}

don't forget about the indentation
(TAB in a function)

def divide_by_two(value):
    return value / 2

# Call the function. 
result = divide_by_two(8)
print(result)

a solution
(there ARE many right answers)

If you're still uncomfortable
it's okay

You  don't need to be great at what we did last week to continue

I know for some people (me) asking questions is haaaaaaard

remember that anything you ask openly, someone else might have been too shy to ask

And you're helping them too

any questions?

Boolean

( true & False )

last week we covered variables

they looked like this

a = 5
b = 4
c = a / b

message = "hello there!"

they're like a container you can store data in you want to use later

we saw two kinds of information to store

a = 5
b = 4
c = a / b

message = "hello there!"

numbers

Text

there's a third type of information you can put in that I want to cover this week

they're called
booleans

and that just means it can be true or false

a = 5
b = 4
c = a / b

message = "hello there!"

is_corgi = True
has_feet = True
has_tail = False

here's what it looks like when it's used

this is pretty useful

there's a lot of situations where a simple yes (true) or no (false) is preferable to numbers or text

for instance

a variable that saves the answer to
"can I read this file?"

works better as a true/false than a number or text

comparisons

there's another situation where booleans come in

I'll show you
through example

create a new file called "comparisons.py"

print(10 > 5)

print(10 < 5)

write this code in

run the program in the terminal

python comparisons.py
py.exe comparisons.py

(          )

what do you notice about the output?

we didn't write true or false here

But we're seeing true & false in the output

This shows up AS true

print(10 > 5)

this shows up as false

print(10 < 5)

in python you can compare things

the result of those comparisons show up as true or false

the result is a boolean!

let's look at those comparisons again

print(10 > 5)

print(10 < 5)

there are two types of comparisons here

print(10 > 5)

a > B
Is a greater than b

print(10 < 5)

a < B
Is a Less than b

now it's your turn

add two more lines of code that do comparisons

Ideas

# is 4.0 less than 4?
print(4.0 < 4)

# is 2 + 2 less than 5?
print(2 + 2 < 5)

# is 2 times zero greater than 1?
print(2 * 0 > 1)

there's a third type of comparison

print(2 + 2 == 5)
print(2 + 2 == 4)

print("cat" == "dog")
print("cat" == "cat")

add the following lines to the "comparisons.py" file

run the program in the terminal

python comparisons.py
py.exe comparisons.py

(          )

this shows up as false

print(2 + 2 == 5)
print("cat" == "dog")

This shows up AS true

print(2 + 2 == 4)
print("cat" == "cat")

What does the "==" mean?

print(2 + 2 == 4)

print("cat" == "dog")
print("cat" == "cat")

a == B
Is a equal to b

i know it's a bit weird to use == to mean
"Is it equal?"

when a single = is already a thing in python

It's like that because python would be confused by the other way we use the = sign

# Store "5" in variable "a"
a = 5

# Give a True/False answer for
# whether "a" is equal to "5"
a == 5

remember: programming languages aren't very smart

we just saw a lot of stuff

here's the take-away

in python you can store numbers, text, and booleans in variables

and booleans just mean a value that is true or false

in python you can also make comparisons

so you can see whether one thing is bigger than another, or equal to another

those comparisons give a true or false answer

it's your turn Again

What do the ">=" and "<=" comparisons do?

print(1001 >= 1000)
print(1000 >= 1000)
print(999  >= 1000)

print(1001 <= 1000)
print(1000 <= 1000)
print(999  <= 1000)

you now know of The following comparisons:
>   <   >=   <=   ==

a = 5
b = 3

print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
print(a == b)

add three more comparisons to your file

Reminder of what comparisons look like:

conditionals

( IF & ELSE )

In the real world programs often have to make decisions

do I do this

...or do I do that?

your phone has to decide when to show pictures of adorable dogs on the internet

you don't want those pictures to show up while you're trying to order an uber

you want to show it when you GO to a website Of cute adorable dog pictures

decisions

in python, one way to have your program make decisions is by using conditionals

that's a big word to mean if this is true then do a thing

value = 4

if value == 4:
   print("value is 4!")
else:
   print("value is NOT 4!")

in python, we can do that with if and else

value = 4

if value == 4:
   print("value is 4!")
else:
   print("value is NOT 4!")

if value is equal to 4

...then show this

...otherwise show that

value = 4

if value == 4:
   print("value is 4!")

}

It's here too

remember how there was indentation in functions?

Indentation is there to distinguish this

value = 2

if value == 4:
   print("value is 4!")
   print("yay!")

...From this

value = 2

if value == 4:
   print("value is 4!")
print("yay!")

"yay" shows up only when value is 4

"yay" Always shows up

Let's try this out

create a new file called "Conditionals.py"

value = 4

if value > 10:
   print("value is over 10")
else:
   print("value is equal to 10 or less")

Write the following code

run the program in the terminal

python conditionals.py
py.exe conditionals.py

(          )

does anyone not see 

"value is equal to 10 or less"

in the output?

now change the conditional ">" to something else like <, <=, >=, or ==

Then change the "4" and "10" to different numbers

check that the output when you run the program is as you expect

beneath that code

create another if... else

this time, make it do this:

If the variable value is equal to the text "dog"

make the program print "Woof!"

If the text is not "dog", print "meow!"

does anyone not get the result they expected?

user input

In everything we've done so far, you haven't been able to interact with the program after it starts running

so far You run the program and You get an output immediately

But sometimes you want to ask something to the user when your program is running

In python, there is an input function

which lets the user write text that's saved to a variable

age = raw_input('Enter your age:')

print("You are this age:")
print(age)
age = input('Enter your age:')

print("You are this age:")
print(age)

Python 2

Python 3

if you want to know what version of python you are running

run the Following in the terminal

python --version
py.exe --version

(          )

if you're on a mac or Linux

and you're running python 2.x

Run this in the terminal:

python3 --version

if it now says python 3.x

then you can write things for python 3 and use this command to run programs

python3 some_file_name.py

I would prefer it if you wrote things for python 3 if possible, since it's newer

now to write some code!

create a new file called "input.py"

Write the following in the new file

name = raw_input("Enter your name:")

print("Hello")
print(name)
name = input("Enter your name:")

print("Hello")
print(name)

Python 2

Python 3

run the program in the terminal

python3 input.py
py.exe input.py

(     )

python input.py

Does anyone not see something like this in the output?

# Python 2
species = raw_input('What species are you?')

Add the following code to the file

# Python 3
species = input('What species are you?')
# Both Python 2 & 3
if species == "human":
   print("Hallo human!")
else:
   print("Hallo other being!")

run the program in the terminal

python3 input.py
py.exe input.py

(     )

python input.py

Does anyone not get the output they expect?

One last example

# Python 2
number_one = raw_input("Enter a number: ")
number_two = raw_input("Enter another number: ")

Add the following code to the file

# Python 3
number_one = input("Enter a number: ")
number_two = input("Enter another number: ")
# Both Python 2 & 3
print("The sum of both numbers:")
print(number_one + number_two)

run the program in the terminal

is the result what you expected?

All input is understood as text and not numbers

So when you do text + text thinks you want to join them

use the function int() to convert text to numbers

# Python 2
number_one = raw_input("Enter a number: ")
number_two = raw_input("Enter another number: ")
# Python 3
number_one = input("Enter a number: ")
number_two = input("Enter another number: ")
# Both Python 2 & 3
print("The sum of both numbers:")
print(int(number_one) + int(number_two))

run the program in the terminal

Does anyone get a result they don't expect?

now it's your turn

make the program ask a new question

ideas

Ask what city you live in

ask for a number

Then have the program tell you if it's in canada

Then print out that number multiplied by itself

self-directed project

we've looked at a number of different things

so here's a project you can do to combine a lot of these ideas together

make a calculator!

create a new file called "calculator.py"

you will write your code in here

requirements

the program must ask you to enter two numbers

the program must ask you to enter the name of an operation ("ADD", "SUBTRACT", "MULTIPLY" OR "DIVIDE")

The program must then perform the addition, subtraction, multiplication or division

1.

2.

3.

Example of the program running

working in groups is welcome

that's it for coding this week!

we covered a lot of ground

so if you feel uneasy with it it's okay!

learning stuff is hard and it takes time

these slides are online for you to revisit

and I'll be giving you a list of resources for where you can learn more

sometimes we can feel down on ourselves for not being as quick to grasp stuff as our neighbour

just try to remember that it's not about them, it's about being a little ahead of where you were

And I hope this helped you learn a little more than what you knew going in

Resources

Stack overflow

stackoverflow.com

Running into errors while programming you can't figure out? The answer is probably here.

 

An invaluable resource for coders.

don't try to write all of this down

I'LL GIVE YOU a LINK TO
THESE SLIDES

Learn python

learnpython.org

Free, interactive, online teaching material to get more in-depth in Python.

Harvard cs50

edx.org/course/cs50s-introduction-computer-science-harvardx-cs50x

Free introduction to computer science course from Harvard. Getting a certificate costs $90.

Free Code camp

learn.freecodecamp.org

Free online courses to learn web development basics.

LEAP

leap.ycombinator.com

Community by & for women in tech, with a focus on startups.

startup school

startupschool.org

Free videos and material to teach how to start a startup. You can audit courses.

Deadline is August 13th.

This is an
incomplete list

There are tons of free resources out there

hacker news is a good source to find out about free new learning materials

Text

news.ycombinator.com

A word for those interested in this as a career

do it.

i have a degree in
physical geography

This is one of the few technical fields where university education isn't a requirement

especially not in startups

source: https://www.digitalocean.com/currents/june-2018/

here's the thing

the qualities of the best programmers

are those who make unimpressive code

code that is
easy to read

and the best coders are those who are kind to colleagues

and are able to both give and listen to feedback constructively

because this is a very cooperative field

anyone can do this as a job

Like anything

It's about practice

And a desire to learn

you got this.

questions?

if you think of anything you want to ask, contact me via my site

maelys.bio

Thank you to our volunteers

Thank you!

get slides here: 
goo.gl/KWWMT

my website: 
maelys.bio

Bonus content!

files

has anyone ever written a complicated excel formula? 

sometimes it's easier to write a program to do the same thing 

but you don't want to store the data in the program

Because then you'd have to change the program every time

What if...

PROGRAMS COULD READ FILES with the data in them

then the program would stay the same

You could just change the data in the file

okay I know this isn't new information

we're going to write a program that reads a file full of numbers

Yes... more numbers

I know it's annoying to do numbers numbers numbers but srsly it makes code simpler and easier to learn

we're going to write a program that reads numbers and multiplies them by 3

But first let's create the file with the numbers

create a new file called "numbers.txt"

write the following in the "numbers.txt" file

3
100
123456
333

do not have empty lines or the program will complain

create a new file called "files.py"

# Open the file.
with open("numbers.txt") as number_file:

    # Read each line in the file.
    for line in number_file:

        # Multiply number by 3.
        result = int(line) * 3
        print(result)

Add the following code to the file

run the program in the terminal

python3 files.py
py.exe files.py

(     )

python files.py

Did anyone get a result they did not expect?

new things were introduced with that code

# Open the file.
with open("numbers.txt") as number_file:

open() is a function to open files

in programming languages like python, files also have to be closed when you're done with them

# Open the file.
with open("numbers.txt") as number_file:

with tells python to close the file after the indented stuff is over

# Open the file.
with open("numbers.txt") as number_file:

number_file is a variable that contains the data that's in the file

You can call this variable whatever you want

next line

    # Read each line in the file.
    for line in number_file:

For ... in repeats the code that follows, in this case for every line in the file

    # Read each line in the file.
    for line in number_file:

Line is the variable that stores the contents of a line in the file

You can call this variable whatever you want

next lineS

        # Multiply number by 3.
        result = int(line) * 3
        print(result)

int() is A function that converts the line, which is text right now, into a number

This takes the number on the line and multiplies it by 3

I know this seems really convoluted

Let's do another example

create a new file called "text.txt"

add these lines in the "text.txt" file

the flag is red
the door is green
the jar is transparent

do not have empty lines or the program will complain

with open("text.txt") as number_file:
    for line in number_file:
        if "green" in line:
            print("This is green.")
        else:
            print("This is not green.")

Add the following code to the "files.py" file

run the program in the terminal

python3 files.py
py.exe files.py

(     )

python files.py
with open("text.txt") as number_file:
    for line in number_file:
        if "green" in line:
            print("This is green.")
        else:
            print("This is not green.")

What does the if "green" in line do?

Now it's your turn

create a new file called "names.txt"

put five people's names in this file

Make sure each name is on its own line

now modify the code in "files.py" so that it opens the "names.txt" file

and lets you know which names are longer than eight characters

name = "Maya Angelou"
length_of_name = len(name)
print(length_of_name)

Reminder: the len() function lets you know how long text is

does anyone not have the result they expected?

A Gentle Intro to Coding - Continued

By Maëlys

A Gentle Intro to Coding - Continued

This is the continuation of a two-part workshop to teach non-programmers a bit about coding.

  • 1,367