Introduction

Required Software

If you’re on Windows, make sure to select the "Add to PATH" option when you install Python.

First Steps

Starting A REPL

REPL is short for "Read, Evaluate, Print, Loop". We’ll go over what that means in depth later on; all you need to know right now is that it opens a text box where you can type Python into it and have it print out the results.

Windows

To start a REPL on Windows, press Win+R. This will open a Run window. Type powershell into it and hit Enter.

Windows Run Prompt

Hello, world!

A common very first exercise is printing "Hello, world!" to the screen, and you can do it too!

Type this into a Python REPL and hit Enter:

print("Hello, world!")

Learning Your Legos

Math (not to be confused with meth)

The simplest way to use Python is as a calculator. You can type math expressions in, and it will print out a result.

These are the operators you can use:

Symbol Name Description

+

Addition

Adds numbers (or sometimes other things) together.

-

Subtraction

Subtracts numbers (or sometimes other things) from each other.

*

Multiplication

Multiplies two numbers.

/

Division

Divides one number by another.

%

Modulus

Returns the remainder if the first number were divided by the second.

**

Exponent

Returns the first number to the power of the second number. I.e. 3**2 == 3*3 == 9

There isn’t a shorthand way to do roots like a sqaure root, but you can do something like 3**(1/2), which is the same thing as the square root of 3.

You can play around with these in a REPL.

Variables

You might be thinking to yourself "why would I do that? My calculator works just fine." You’re right, it can do those things. You know how you always have to keep a piece of paper next to your calculator as scratch paper to remember numbers?

Python is better than your calculator, because it has scratch paper built in.

In programming, we call them "variables". A variable is basically a little piece of digital scratch paper that you can write stuff on and have the computer remember it for you.

To use them, you tell Python what you want to call this piece of scratch paper, then an equals sign to tell Python that you want to write on that scratch paper, then the value you want to put in it.

For example, to save the number 5 into a variable named x:

x = 5

Once you do that, you can start using it by just using it in place of the number:

x = 5
x*3

Which should show 15.

You can also write the output of your math to the variable:

x=5
y=x*3
y*6

Which should print 90

Strings

No, not the kind of strings your cat likes. If your cat likes these kinds of strings, you should probably call the people outside Area 51.

A string in programming languages is a series of one or more letters (this is only half true, but we’ll talk about that later). In Python, you have to put double quotes around the letters so Python knows it isn’t a variable. You saw this earlier when we did:

print("Hello, world!")

"Hello, world!" is a string! Be aware that an empty string, "", is still a string, even though there aren’t any letters in it.

A neat trick is that the addition operator (+) also works on strings; it just adds them together. I.e.

greeting = "Salutations, "
name = "Seth"

greeting + name

Will show "Salutations, Seth".

Functions

Functions are kind of like a recipe. They specify the ingredients you need to make them, what to do with the ingredients, and what comes out. The difference is that instead of hoity toity stuff like saffron and lavender, we use trans fats. Er, variables, I mean.

For example, we could rewrite our "Salutations, Seth" example as a function.

def salutations(name):
    return "Salutations, " + name

salutations("Seth")

There’s a lot of new stuff in there. Let’s break it down a little bit.

def salutations(name): is like the cheat sheat at the top of the recipe that lists the ingredients.

def just lets Python know that we’re making a function. All functions will start with this.

salutations is the name of our function. Functions get saved as variables, so Python needs to know what to call this function. Just like how you’d write a recipe on a piece of scratch paper.

Even though they don’t include the = symbol, functions are just like normal variables. If you make a variable with the same name as your function, it might overwrite your function and break all kinds of stuff.

(name) lists the "ingredients" we are supposed to get. These ingredients are variables that you give to the function when you run it. The formal term for them is arguments. The parantheses are just there to help Python tell that those are arguments instead of part of the name of the function.

salutations("Seth") is where we actually "make" the recipe, or "execute the function" in official terms. Python doesn’t actually run the function when we declare it. All we’ve done is told Python to save the recipe on a piece of scratch paper.

To run a function, you put the name of the function, followed by parantheses that enclose the arguments, or ingredients, that we want to use this time.

Other Resources

Resource Description

Code The Blocks

A fun resource for practicing Python using 3D Lego-like blocks.

Learn Python The Hard Way

A less fun, but effective resource that has you practice Python by repeatedly typing it out.