Modal Title
Software Development

More Python for Non-Programmers

Wrote your first program in Python!
Dec 2nd, 2021 9:00am by
Featued image for: More Python for Non-Programmers

So you’ve taken your first steps with Python with the help of “An Introduction to Python for Non-Programmers.” You understand what Python is, why it’s a high-level, general-purpose language, and you’ve even written your first application with the language.

That’s exciting. But there’s so much more to learn. Fortunately, this language is really easy to get up to speed with (although it will take a while to really master). What I want to do this time around, is move you forward a bit with the language, all the while making sure you understand the concepts as a non-programmer.

It’s really easy to teach someone who firmly grasps the concepts of code. But when you’re absolutely new to the game, those concepts can fly right over your head.

Let’s make sure that doesn’t happen.

In the first piece, I explained what a function is and showed you how to use the print() function in a simply Hello, World! application. We’re going to build on that, but first I want to show you an easy way to test your function skills from the Python console.

What Is the Python Console?

Remember how you used the print() function within the Hello, World! app? For a refresher, the file used for that app is:


The file consists of a comment (the first line) and the code that makes the app run. To run the app (named hello-newstack.py), the command would be:

python3 hello-newstack.py

You remember that, right? Okay, what if you wanted to first make sure you knew how to write that function properly, without adding it to the file? To do that, you could open the Python console, which is a quick and easy way to test and execute Python commands.

Say, for instance, you want to create a Python app, but before you add a new line of code to it, you want to make sure the code works. Let’s use our print() example.

To access the Python console, open a terminal window (on the machine that has Python installed) and issue the command:

python3

You should now find yourself in the Python console (Figure 1).

Inside the Python console.

Figure 1: The Python console is ready to test and execute your commands.

Go back to the print() function by issuing the command:

print('Hello, New Stack!')

You should see Hello, New Stack! printed in the console.

To exit out of the console, you have to type another function, which is:

exit()

Hit Enter on your keyboard and you’ll be back at your computer’s terminal window.

How to Use Variables with Python

Now that you know how to create a very basic Python application and different ways to execute Python commands, let’s create a new application, one that contains a variable. What is a variable? In terms of programming, a variable is used to store specific information that will be referenced within the code.

Here’s a very simple example. Let’s say you need to use a date within your program. Instead of always having to type out the necessary date, you could set it as a variable like so:

date = "Thursday November 18, 2021"

We’ve now set our variable. Let’s use it in an application. Create a new file with the command:

nano date.py

In that file, we’ll set our variable by adding the line from above, so:


Our program won’t be much good with just the data variable set. Let’s make use of the variable by printing it out with the print() function like so:


Our full application looks like this:


Save and close the file. Execute the program with:

python3 date.py

You should see “Thursday November 18, 2021”, printed in the terminal window.

Let’s expand on that. As is, our application isn’t any good if we run it on, say, Friday 19, 2021, because it’ll print out yesterday’s date. Instead of explicitly setting the data variable we can use a function that stores today’s date as the date variable. That function is date.today(). Using that function, we set our variable with:


The above line would use the date.today() function to set the date variable to whatever the date was when the application is run.

However, there’s already a problem. The date.today() function isn’t available to use without first loading it. How do we do that? Easy. What we do is import the date function from the DateTime module with the line:


With that line at the top of our file, date will be available to any function that calls it. So now our new application looks like this so far:


Outstanding. Next, we’re going to add the line that actually prints out the information, which makes our little application useful. But instead of just printing out the results of the date variable, we’d add the text line “Today’s date is:” That print() line would look like:


Our entire application would look like this:


Let’s add comments, so we know what each section does:


Save and close the file. Run our new application with:

python3 date.py

The output should be:

Today's date is: 2021-11-18

Of course, the date in the output will change, depending on the date you run the application.

And there you go, you’ve taken your next step with the Python programming language. When next we visit this topic, we’ll create an application that accepts input from a user.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.