Getting Started with Python Programming and Scripting in Linux – Part 1

Basic Operations with Python

As it is to be expected, you can perform arithmetic operations (feel free to use as many parentheses as needed to perform all the operations you want!) and manipulate text strings very easily with Python.

You can also assign the results of operations to variables and display them in the screen. A handy feature in Python is concatenation – just supply the values of variables and / or strings in a comma-delimited list (inside parentheses) to the print function and it will return the sentence composed by the items in the sequence:

>>> a = 5
>>> b = 8
>>> x = b / a
>>> x
>>> print(b, "divided by", a, "equals", x)

Note that you can mix variables of different types (numbers, strings, booleans, etc) and once you have assigned a value to a variable you can change the data type without problems later (for this reason Python is said to be a dynamically typed language).

If you attempt to do this in a statically typed language (such as Java or C#), an error will be thrown.

Learn Python Basic Operations
Learn Python Basic Operations

A brief comment about Object Oriented Programming

In Object Oriented Programming (OOP), all entities in a program are represented as objects and thus they can interact with others. As such, they have properties and most of them can perform actions (known as methods).

For example, let’s suppose we want to create a dog object. Some of the possible properties are color, breed, age, etc, whereas some of the actions a dog can perform are bark(), eat(), sleep(), and many others.

Methods names, as you can see, are followed by a set of parentheses which may (or may not) contain one (or more) arguments (values that are passed to the method).

Let’s illustrate these concepts with one of the basic object types in Python: lists.

Illustrating methods and properties of objects: Lists in Python

A list is an ordered group of items, which do not necessarily have to be all of the same data type. To create an empty list named rockBands, use a pair of square brackets as follows:

To append an item to the end of the list, pass the item to the append() method as follows:

>>> rockBands = []
>>> rockBands.append("The Beatles")
>>> rockBands.append("Pink Floyd")
>>> rockBands.append("The Rolling Stones")

To remove an item from the list, we can pass the specific element to the remove() method, or the position of the element (count starts at zero) in the list to pop().

In other words, we can use either of the following options to remove “The Beatles” from the list:

>>> rockBands.remove("The Beatles")
>>> rockBands.pop(0)

You can display the list of available methods for an object by pressing Ctrl + Space once you’ve typed the name followed by a dot:

List Available Python Methods
List Available Python Methods

A property of a list object is the number of items it contains. It is actually called length and is invoked by passing the list as argument to the len built-in function (by the way, the print statement, which we exemplified earlier-, is another Python built-in function).

If you type len followed by an opening parentheses in the IDLE, you will see the default syntax of the function:

Python len Function
Python len Function

Now what about the individual items of the list. Do they have methods and properties as well? The answer is yes. For example, you can convert a string item to uppercase and get the number of characters it contains as follows:

>>> rockBands[0].upper()
>>> len(rockBands[0])


In this article we have provided a brief introduction to Python, its command-line shell, and the IDLE, and demonstrated how to perform arithmetic calculations, how to store values in variables, how to print back those values to the screen (either on its own or as part of a concatenation), and explained through a practical example what are the methods and properties of an object.

In the next article we will discuss control flow with conditionals and loops. We will also demonstrate how to use what we have learned to write a script to help us in our sysadmin tasks.

Does Python sound like something you would like to learn more about? Stay tuned for the second part in this series (where among other things we will combine the bounties of Python and command-line tools in a script), and also consider buying our Ultimate Python Coding bundle (more details here).

As always, you can count on us if you have any questions about this article. Just send us a message using the contact form below and we will get back to you as soon as possible.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

12 thoughts on “Getting Started with Python Programming and Scripting in Linux – Part 1”

  1. Please, whatever you do, *do not touch the system Python installation!* This includes modifying symlinks or paths or whatever. Large parts of your operating system depend on a particular Python version in the /usr/bin/ directory, be it 2.x or 3.x. This is why some users in the comments report broken functionality.

    Always, always, always use virtual environments which allow you to use whatever Python and third-party package versions you want in an isolated location. Anything else can seriously screw up your distribution.

  2. Warning: I don’t know why but wicd stops working if you modify the symlink for python3.x to python, effectively breaking your ethernet / wi-fi.

  3. Hey Guys,

    I am new Linux SysAdmin and I am going to use Python for scripting language so, I Googled about it before but i did not find anything useful for someone like me.

    would you give me some direction for starting python actually for Linux-system administration?

    thank you

  4. If you want to discard Python 2.x and use 3.x instead when you type python, you can modify the corresponding symbolic links as follows:”

    Be advised this will result in broken packages (Samba and Python libs) in Linux Mint 18.2

  5. Nice one!
    I will wait for the update.

    Just as note. As sysadmin never remove python 2. One of the utilities that will break will be yum.

      • Seriously Gabriel? That’s like posting “If you’d like to increase space on your hard drive, you can use “rm -rf /” and then saying that you” didn’t actually recommend it.” when someone points out it deleted their entire hard drive.

        Novice Linux users come to sites like this and follow directions; “clarifying” potentially damaging instructions after the fact isn’t very useful. You should, at the very least, insert a clear cautionary statement. Better yet, move this “suggestion” and the “clarification to the suggestion” adjacent to each other AND include a cautionary statement that it can break packages on some common distros.


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.