Workshop - Part 1
This is the self-guided, self-paced tutorial for part 1. We’ve included a break in the middle, but remember to take a break to stretch, get some water, or take a moment to relax any time you need to. If at any time you have questions, ask away!
If you’ve not yet done so, ensure you’ve gone through these setup instructions to install and configure Python.
As you go through this tutorial, you’ll see code samples like this:
a = "Hello"
This means that you can type this line in at the
>>> prompt in your Python shell. Simply enter the line, hit Return or Enter after every line you enter, and note the output (although sometimes there won’t be any!).
Tip: Don’t copy and paste commands – type them out. You’ll learn far more if you take the time to type everything yourself.
Math in Python looks a lot like math with a calculator.
2 + 2
12 - 5
Multiplication uses the
* (asterisk or star) symbol.
6 * 7
Division uses the
Note: If you’ve used Python 2, you’ll see that division works differently in Python 3. Python 2 uses floor division for integers, meaning it will return only the whole number part of the answer. Python 3 performs true division, returning the real or true value of the division.
To get only the whole number in Python 3, use this syntax:
Thinking back to long division that you may have learned in school, the modulus is the “remainder” after perfoming division. It uses the
Order of operations works just like you learned in math class - Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.
5 + 4 * 3
There’s a helpful function (more on what a function is later) called
type that tells you the data type of a given variable. We can check for ourselves that Python considers ‘1’ and ‘1.0’ to be different data types:
So now we’ve seen two data types: integers and floats. We’ll see more data types as we work through the lesson.
We also used the term “function” without explaining what it is – we’ll talk about functions more in a bit, and write our own, but for now know these things:
- Functions encapsulate some useful bit of work. We save that useful bit of work inside a function so we don’t have to type it repeatedly. For example, some nice person decided that being able to determine the type of an object was useful, so he or she put the Python code that figures out an object’s type into the function
type(), and now we all get to use it, instead of having to write it ourselves.
- Functions are sort of like functions in math class. You can provide input to a function and it produces output. The
type()function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output.
- To use a function, write the name of the function followed by an open parenthesis, what the function takes as input (we call that input the arguments to the function), and then a close parenthesis.
So in our case, ‘type’ is the name of the function, and it takes one argument; in the two examples above, we first give type an argument of 1 and then give it an argument of 1.0.
Stop for a moment here and try pressing the up arrow on your keyboard a few times. The Python interpreter saves a history of what you’ve entered, so you can arrow up to old commands and hit Return to re-run them – just like your computer’s command prompt!
A lot of work gets done in Python using variables. A variable consists of an identifier (the variable’s name) and a value stored in the variable – any kind of value, not just a number. In the example below,
x is our identifier and
4 is the value stored:
x = 4
2 * x
Giving a name to something so that you can refer to it by that name later is called assignment. Above, we assigned the name ‘x’ to 4, and after that we can use x wherever we want to use the number 4.
The space between the identifier, the equal sign, and its value does not matter. The two variables below are indentical. However, you should be consistent in your code.
x = 5
Variables can also be re-assigned.
x = 3
x = 7
Be careful when you do this though… accidentally re-assigning a variable can cause bugs in your code.
A variable’s identifier can contain letters, numbers, and underscores. However, identifiers must start with a letter and can not contain any other special characters. Here are some valid variable names:
magic_number = 1500
amountOfFlour = .75
my_name = "Jessica"
Projects develop naming conventions, so multi-word identifiers may use underscores (like
magic_number), or “camel case” (like
amountOfFlour). The most important thing is to be consistent within a project, because it makes the code more readable.
So far we’ve seen two data types: integers and floats. Another useful data type is a string, which is just what Python calls a bunch of characters (like numbers, letters, whitespace, and punctuation) put together between quotation marks:
"Python, I'm your #1 fan!"
Like with the numeric data types, we can check the type of a string:
You can assign a string value to a variable:
my_car = "Toyota"
You can concatenate (join) strings together using the
"Hello" + "World"
"I drive a " + my_car
You can not concatenate a string with another data type.
"Hello" + 123
will give you a traceback:
Traceback (most recent call last):
A traceback provides details on what was happening when Python encounters an Exception or Error – something it doesn’t know how to handle. There are many kinds of Python errors, each with descriptive names to help us humans understand what went wrong. In this case we are getting a
TypeError: we tried to do some operation on a data type that isn’t supported for that data type.
Python gives us a helpful error message as part of the
"cannot concatenate 'str' and 'int' objects".
We can, however, use the
str() function to convert a number to a string. Like the
type() function we’ve been using,
str() takes one argument as input and outputs it as a string.
There’s another useful function that works on strings called
len(). This returns the length of a string as an integer.
president = "Obama"
We’ve been using double quotes around your strings, but either double or single quotes are valid in Python:
You do have to be careful about using quotes inside strings:
'Let's learn Python together!'
will give you another
traceback, for a
SyntaxError. When Python evaluates this expression, it starts at the first single quote as the start of the string, and to the next single quote as the end of the string. Then it doesn’t know what to do with the rest of the stuff that follows.
There are a few ways to solve this problem. One is to use double quotes on the outside:
"Let's learn Python together!"
or we can escape the quote with a backslash:
'Let\'s learn Python together!'
The backslash is a special character that tells Python to treat the next character literally, not as part of the syntax or code. Thus, you can do things like this:
str = "Fernando said, \"Python is fun!\""
Let’s take a look at a couple of exercises. Read the following lines of code, but don’t execute them. Try to figure out what they will do. Sketch them out with pen and paper if you need to. Then type them in your terminal and execute them to see what happens.
total = 1.5 - 1/2 + ((-2.0/2) - (1.0/2))
a = "quick"
print(2.0 * 123 + str(2.0) * 123)
Pause: Let’s take a break. We’ve covered a lot!
Below is an example of how to use comments and insert new lines:
# The pound sign is used as a comment character in Python. Programmers
So far, we’ve been running code directly from the shell interpreter (the
>>> prompt). This is great for testing and exploring short bits of code, but for longer projects we want to save our script in a file and run it from our computer’s terminal.
- Copy and paste the entire code block from the previous section (including the comments) into a new text file and save it as
mayors.py. Note the
.pyfile extension, indicating that this is a Python script.
Open your computer’s terminal (not the Python shell) and navigate to the directory where you saved this file. At your computer’s command prompt, type:
to run this script. Study what happens. Edit this file so it displays the last four mayors of Philadelphia (Look it up on Wikipedia if you need to!). Save it and run it again.
CHECKPOINT: Do you know how to do these things?
: How do you run a Python script from your computer’s terminal?
: How do you comment code in Python?
: How do you print just a newline?
: How do you print a multi-line string so that whitespace is preserved?
So far, the code we’ve written has been unconditional: no choice is getting made–all of the code runs. Python has another data type called a boolean that is helpful for writing code that makes decisions. Booleans hold two values:
Re-open the Python console and try typing these:
You can test if Python objects are equal or unequal. The result is a boolean. Try typing these expressions in your Python console:
0 == 0
1 == 0
54 = 42
== to test for equality. Recall that
= is used for assignment of a variable to a value. This is an important idea and can be a source of bugs until you get used to it:
= is assignment,
== is comparison.
!= to test for inequality:
"a" != "a"
"a" != "A"
>= have the same meaning as in math class. The result of these tests is a boolean:
1 > 0
2 >= 3
-1 < 0
.5 <= 1
You can check for containment with the
in keyword, which also results in a boolean:
"H" in "Hello"
"X" in "Hello"
Or check for a lack of containment with
"a" not in "abcde"
"Chicago" not in "Philadelphia Python Workshop"
Now that we know how to check if something is
False, we can use this to make Python execute commands conditionally.
if 6 > 5:
That was our first multi-line piece of code, and the way to enter it at a Python prompt is a little different.
- First, type the
if 6 > 5:part, and hit
enter. The next line will have
...as a prompt, instead of the usual
>>>. This is Python telling us that we are in the middle of a code block, and so long as we indent our code it should be a part of this code block.
- Type 4 spaces, type
print("Six is greater than five!"), and then hit
enterto end the line.
- Finally, hit
enteragain to tell Python you are done with this code block. All together, it will look like this:
if 6 > 5:
So what’s going on here? When Python encounters the
if keyword, it evaluates the expression following the keyword and before the colon. If that expression is
True, Python executes the code in the indented code block under the
if line. If that expression is
False, Python skips over the code block.
In this case, because 6 really is greater than 5, Python executes the code block under the if statement, and we see “Six is greater than five!” printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct:
if 0 > 2:
if "banana" in "bananarama":
more choices: if and else
You can use the
else keyword to execute code only when the
if expression isn’t
sister_age = 15
if, the code block under the
else statement must be indented so Python knows that it is a part of the
We’ve been testing single conditions, but we can also test multiple conditions that result in execution of some code. You can check multiple expressions together using the
- If two expressions are joined by an
and, they both have to be
Truefor the overall expression to be
- If two expressions are joined by an
or, as long as at least one is
True, the overall expression is
Try typing these out and see what you get:
1 > 0 and 1 < 2
1 < 2 and "x" in "abc"
"a" in "hello" or "e" in "hello"
1 <= 0 or "a" not in "abc"
Guess what will happen when you enter these next two examples, and then type them out and see if you are correct. If you have trouble with the indenting, call over a staff member and practice together. It is important to be comfortable with indenting for tomorrow. Indenting is a crucial part of the syntax of Python.
temperature = 32
hour = 11
You can have as many lines of code as you want in if and else block; just make sure to indent them so Python knows they are a part of the block.
even more choices: elif and else
If you have more than two cases, you can use the
elif keyword to check more cases. Think of
elif as Python-speak for else if. You can have as many
elif cases as you want. Python will go down the code checking each
elif until it finds a
True condition or reaches the default
sister_age = 15
You don’t have to have an
else block if you don’t need it. That just means there isn’t default code to execute when none of the
color = "orange"
If color had been “purple”, that code wouldn’t have printed anything.
= is for assignment and
== is for comparison.
Functions take input from the user or the application and (usually) produce output (e.g. they return a value). You can then assign a variable to this output. As we’ve shown in previous sections, you call a function by using its name followed by its arguments in parenthesis.
Why are functions important?
- They allow tasks to be run quickly and to be automated, i.e. they do some useful bit of work.
- They let us re-use code without having to type it out each time.
- They facilitate consistency and reduce the risk of error.
Python has many built in functions. For example:
length = len("Mississippi")
Executing this code assigns the length of the string “Mississippi” to the variable length. We can write our own functions to encapsulate bits of useful work so we can reuse them. Here’s how you do it:
Step 1: Write a Function signature
A function signature tells you how the function will be called. It starts with the keyword
def, which tells Python that you are defining a function. Then comes a space, the name of your function, an open parenthesis, the comma-separated input parameters for your function, a close parenthesis, and a colon.
Here’s what a function signature looks like for a function that takes no arguments:
Here’s what a function signature looks like for a function that takes one argument called my_string:
And one for a function that takes two arguments:
def myFunction(myList, myInteger):
Parameters should have names that usefully describe what they are used for in the function.
Note: We’ve used the words parameters and arguments seemingly interchangeably to reference the input to functions. The distinction isn’t really important right now, but if you’re curious: in function signatures the input is called parameters, and when you are calling the function the input is called arguments.
Step 2: Do useful work inside the function
Underneath the function signature is where you do your useful work. Everything inside the function is indented, just like with if/else blocks, so Python knows that it is a part of the function. You can use the variables passed into the function as parameters, just like you can use variables once you define them outside of functions.
def add(x, y):
Step 3: Return something
If you want to be able to assign a variable to the output of a function, the function has to return that output using the
def add(x, y):
or, even shorter:
def add(x, y):
return x + y
You can return any Python object: numbers, strings, booleans … even other functions! Once you execute a return, you are done with the function – you don’t get to do any more work. That means that if you have a function like this:
number is less than 0, you return number * -1 and never even get to the last line of the function. However, if number is greater than or equal to 0, the if expression evaluates to False, so we skip the code in the if block and return number.
We could have written the above function like this if we wanted. It’s the same logic, just more typing:
Step 4: Use the function
Once you define a function you can use it as many times as you want. You can assign the value it returns to other variables and use those variables in other commands.
def add(x, y):
result1 = add(1234, 5678)
Functions don’t have to return anything if you don’t want them to. They usually return something because we usually want to be able to assign variables to their output. If your function does not return anything, you won’t be able to assign a variable to its output and won’t be able to use its output anywhere else.
What do you think will happen here? Try it and see:
That’s it for part 1! Now you can take some time to try some of the practice exercises and review anything here that seemed challenging. We’ll review all this and get deeper into Python during part two’s presentation.