Salvatore Lopiparo

Software Engineer

Super Simple Python #9 – Running a Python Script

After this lesson, you will be able to make your very own Python scripts! You’ll be a fully fledged Programmer! (Well, kinda…)

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

First, we need to create the file that will contain our script. In PyCharm, select our “super_simple_tutorial” project that we created in Lesson #0. Next, go to File > New > Python File. We normally name modules (a.k.a. Python files) something related to what it will contain, so lets name it lesson_9.py. Mine now looks like this:

__author__ = 'TorelTwiddler'

Not much there at the moment. You can leave or remove the __author__ = 'TorelTwiddler' line if you want, it won’t affect the rest of our script.

Now let’s put something useful in our module, like a function or two!

def greeter(name):
    print("Hello " + name + "! Nice to meet you!")


def adder(x, y):
    print("Adding " + str(x) + " to " + str(y) + "...")
    return x + y

We now have the functions defined in our module, but we still need to do something with them. That’s when we define the main function. The main function is the entrance point to your script. It’s where you set up anything you need before execution of the interesting stuff happens.

def main():
    greet("Thrall")
    apples = 3
    bananas = 4
    fruit = adder(apples, bananas)
    print("My fruit: " + str(fruit))

At the bottom of your file, we need to tell the Python Interpreter that if this file is the “main” entry point, to execute the main function. I will go into more detail about this is a later lesson. For now, put this at the bottom of the file:

if __name__ == "__main__":
    main()

Hurray! You now have a full Python script! Now to run the file, we’ll need to run a command shell (aka terminal). Luckily PyCharm has one built-in!

In PyCharm, go to Tools > Open Terminal. A new window will pop up in place of our Python Console (if it was open). It should look something like this:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\TorelTwiddler\PycharmProjects\super_simple_tutorial>

Note: You can also get to the operating system Terminal or Command Shell from one of the following: Windows – Start > search for Command Prompt; Mac – Finder > search for Terminal; Linux GUI – search button > Terminal; Linux Terminal – wat?

Now that we have our terminal, let’s run our program. To run a Python script in a terminal, we have to tell Python which script to run. We can use python [script_path] to execute a script. Here is how we run our script:

C:\Users\TorelTwiddler\PycharmProjects\super_simple_tutorial>python lesson_9.py
Hello Thrall! Nice to meet you!
Adding 3 to 4...
My fruit: 7

C:\Users\TorelTwiddler\PycharmProjects\super_simple_tutorial>

Super Simple Python #0 – Installing and Rules

I have always been meaning to flush out this Python tutorial, so here it is. It’ll be really easy, designed for someone who doesn’t know any programming. Also, it shouldn’t take more than about 5 minutes for each lesson! I don’t like long intros, so let’s start!

First, some ground rules.

  1. Type things yourself – Don’t just copy what I type and paste it into your own window. You won’t learn anything that way. They’ll be short, I promise.

  2. Ask questions – Please. Let me know if you don’t understand something. I’ll probably miss something that I take for granted but probably looks like voodoo to others. It’s not magic, I promise.

  3. Have fun – Programming is fun when you get the hang of it. I won’t post anything that I won’t laugh at myself*, I promise.

*and trust me, I laugh at myself all the time.

Install Python!

First things first: Go to http://www.python.org/download.

If you’re on Windows, download Python 3.3 Windows X86 MSI Installer. (We’re installing the 32-bit because there are a few libraries that do not support 64-bit.) Double click on the install file and use the default for everything (keep hitting Next, OK and Finish until the box goes away).

If you’re on a Mac, download the Python 3.3 Installer based on if you have a Puma or a Tabby. Double click (or whatever Mac people do) on the install file and use the default for everything.

If you’re on Linux, and you don’t know how to download Python, you need to set down your older sibling’s computer and go shout profanities on Modern Duty 2 (or whatever you kids play now-a-days). But if you’re insistent, it’s probably something like apt-get install python.

Install PyCharm!

PyCharm is a piece of software called an Integrated Development Environment or IDE. That’s fancy speak for “a feature-rich program to write code.” This step isn’t necessary, but is strongly suggested. Please download their Community Edition (it’s free). It will take us a minute to set up, but it will be worth it!

  1. After you install PyCharm and you get to the first configure window, choose whatever color scheme you like. (I like Darcula!)
  2. On the Welcome screen, choose “Configure”, then “Settings”.

  3. Find “Python Interpreter>Python Interpreters” on the left, then click the “+” on the right. Choose your Python 3.3 install listed, then click “OK” to go back to the Welcome screen.

  4. Click “Create New Project” and name it something like “super_simple_tutorial”.

  5. Finally, go to “Tools>Run Python Console…” on the menu bar.

This Python Console window will be your primary tool through most of the lessons. It will evaluate what you type on the line after the “>>>” and print it back to you below it. For example, type print("hello world") after the “>>>” and you will get something like this:

>>> print("hello world")
hello world

Congratulations! You can now program!