Gather Workshops Logo

The Power of Python

Finding Python's place in the world

What is Python?

Python - It's for everybody

Python Loves:

  • Readability of Code
  • Simple Syntax
  • Basic Scripting
  • Complex Applications

Install Python > Create Script > Make Magic

Easy as that!

Python Logo

Python 3

is the latest greatest Python

Python in the Real World

What is it actually used for?

Python is everywhere!

Github's Top 5

  1. JavaScript
  2. Ruby
  3. Java
  4. PHP
  5. Python - woohoo!

Python - It's for everybody

Benefits of Learning Python

Why not learn something else?

Python teaches us many

Common Concepts

Python - It's for everybody

Solve Real-World Problems

using hardware as well as software

A Valuable Skill In Any Career

pop it on your next job application!

Workshop Goals

What will we achieve today?

We're Here To Make Cool Stuff

Not to do a crash course in programming theory

The Power of Python

is wide and varied.

Gather Workshops Logo

Language Basics

A Whirlwind Tour of Variables, Conditionals, Loops and Functions

Getting Started is Easy

Code Snippets

Python - It's for everybody

snippets.zip

Komodo Edit

or IDLE as an alternative

Komodo Projects

  1. Open Komodo
  2. Create a new project
  3. Select folder "Snippets"
  4. Click "Open"

Running Scripts

  • Go to Tools > Run Command...
  • Enter %(python) -u %F
  • Tick "Add to toolbox"
  • Done!

Hello World

Using the print() function

Variables

are like named boxes where you can store information

username = "pythongeek93"

topScore = 81000500

Playing with Variables

Make the program to say hello to you by name!

Can you combine a string variable and a number variable
to print out "Welcome to 2014"?

 

Pro Tip: to convert a number to a string, use

str(numberVariable)

Dictionaries and Arrays

store sets of related data values

Dictionaries let us look things up by their name.

Arrays let us look things up by their position in the array.

Experiment with Dictionaries and Arrays

Add your own name and score to the dictionary!

Can you print out everyone's name and score?

Loops

Let you run the same piece of code many times

Play with loops

Can you make the loop print out the numbers 1 to 10?

What about printing the 10 times tables?

Conditionals

If this, then that

Play with Conditionals

Can you add support for a bronze trophy?

The award should be given if the player's score is over 600.

Functions

are named pieces of reusable code

Playing with Functions

Said hello? Now say goodbye!

See if you can multiply by ten instead of two.

Classes

Classes can be used to represent real-world objects

Multiple Classes

You'll probably need more than one class

Class Inheritance

Classes can even be mixed together!

The Language of Python

is made up of just a few different parts.

Gather Workshops Logo

Creating Python Programs

Bringing together Python code into a full program

Python Programs

Our Program

Starter Code

AdventureWorld.zip

Project Tour

game.py
adventureworld.py
objects:
  - backpack.py
  - door.py
  - item.py
rooms:
  - room.py
  - alleyway.py

First Run

Welcome to my Adventure Game

You wake to find yourself in a dark, dirty alleyway. Night is falling. Your head hurts and you can't remember how you got here.

Rooms

Add alleyway to the game

Import Alleyway class at the top of adventureworld.py:

import sys
from rooms.alleyway import Alleyway

Add the Alleyway to the list of available rooms:

def createRooms(self):
    self.rooms = {
        "The Alleyway": Alleyway()
    }

Enter Alleyway

def start(self):

print("")
print("Welcome to my Adventure Game")
print("You wake to find yourself in a dark, dirty alleyway. Night is falling. Your head hurts and you can't remember how you got here. ")
print("")

room = self.rooms["Alleyway"]
print(room.name)
print(room.description)

Door options

# print out the options
print("Options:")

# list the door options
for door in room.doors:
    print ("[" + door.button + "] " + door.name)

User input

# ask the user to make a choice
print("")
buttonPressed = input("What next? ")

Gather Workshops Logo

Game Loops

Running the same code over, and over, and over...

Loop Identification

  • Set Up Game
  • Introduction
  • Enter Room
    • Describe Room
    • Offer options
    • Choose option
  • End Game

Loop Definition

def enterRoom(self, roomName):

    room = self.rooms[roomName]
    print(room.name)
    print(room.description)

    # print out the options
    print("Options:")

    # list the door options
    for door in room.doors:
        print ("[" + door.button + "] " + door.name)

    # ask the user to make a choice
    print("")
    buttonPressed = input("What next? ")

Enter Room

self.enterRoom("Alleyway")

Other Rooms

Navigating Rooms

# try to find a door associated with that button
chosenDoor = room.getDoorByButton(buttonPressed)

self.enterRoom(chosenDoor.leadsTo)

Victory Condition

End in tool shed

if room.name == "Tool Shed":
    self.end()

Pro Tip: Function length

# enter a specific room
def enterRoom(self, roomName):

    room = self.rooms[roomName]

    # describe the room
    print("")
    print(room.name)
    print(room.description)
    print("")

    if room.name == "Tool Shed":
        self.end()
    else:
        self.askWhatToDo(room)

Ask What To Do

def askWhatToDo(self, room):

    # print out the options
    print("Options:")

    ... all the lines between ...

    self.enterRoom(chosenDoor.leadsTo)

Simplified enterRoom

def enterRoom(self, roomName):

    room = self.rooms[roomName]
    print(room.name)
    print(room.description)

    if room.name == "Tool Shed":
        self.end()
    else:
        self.askWhatToDo(room)

Hide Key

In cellar.py:

# create all the items you can interact with in the room
self.items = [
    Item("k", "Key")
]

Print Room Items

# list the door options
for door in room.doors:
    print ("[" + door.button + "] " + door.name)

# list the item options
for item in room.items:
    print ("[" + item.button + "] " + item.name)

Picking Up Items

Backpack

self.backpack = Backpack()

Item By Button

# try to find a door associated with that button
chosenDoor = room.getDoorByButton(buttonPressed)

# try to find an item associated with that button
chosenItem = room.getItemByButton(buttonPressed)

Take Item

# add an item to your backpack  
def takeItem(self, item, room):
    room.removeItem(item)
    self.backpack.items.append(item)
    print("Added " + item.name + " to backpack.")

Item to Backpack

# act based on the door or item selected
if chosenDoor != None:
    self.enterRoom(chosenDoor.leadsTo)     
elif chosenItem != None:
    self.takeItem(chosenItem, room)
    self.askWhatToDo(room)

Lock Door

In gardens.py, modify the Shed Door:

# create all the doors leading out of the room
self.doors = [
    Door("g", "Glass Door", "Dining Hall"),
    Door("s", "Shed Door", "Tool Shed", "Key")
]

Respect Lock

Instead of just:

# act based on the door or item selected
if chosenDoor != None:
    self.enterRoom(chosenDoor.leadsTo)

We need to do:

# act based on the door or item selected
if chosenDoor != None:
    self.openDoor(chosenDoor, room)

Open Door

# try to open the chosen door
def openDoor(self, door, room):

    if door.requiredItem == None:   
        self.enterRoom(door.leadsTo)
    else:
        item = self.backpack.getItem(door.requiredItem)

        if item != None:
            print("Used " + item.name)
            self.enterRoom(door.leadsTo)
        else:
            print("It looks like you need a " + door.requiredItem)
            self.askWhatToDo(room)

NCSS Challenge

6-week programming challenge

working with other students and mentors

plus you could earn a trip to Sydney!

Congratulations!

You're now officially a Python Developer!

Before you go, click through to the last link of the day:

http://tiny.cc/gather

Loading...