0

Creating and running a basic Python script

Let’s start out by doing something extremely simple.  The traditional “starting point” is a “Hello world” script, so let’s start there.  All we want to do is print some text, which is pretty much as simple as it seems:

  1. Create a new text file and name it python4mm.py.
  2. Add the following code:
    print ("Hello, world!")
  3. Save the file and run the script by typing
    python3 ./python4mm.py
    on the command line.  If you get output of
    Hello, world!
    then you know everything is working perfectly.

Now, that was a pretty simple task, but we can already learn a whole lot from it.  Let’s take a look at the syntax here:

print ("Hello world!")

Here’s what we know so far, just from this one line script:

  1. There aren’t any line numbers.
  2. When we send data to a function (such as print()) we enclose it in parentheses.
  3. Strings, or bits of text, get enclosed in quotation marks.  (It’s a style choice; single quotes (‘) would have worked too.)
  4. There are no semi-colons or other line-ending marks.
  5. Python as an interpreted language, so we don’t have to compile the script.
  6. We don’t have to import anything to do basic operations.

That’s a good start from just one line!  Now let’s start actually doing something and see what else we can learn.

In Python for Mere Mortals we’ll be building an entire RSS application using Python, and you can download the entire book now, rather than waiting for all of it to be posted.

admin

Leave a Reply

Your email address will not be published. Required fields are marked *