NEW! Mirantis Academy -   Learn confidently with expert guidance and On-demand content.   Learn More

< BLOG HOME

Python for Mere Mortals: Learn Python by building a simple project - even if you’re not a programmer

Nick Chase - December 13, 2021
image

We are excited to launch the second edition of our programming guide, Python for Mere Mortals, by Mirantis' own Nick Chase. Below, we've shared the opening chapters where you will learn who this book is meant for, get set up to start coding in Python, and learn how to create and run the most basic Python script. Download the full eBook for free.

Who this book is for

As a general rule, I tend to avoid learning a new programming language until I'm forced to because I need it for a particular project. The upside of this is that I don't waste my time on a language that may not be useful to me. The downside is that I often spend a lot of time being intimidated by a language that a lot of other people are using.

Most recently, that was what happened with Python, a powerful interpreted, object-oriented scripting language behind sites such as Google, YouTube, Yahoo and Quora.

I'd managed to get by without getting into its strictly indexed goodness (even after years of working in the OpenStack market) until realizing it was the best way for me to use Tensorflow to learn machine learning.

Fortunately, Python is really pretty easy to learn and use. In this book we're going to create a sample application that shows you how to:

  • Take info from the command line
  • Output text
  • Use a for-loop
  • Read a file
  • Write to a file
  • Create a function
  • Import a library

To do that, we'll look at an old standby favorite app of mine: the Really Simple Syndication (RSS) reader, which enables you to read web news feeds, podcast feeds, and so on.

This book is for anyone who wants to get started with Python. It will be easier if you have programming experience, but it's not essential: everything you need to get started is right here. The main thing is to take it slowly and concentrate on one step at a time.

If you have questions, contact me and I'll do my best to help you out.

Setting up

If you're using Linux or Mac, you should already have Python installed, though it may be an older version. You can also download binaries for Linux and various operating systems such as Windows and Mac here.

There are literally dozens of different ways to install Python. For example, if you're using Ubuntu, rather than directly downloading binaries, you could simply type:

sudo apt-get update
sudo apt-get install python3

If you're using Windows or MacOS, you can download the proper installer and run it just like any other installer.

Either way, the important thing is to ensure that you're using a version of Python 3 so that you can follow along with the code examples. Python 2 is still available, but there are important syntax changes between the two.

To ensure you're using the right version, type:

python3 --version

This should return a result listing a version of Python 3 (for example, 3.9.7). Your system may include a version of Python 2 as well, since the older version of the language remains in very wide usage, but for this tutorial you should make sure you are using Python 3.

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 is 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.

Working with variables

Now let's look at giving ourselves a little more functionality. We can start by creating and outputting a variable, or a placeholder for a piece of information. Go back to your python4mm.py file and add:

print("Hello world!")
feed_url = "http://www.mirantis.com/feed/" print("We're going to output the feed at: " + feed_url)

Now if you save and run the file, you'll get additional output:

Hello, world! We're going to output the feed at: http://www.mirantis.com/feed/

This snippet shows us a few more things:

  1. Variables, or placeholders for information, are just names. They don't have a dollar sign, or anything else that distinguishes them. If it's a word and it's not a "reserved" word such as "print", it's going to be treated as a variable.
  2. We can combine strings using the plus sign (+). (There are other ways to do it, but we're starting small.)
  3. Each print statement is going to output on its own line, even though we didn't tell it to.

So far we've been dealing only with built-in functions. Sometimes, though, Python itself doesn't give you what you need, and you need to get more help. In the next step, we'll do that by importing a package.

Want to keep going? Download Python for Mere Mortals (2nd Edition), by Nick Chase, here.

Choose your cloud native journey.

Whatever your role, we’re here to help with open source tools and world-class support.

GET STARTED
NEWSLETTER

Subscribe to our bi-weekly newsletter for exclusive interviews, expert commentary, and thought leadership on topics shaping the cloud native world.

JOIN NOW