Introducing Python in an After School setting (5th-8th grade)

Ken Wade

@kenjyco (github)

kwade@optionsaway.com

About me

  • Fan of GNU/Linux tools and light-weight distros
  • Vim user since '03
  • Python user since '05
  • Enjoy simplifying processes whenever possible
  • Enjoy volunteering and interacting with kids of various ages
  • Engineer at Options Away (startup in 1871 incubator)
    • yes, we're hiring

The class

  • Meets once a week after school for 10 weeks
  • Around 12 students between 5th and 8th grade
  • Sometimes has access to Windows laptops (cart)
  • Sometimes has access to Mac minis (lab)
  • Supplement with several customized Ubuntu Mini X200s
  • Primarily use terminal or https://try.jupyter.org

Goals for students

  • Don't fear the terminal (it's fun)!
  • Build confidence & develop curiosity about what's possible
  • Understand basic object types and operators
  • Get familiar with commonly used built-in functions
  • Understand the differences between basic containers
  • Understand how to access data in containers
  • Get familiar with the properties and methods on common object types
  • Recognize the difference between positional and keyword arguments
  • Use formatted strings to display information
  • Get familiar with conditional statements and loops
  • Be able to use comprehensions to re-shape containers
  • Import and use modules from standard library

Bonus goals

  • Don't fear Exceptions (they are helpful)!
  • Use type constructors to create/convert objects
  • Be able to create custom objects and functions
  • Be able to create a module
  • Conceptually understand files, directories, and repositories
  • Understand that 3rd party packages exist
  • Be familiar with virtual environments
  • Be familiar with markdown syntax
  • Be able to help other students with concepts you understand

Concepts to Introduce

Python objects, basic types, and variables

Everything in Python is an object and every object has a type.

  • int
  • float
  • str
  • bool

A variable is a name you specify in your code that maps to a particular object, object instance, or value.

Basic operators

There are different types of operators (special symbols) that operate on different values

  • arithmetic operators
  • assignment operators
  • comparison operators (return True or False)

When multiple operators are used in a single expression, operator precedence determines which parts of the expression are evaluated in which order (like PEMDAS in math).

Basic containers

Containers are objects that can be used to group other objects together.

  • str
  • list
  • tuple
  • set
  • dict

Strings, lists, and tuples are all sequence types that can use the +, *, +=, and *= operators

Accessing data in containers

 

For strings, lists, tuples, and dicts, we can use subscript notation (square brackets) to access data at an index.

  • strings, lists, and tuples are indexed by ints, starting at 0
    • these also support accessing a range of items (slicing)
    • negative indexing is also supported
  • dicts are indexed by their keys
  • sets are not indexed, so we cannot use subscript notation to access data elements

Built-in functions and callables

A function is an object you can "call" to perform an action, by placing parentheses to the right of the function name. Some functions allow you to pass in arguments.

  • type()
  • len()
  • callable()
  • sorted()
  • sum()
  • min()
  • max()
  • repr()

Object attributes

Different types of objects have different attributes that can be accessed via dot notation (i.e. obj.attribute).

  • When an attribute is a callable, it's called a method (same as a function, but bound to a particular object)
  • When an attribute is not a callable, it's called a property (just a piece of data about the object, that is another object)

The built-in dir() function can be used to return a list of an object's attributes.

Positional args and keyword args

 

Different types of objects have different attributes that can be accessed via dot notation (i.e. obj.attribute).

  • func()
  • fun(arg1, arg2, ..., argn)
  • func(kwarg1=value, kwarg2=value)
  • func(arg1, arg2, kwarg1=value, kwarg2=value)
  • obj.method()
    • same for func and other func examples

When using positional args, you must provide them in the order the function defined them.

Formatting strings

string_1 = 'My name is {name} and I like {thing}'
string_2 = 'Three things are {}, {}, and {}'

print(string_1.format(name='Mary', thing='to dance'))
print(string_1.format(name='Todd', thing='dogs'))
print(string_2.format('apples', 'oranges', 'grapes')

You can use named or unnamed place-holders in strings you define. When you call the "format" method on a string, you can pass keyword arguments or positional arguments.

Python "for loops"

It is easy to iterate over a collection of items using a for loop. The strings, lists, tuples, sets, and dictionaries are all iterable containers.

 

The for loop will go through the specified container, one item at a time, and provide a temporary variable for the current item.

for item in [-2, 5, 7, -11, 4]:
    print(abs(item))

for key, value in some_dict.items():
    print('{} -> {}'.format(key, value))

Python "if statements" and "while loops"

The if statement allows you to test a condition and perform some actions if the condition evaluates to True. You can also provide elif and/or else clauses to an if statement to take alternative actions.

 

The while loop will keep looping until its conditional expression evaluates to False (possible to loop "forever").

 

Note: Since the for loop will iterate over a container of items until there are no more, there is no need to specify a "stop looping" condition.

Other

  • list, set, and dict comprehensions
  • importing modules
  • exceptions
  • classes (creating your own objects)
  • defining functions and methods
  • creating an initializer method for your class
  • other "magic methods"
  • creating a module/script
  • files and directories
  • source control (git)

Challenges for students

  • Confused when to use () vs [] vs {}
  • Forgetting to enclose strings with quotes
  • Forgetting to start the Python shell (and typing Python statements at bash/zsh prompt)
  • Forgetting to separate sequence items with comma
  • Indentation

Challenges for volunteer

  • Students can be restless at the end of the school day when lesson starts
  • Students can doubt themselves and think things are hard before they even start
  • Meeting an hour a week with no homework is insufficient practice
  • General classroom disruptions

Tips/Rules

  • Be patient
  • Go slow
  • Be encouraging
  • Build on concepts students have already been exposed to
  • Be willing to explain the same thing in different ways
  • Ask students about their personal interests to make examples more relevant
  • Float around the room and give students 1-on-1 time
  • Be genuine and earn their trust over time
  • Share your enthusiasm

Intro to Python After School

By Kenneth Wade

Intro to Python After School

  • 1,977