Let’s play with Python!

People always feel compelled to make dumb Monty Python jokes when they hear about Python. I know you won't do that, though.
People always feel compelled to make dumb Monty Python jokes when they hear about Python. I know you won’t do that, though.

It’s really fun to work in the terminal, but Python is a more broadly useful programming language. A lot of people use it to manipulate data and documents, automate tasks, and even build complex applications.

Python is installed on the Lab computers. If you want to install it on your own computer later, here are instructions for how to do it.

We’ll start by using Python in the terminal, and then we’ll move to a Python-specific coding environment called IDLE.

(By the way, you may wonder why it’s called Python.)

Start Python

This is incredibly complicated. Type

python

and press return. You should then see some information about the version of Python you’re running, followed by three little angled brackets, like this: >>> Those three angled brackets mean your computer is waiting for you to tell it what to do, in the Python computer language.

start-python

Pretend your computer is a giant calculator

Your computer is waiting for you to give it instructions in Python, so let’s do that. Type

2+2

and press return.

Python, as you can see, recognizes integers, like the number 2, and operators, like the plus sign (as well as other operators, like the multiplication (*) and division (/) signs). If you want, try writing some other arithmetical expressions and pressing return so that your computer will tell you the answer.

Admire yourself as you do this. You look like a hacker!

pretend-your-computer-is-a-giant-calculator

Concatenate strings

In programming parlance, a string is a series of characters. Concatenate is a fun word that basically means “stick them together.” In Python, you denote a string by surrounding it with quotation marks. You concatenate with the plus sign. Try this:

"I"+"love"+"digital"+"humanities"

and press return. Your computer should spit out a concatenated version of the strings you fed it.

concatenate-strings

Assign a variable

You can begin unlocking the power of a programming language when you start using variables. A variable is a set of characters that stands for something else. A variable could stand for a string, or a number, or even a series of operations. We’ll assign a variable to a string. Type:

a = "I love digital humanities"

and press return. Then type

print(a)

and press return. (With a lot of programming languages, print doesn’t mean “print something on paper”; it means “show me this on the screen.”)

Your computer will show you the contents of the variable called a — namely, the string “I love digital humanities.”

assign-a-variable

Perform operations with variables

What do you think will happen if you type

a+a+a+a

and press return? Try it and see.

Now try

a*3

and press return.

perform-operations-with-variables

Open your IDLE

As you’ve seen, you can work with Python directly in the terminal. But it’s kind of a pain, because you have to type in one line at a time, and you can’t go back and change stuff once you’ve pressed return.

Most people, instead of writing programs directly in the terminal, will write code in a text document, which you can work on and save to run again whenever you want. You can use any kind of code editor to write Python — you just save the document with the file extension .py so that your computer knows it’s a Python program.

Today, we’ll use IDLE to write our Python program. IDLE, which stands for Integrated DeveLopment Environment, is sort of a combination of a text editor and a terminal. You’ll write your Python code in a document and then press Run to make the program go.

To find IDLE, go to your Applications folder and open up the folder called Python 3.5. (The version is important.) Once inside that folder, find the application called IDLE and double-click.

open-your-idle

Understanding IDLE

When IDLE first opens, you’ll see the shell, which is kind of like your terminal, but just running Python. You won’t do your coding there, though. Instead, open a new, blank file by going to File, then New File.

This new, blank file is where you’ll write your code. Go ahead and save it now (File -> Save). It doesn’t matter where you save it or what you call it, but try to put it somewhere where you can find it again later.

understanding-idle

 

Our first expression in IDLE

Let’s just get the hang of how IDLE works. In the new file you just created, type

print("Hello world")

That means print the string inside the quotation marks. Save your document, and then, from the top menu, click on Run and then Run Module. This tells IDLE that you now want to execute the Python program you wrote.

Look over at the shell. You should see the output of your program there.

our-first-expression-in-idle

Our first real program!

(Borrowed from Automate the Boring Stuff with Python.)

Delete the contents of your file and copy and paste the following into the document:

# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?')    # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')    # ask for their age
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')

Before you run this little program, let’s talk about what it’s doing. Read through the explanation contained in the image.

If you don’t get it, put up a red flag, and we’ll talk you through it.

Click on the image to see it full-size.
Click on the image to see it full-size.

Run your program

Save your file, and then from the top menu, click on Run and then Run module. If all goes well, you should be able to interact with the Python program you just created.

run-your-program

Next steps

That’s probably enough for today, but now you know the basics of writing a Python program! Because you’ve written your code in a file and saved it, you could in theory run it anytime you wanted.

Of course, Python gets a lot more complicated, but it’s nothing you can’t handle. If you want to learn more, many people like Learn Python the Hard Way, which is available for free online or for purchase as a book. I like Automate the Boring Stuff with Python because it covers a lot of familiar tasks. Finally, many people have gotten up and running with humanities programming with the excellent Programming Historian.

If you prefer learning in person, you might be interested in joining PyLadies L.A. (You don’t actually have to be a lady!) This is a welcoming group of people who are interested in fostering diversity and inclusion.

When you’re learning to program, play and have fun. Take a break if you get overwhelmed and come back to it later. It takes time to absorb programming concepts, even though everyone’s always talking about bootcamps and crash courses and the like.

If we still have time left in class, take a look at chapter two of Automate the Boring Stuff with Python and learn about control structures, one of the most important things to know about any kind of programming language.