To begin this interactive lesson, click on the rocket ship in the top navigation and then select “Binder” to launch the Jupyter Notebook. It may take a few moments to load the first time you launch it.

The rocket and Binder logos

When you are in the interactive environment, you will see a Jupyter logo in the upper left-hand corner.

The Jupyter logo


What is Python?

Python is a programming language that allows us to write instructions for a computer. We call these instructions “code.”

Behind the scenes of our computers, there are millions of lines of code written in many different programming languages that make it possible for us to use them. For example, if you want to delete a file called “untitled.txt” on your computer, you probably drag-and-drop that file into your desktop trashcan. When this happens, behind the scenes, a programming language like Python runs a piece of code like this:

os.remove(“untitled.txt”)

Today, we will be writing instructions in Python for the virtual computer inside this Jupyter Notebook.

What does Python code look like?

Python code has two parts:

  1. Comments

  2. Code

Take a look at the example below. The # symbol denotes a comment, which contains explanatory notes for us as the reader. The 1 + 1 is the actual code and contains the instructions for the computer.

# this is a comment
1 + 1

Learning to write Python is a bit like learning to write a foreign language. In the beginning, you will probably make some mistakes while writing your code that will make it impossible for the computer to understand.

Helpfully, if you make a mistake, Python will output an error message to try to help you find the problem. Try running the code below to produce an error:

1 +

What can I do with Python?

Python can do four main things:

  1. Basic operations (such as calculator math)

  2. Save variables

  3. Built-in functions

  4. Custom functions

1. Basic operations

The simplest thing you can do in Python is use basic operators (such as +,-,*, or /) to do calculator math. As an example, try running the code below:

1 + 1

You should see an output appear with the answer. This whole 1+1 line of code is called an expression, the + is called an operator, and the numbers are called values.

Note: Spaces do not matter here. You can write 1+1, 1 + 1, or even 1+ 1 and all of them will run successfully.

Try writing and running your own basic operation here:

# try your own basic operation here

There are also more advanced operators, such as those used to make comparisons. These return an output of “True” or “False” depending on whether or not the comparison is true. These comparison operators include:

  • > greater than

  • < less than

  • >= greater than or equal to

  • <= less than or equal to

  • == equal to

As an example, try running the code below:

1 < 4

The output here is “True” because 1 is less than 4. Try writing and running your own comparison operation here:

# try your own comparison operation here

To learn more about operators, check out this list of Python operators.

2. Save Variables

Python allows you to create and name containers that you can store your data in. These containers are called variables. You can name your variable just about anything you want, but the name must follow these rules:

  • must contain only letters, numbers, and _ (e.g. cannot contain spaces or symbols like #)

  • can’t start with a number

  • is case sensitive (e.g. number, Number, and NUMBER are three different variables)

For example, try running the code below that creates a variable called myNumber and sets its initial value equal to 1.

myNumber = 1

A line of code like this that uses the = operator is called an assignment statement and sets the value of a variable. Now that the value has been set, try using the variable in the code below:

myNumber * 10

You can also update the value of your variable by using another assignment statement. Try updating the value of your variable below:

myNumber = 2

Now try running this line of code again and see how the output changes:

myNumber * 10

To learn more about variable names, check out this guide to variable names.

3. Built-in Functions

A function is a command in Python that executes a set of pre-written instructions, without you having to write them out each time. You give the function an input and the function runs its instructions and gives you back an output.

Python has many built-in functions, including the following for popular math equations:

  • abs(x) returns the absolute value of x

  • round(x) rounds x

  • pow(x,y) returns the value of x to the power of y

As an example, try running the code below:

abs(-1)

Now try running your own built-in function example here:

# try running your own built-in function example here

To learn more about built-in functions, check out this list of built-in Python functions.

4. Custom Functions

You can also name and write your own custom functions with pre-written instructions that can be saved and used later.

For example, see this function that adds one to a number:

def addOne(someInputNumber):
    outputNumber = someInputNumber + 1
    return print(outputNumber)

Try using this function to add 1 to the number 10 by running addOne(10) below:

addOne(10)

Now try using the function on your own number here:

# try using the addOne function on your own example number here

To learn more about custom functions, check out this guide to Python functions.

What types of data can Python use?

Python can work with many different types of data including numbers and text. For a full list, check out this list of Python data types.

For this workshop, will mainly be using the three following basic types of data:

Data Python "Data Type" Example
Whole number int ("integer") 534
Decimal float 6.3
Text str ("string") "hello"

To figure out the type of a particular piece of data, use the built-in type() function, like so:

type("this is some example text")

Try testing the data type of your own example below:

# try testing the data type of your own example

How does Python work with text?

Python has all of the same functionality with text as it has with numbers, with a few subtle changes (e.g. it wouldn’t really make sense to use abs() on text). To get a sense of how Python works with text data, check out the examples below.

Example 1: Basic operators.

For text, the + operator combines text. This action is called “concatenation.” Try concatenating two pieces of text together using the code below:

"Hoo" + "ray!"

Example 2: Comparison operators.

For text, the == operator can be used to compare two pieces of text, including their capitalization. Try the example below:

"Example" == "example"

This returns “False” because, although the two words are the same, their capitalization does not match.

Example 3: Built-in functions.

For text, there are useful built-in functions such as len() which outputs the length of a piece of text. Try the example below:

len("Hooray!")

Example 4: Custon functions.

For text, custom functions can be defined in the same way they could for functions involving numbers. Try using the example function below that makes use of concatenation with the + operator and makes use of the built-in print() function to output text.

def sayHello(firstName, lastName):
    whatToSay = "Hello " + firstName + " " + lastName + "!"
    print(whatToSay)

Try using the function with your own name by editing and running the code below:

# edit this function to use your name
sayHello("Jacinda","Ardern")

Lesson complete!

In the next lesson, you’ll find the most significant terms in that dataset you created in Constellate.

If you’d like to learn more Python, Constellate has published some great open Jupyter Notebook lessons including beginner and intermediate Python. There are many additional resources online for learning Python, such as Python for Everybody.

https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/CC_BY.png

Adapted by Kate Kryder from two notebooks created by Nathan Kelber and Ted Lawless for JSTOR Labs under Creative Commons CC BY License.
See here for the original versions. Some content was also adapted from notebooks created by Sarah Connell and Jen Ferguson for earlier versions of this workshop.