Pin Me

A Guide to Linux Programming with Python

written by: Berry van der Linden•edited by: Michael Dougherty•updated: 1/23/2011

This is the first article in a series for programming with Python in Linux. This article will provide you with a brief introduction to Python and show you how to set up your Python programming environment, code your first Python application and get you started with variables.

  • slide 1 of 6

    Introduction

    Python is a powerful, yet simple to learn programming language. Python has high-level data structures and an effective approach to object-oriented programming. It's graceful syntax makes it a perfect programming language for scripting and application development on most platforms.This informal article introduces you to the basic concepts and features of Python.

    It is surprising how many applications and libraries have connections with Python: MySQL, SVN, Qt and many more. There are applications that have a Python plug-in interface, like GIMP and Blender. You can also write extension modules for Python, or you can embed Python in your software.

    Python is available for any platform you can think of. This is a very important aspect of Python, since you never know when you will be asked whether you code runs on platform "X" also.

    For this article I will use Python 2.5. Don't worry if you have an older version, the examples will run on older versions also.

  • slide 2 of 6

    Set up your programming environment

    To begin programming in Python, you will need a text editor, preferably with syntax highlighting; such as "gedit." Also, you will need a Python interpreter, which is usually installed with most Linux distros. If you do not have a Python interpreter installed just install it via your package manager.

    You can, of course, use an IDE instead of a basic text editor. An IDE can make things much simpler. Eclipse or Netbeans are my favorite IDEs, but I'm sure you will have your own favorites. Just remember to install the PyDev library if you are using Eclipse, this can be found here or in your package manager

  • slide 3 of 6
  • slide 4 of 6

    Get your hands dirty

    I won't waste time with theory or syntax right now, as I am presuming you have some programming experience, if not these things can be learnt elsewhere. Anyway, I often think it's best to learn by getting your hands dirty so lets get started. Once you can code a hello world app, you are only a few steps from doing anything you want.

    #!/bin/python

    print "Hello, world!"

    Just enter these lines to an empty file, either in your IDE or your text editor. Then you just need to make the file executable by giving the it a chmod +x command and run it: chmod -x filename.py For more information on the chmod command, please read this Bright Hub article here.

    Line one tells the interpreter to run the code below. In the second line, we print the text that we want to return, in this case "Hello, world!" Since there are no lines after this, the program will quit.

    A useful feature with print is, multi-line printing, which can be used for printing any kind of multi-line text. Again enter the top line to tell the Python Interpreter to execute the code below. Then start the block with triple quotes, and end with triple quotes, like this:

    #!/bin/python

    print """

    Line one.

    Line two.

    Line three.

    """

  • slide 5 of 6

    Variables

    Moving on from printing to something a little more juicy - variables. All programming languages use variables, so now would be a good time to teach you how to use variables in Python.

    The best way to explain a variable is to imagine a box that stores information. You can put data in the box, you can remove data from the box and you can swap different data in the box.

    Here is how to use variables in Python:

    #!/bin/python

    text = "Programming Python is simple with a little help!"

    num = 7

    print text

    print num

    There is no need to tell Python the variable type. As with PHP, it will choose the appropriate type for you, you will hardly ever have to think about types with Python.

  • slide 6 of 6

    Next time

    The next article in the series will expand on the use of variables in Python, as well as covering Lists and Data Structures.

A Guide to Linux Programming with Python

Python is a very useful language, whether you need a little script that edits files or want to create a bigger project. Python has all the power you need, including testing, modularization, object-orientation and a massive API. This series will let you get your hands dirty with Python and Linux.
  1. A Guide to Linux Programming with Python
  2. A Guide to Linux Programming with Python - Variables & Data Structures
  3. A Guide to Linux Programming with Python - Tuples, Dictionaries and Sets





© Copyright 2016 brighthub.com.