How to write and execute a simple Python script on Mac OS X, for complete beginners

Lately there is many people who want to learn computer programming and their first choice is to start with Python, a scripting language which can be easily utilized to automate different tasks such as scraping web pages on the Internet, interacting with public APIs and even pulling data our of your .excel documents.

Being a Python nerd myself, it makes me very happy to see new comers to the technology as the more coders make use of it, the higher is the chance it lives really long.

About Python

Python is a high level computer programming language which offers the professional programmer the necessary tools required to prototype and build computer software. Completely open source and free as in beer, it is widely used by software engineers all over world. Giants like Google and Youtube make use of Python computer programming language too, in fact they have big systems which depend heavily on Python code.

Not only already established companies make use of Python, but startups too, as the Python technology offers the right tools needed for doing rapid development and prototyping.

Python is not hard, but truth is that it is not easy as most of the newbies make it. One has to code a real project before giving any opinion on the difficulty of the programming language.

The first time I got introduced to Python code it felt like I was reading myself, my personal thoughts materialized in a computer technology.

Enough words, time for some action.

You need a Python interpreter to execute Python code

Python is an interpreted programming language, which means that for one to execute Python code on their local machine, they have to make sure they have the official interpreter. Fortunately for you guys, in Mac OS X computers, Python is shipped by default.

To make sure you have Python already installed on your Mac OS X machine, go to Launchpad, search for the terminal and after you have opened it, type the following command.

python

After the above command is executed on your Mac OS X, if everything goes fine, the following will come up.

The stuff shown in the above screenshot comes from the Python interpreter.

Execute you first Python line code

To execute Python code in the interpreter all one has to do is type the line of code and then hit Return button. In Python, code is being executed line by line. As far as I know there is two ways to run Python code, interactively and  script mode.

Once one launches Python shell from their terminal, the interactive mode of executing code is being activated.

Type the following in your Python shell and hit Return.

1 + 2

The following comes up.

If everything has worked correctly, you have successfully executed your first Python line code. As it is seen from the above example executed in the Python shell, when one works in interactive mode, every line of code produces an immediate result. The good thing about working with Python in interactive mode is the fact that one can easily test pieces of code and see for themselves what they do.

Interactive mode code execution, is a Python feature which I truly love as not only it does help one to test and play with parts of their code, but it is very useful for the beginners too.

Some Python basics needed to write the script

One important part of programming languages is the variables which is being used to keep track of data. One can easily declare variables in Python programming language by using the following syntax.

a = 5

The above variable links to data of type int, to an integer. There is many other data types supported in Python such as strings, floating point, list, tuple and dictionary.

b = 13.0 # a floating point

The above variable b, links to a floating point object. To experiment a little bit in the Python interactive shell, run the following arithmetic operation.

a + b

 

Python supports arithmetic operations by default. Other data types important for one during their Python coder journey is list and tuple.

A list is used to store objects of various data types. The following is the syntax for declaring a list.

l = []

Perfect for storing different objects, the list data type supports indexing which can be used to access its elements.

Declare another list in Python interactive shell like shown below.

l = [1, ‘liberiangeek’, 1.0]

For one to access the elements of the list, indexing operations can be used. The first element has an index of 0.

l[0]

It should produce the following result.

1

The second element of the list can be accessed with the following syntax.

l[1]

The following result should come after executing the above Python code in the interactive shell.

‘liberiangeek’

Fact is that list objects can change in time, elements can be added to and removed from them.

To add an element inside a Python list, the list specific method should be used like shown below.

l.append(‘new_element’)

Once the above code is being executed in the Python interactive shell, the list should be updated.

To verify is the list is updated or not, do the following check.

l

On the other hand, tuples is similar to lists, but the main difference is that they don’t change in time.

A tuple is declared with the following syntax.

t = () # this is a tuple

Same as lists, tuples support indexing too.

t = (1, 2, 3)

t [0]

Write the Python script

Any script written in Python should end in the .py extension, and it is called a module. For example, for the purpose of this tutorial, I am going to create the following text document in my text editor and then save it as a Python file.

liberiangeek.py

First thing I recommend right now for the level of knowledge you possess, is to comment the script, so you can easily understand its purpose when referring to it in the future.

The following syntax can be used to write a comment in Python. Comments is being used to explain code, they don’t get interpreted by the interpreter, instead they get ignored as their purpose is to help the coder comment their code.

# this is a comment which explains the python script

After having written the above code in the Python script, save the file in a .py extension. The script is not finished yet, it’s purpose is to practice all the Python concepts being covered through this article.

Create two variables like shown below, and make sure to save the Python script again, so you can avoid losing your work in case of any problems with your computer.

Then try to write a simple arithmetic operation in the script, like shown below.

c = a + b

Once you have written all the code above and saved the script, declare a list like shown below, and pull the first element out of it by indexing.

l = [‘liberiangeek’, ‘python’, ‘coder’]

first_el_of_list = l[0]

Before executing the Python script, there is a very important Python statement which we need to make use of, the print statement. It helps one to display output on the console.

Add the following two lines in your script, and you are done.

print(c)

print(first_el_of_list)

Execute the Python script

Before running the script it is very important that the working directory on your terminal, matches the path where the script is being stored. Mine is placed on /Users/Oltjano/Desktop/liberiangeek.py.

The following command is used to execute a Python script from the Mac OS X terminal.

python liberiangeek.py

Final thoughts

Executing a Python script on local Mac OS X is truly easy as the machine offers the interpreter by default, but those who have no idea about Python code, get usually stuck when they have to run a script.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Invest in some screen real estate with a new big screen, backlit monitor availabe in five different sizes and price points.

Leave a Reply

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