Scripts

Command-Line Scripts

We’ve typed all of our code at the REPL up to now, which is pretty limiting. So let’s move on to making Python programs!

A “program” is a file with code in it that you can run to get something done. A “script” is cute name for a small program. We’ll be making command-line scripts, which are just scripts that we run from your computer’s command-line.

In the ZIP file you downloaded earlier, there’s a file called birds.py which contains the following code:

"""Ever make mistakes in life? Let’s make them birds.
Yeah, they’re birds now. -Bob Ross"""

To run this program we can go to our terminal window (not the REPL, so you shouldn’t see >>>), and type:

$ python3 birds.py

Or in Windows:

> py -3 birds.py

Huh. That didn’t do anything. The string in the file isn’t printing. That’s because running code in in the REPL automatically prints it out for us, but when we’re writing a script we have to tell Python what to do with the string, or it’ll do nothing. Let’s make a little tweak to the file so it’ll print out.

print("""Ever make mistakes in life? Let’s make them birds.
Yeah, they’re birds now. -Bob Ross""")

Now if we run the file in the terminal window this happens:

$ python3 birds.py
Ever make mistakes in life? Let’s make them birds.
Yeah, they’re birds now. -Bob Ross

We made a tiny baby program that says an adorable thing from the legendary Bob Ross! Yay for us!