Welcome to Python for Buffy fans

Introduction

Python is a widely used, high level programming language created by Guido van Rossum in 1991.

Buffy the Vampire Slayer is a TV show created by Joss Whedon, that first aired in 1997, and became a cult classic.

This page briefly introduces us to some simple concepts in Python, by using examples from Buffy the Vampire Slayer.

Print

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object.

We wrap the content of what we want printed in brackets ()

Therefore, the following code:

print('In every generation, there is a chosen one.')

outputs:

In every generation, there is a chosen one.
Numbers and Strings

Numbers and strings are the most basic of data types we use in Python.

A string is a series of characters inside quotes.

'I am a string'

Strings can be concatenated, or added one to another, by using an addition operator +.

A number can be an integer (meaning it can be written without a fractional component), like 42, or a float (any number with a decimal point), like 4.2.

We can use the following operations when working with numbers:

  1. Addition +
  2. Subtraction -
  3. Multiplication *
  4. Division /
  5. Exponents **

If we want to concatenate strings and numbers, we need to wrap the number inside the str() element.

print('There are ' + str(7) + ' seasons of Buffy')

outputs:

There are 7 seasons of Buffy.
Lists

Lists allow us to store sets of information in one place. They contain items in a particular order.

We define lists by giving them a plural name, and wrapping the values inside square brackets [].

villains = ['master', 'spike', 'angelus', 'mayor']

To access elements inside a list, we can tell Python the index position of said item. Note that the first item in a list will always have the index of 0, so if we want to access the third element, we'll have to offset it by one.

print(villains[3])

outputs:

Mayor

Lists can be modified by adding items, inserting them, and removing them.

Tuples

Tuples are immutable lists, which means that their content cannot be modified. They visually differ from lists as well, because their values are defined within regular brackets (), instead of square brackets we use for lists.

Tuples are:

  1. ordered
  2. unchangeable
heroes = ('buffy', 'xander', 'willow', 'giles', 'oz')
For Loops

A loop simply enables us to repeat an action (or actions) for the entire list, without having to go through each value separately.

A simple for-loop looks like this:

heroes = ['buffy', 'xander', 'willow', 'giles', 'oz']

for hero in heroes:
    print('I love ' + hero)
If Statements

Python supports the usual logical conditions from mathematics, and responds adequately to results True and False.

Boolean values are constant objects that are either

If statements can be tested for truth values, and give the result of either True or False. Depending on the result, if statements allow us to examine the current state of a program, and respond to it appropriately.

if hero in heroes == 'willow': 
    print(hero + ' turns evil at one point')
If-else Chains

We use the if-else chain for taking an action when a conditional test doesn't pass. In just an if-statement, the code would be ignored, and we would have no input.

if hero in heroes == 'willow':
    print('You just turned evil, ' + hero)
else:
    print('You are still cool, ' + hero)  
If-elif-else Chains

If we want to test more than two situations, we can use a chain that has the following components:

if, elif, and else

Note:

  1. Python creates one block of code
  2. tests are ran in order until one has passed
if hero in heroes == 'willow':
    print('You just turned bad ' + hero.title())
elif hero in heroes == 'xander':
    print('You just turned rad, ' + hero.title())
else:
    print('You guys rock ' + hero.title())