More Python for Non-Programmers – The New Stack

0

So you have taken your first steps with Python with the help of “An Introduction to Python for Non-Programmers”. You understand what Python is, why it is a high-level generalist language, and you even wrote your first application in this language.

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

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

Let’s make sure that doesn’t happen.

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

What is the Python console?

Remember how you used the print() function in the Hello, World! application ? As a reminder, the file used for this application is:

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

python3 hello-newstack.py

You remember that, don’t you? Okay, so what if you wanted to make sure you first knew how to write this function correctly, without adding it to the file? You can do this by opening the Python console, which is a quick and easy way to test and run Python commands.

Say, for example, 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 is working. Let’s use our print() Example.

To access the Python console, open a terminal window (on the machine where Python is installed) and run the command:

python3

You should now end up in the Python console (Figure 1).

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

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

print('Hello, New Stack!')

You should see Hello, new battery! printed in the console.

To exit the console, you need to type in another function, which is:

exit()

Hit enter on your keyboard and you will be back to 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 run Python commands, let’s create a new application that contains a variable. What is a variable? In terms of programming, a variable is used to store specific information that will be referenced in the code.

Here is a very simple example. Let’s say you need to use a date in your schedule. Instead of always having to type in the necessary date, you can define it as a variable like so:

date = "Thursday November 18, 2021"

We have now defined our variable. Let’s use it in an app. Create a new file with the command:

nano date.py

In this file, we will define our variable by adding the line above, so:

Our program will not be very good with just the set of data variables. Let’s use the variable by printing it with the print() works like this:

Our full app looks like this:

Save and close the file. Run the program with:

python3 date.py

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

Let’s expand on that. As it is, our app is of no use if we run it on Friday 19 2021, for example, because it will print yesterday’s date. Instead of explicitly setting the data variable, we can use a function that stores today’s date as a date variable. This function is date.today(). Using this function, we define our variable with:

The line above would use the date.today() function to set the date variable regardless of the date the application is run.

However, there is already a problem. the date.today() The function is not available for use without first charging it. How do we do that? Easy. What we do is import the date function from the DateTime module with the line:

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

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

Our whole app would look like this:

Let’s add some comments, so we know what each section is for:

Save and close the file. Run our new app 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 when you run the app.

And there you have it, you’ve taken your next step with the Python programming language. The next time we visit this topic, we’ll create an app that accepts input from a user.

Share.

Comments are closed.