Introduction to Python Programming in Ubuntu Linux

By Veronica Henry on February 23, 2011
Test your skills. Learn something new. Get help. Repeat. Start a FREE 10-Day Trial
Python is one of a handful of modern programming languages gaining a lot of traction in the development community. It was created by Guido von Rossum in 1990, named after - you guessed it - the comedy, "Monty Python's Flying Circus". Like Java, once written, programs can be run on any operating system.

Python ProgrammingI began programming many years ago, and admittedly once I got into networking, turned away from my first love. But for those of you who would like to begin trying your hand at a new language, Python is excellent to start with. Supporters tout its low learning curve and elegant coding system.

Python Installation

Ubuntu makes starting easy, as it comes with a command line version pre-installed. In fact, the Ubuntu community develops many of its scripts and tools under Python. You can begin the process with either the command line version or the graphical Interactive Development Environment (IDLE).

Python Programming From the Command Line

Open a terminal window and type 'python' (without the quotes). This opens python in interactive mode.

Python Programming

While this mode is good for initial learning, you may prefer to use a text editor (like Gedit, Vim or Emacs) to write your code. As long as you save it with the .py extension, it can be executed in the terminal window.

Ready to write your first program? Many a road to programming excellence began with the old standby – the Hello World program. So that's where we'll begin.

At the prompt, type in print "Hello World!" (with the quotes) and hit enter. You'll see the command printed on the next line.

Python Programming

To run a script from the command line, type: python my_script.py. To exit the command line, type exit() or hit ctrl+d

Python Programming with IDLE

Can't imagine writing a long program from the command line? Me either. In that case, you can try IDLE. Open a terminal window and type: 'idle'. (without the quotes). You'll see the python graphical shell loaded.

Python Programming

To write a Python script, click on File > New Window. This will open a sort of Notepad type editor. Type in the following code:

#linuxversion.py

#Have user input version and print response

name = raw_input("What Linux release do you use?")

print "I also like", name, " - Linux rules!"

Couple things going on here. The first 2 lines, preceded by the # sign are just comments. One of my earliest programming lessons was to always comment your work, not only for your own sanity, but for those who may need to edit the code after you. The third statement uses the input gathered from the raw_input function and assigns it to a variable, name. Finally, the print statement will return the result.

Save the file, with a .py extension, then click on Run > Run Module from the menu to run the program.

Python Programming

A Word of Caution

Many programming languages typically ignore whitespace – that is the spacing in your code. But in Python, improper use of spacing can generate syntax errors. As an organization nut of sorts, I can appreciate the simplicity and readability of properly aligned code, but for some this takes a bit of getting used to.

Python Programming Resources

Now that you have Python up and running, you'll undoubtedly want to move to on to a bit more complex programs. One of the best resources is the main Python website. Here are a few additional recommendations:

Conclusion

I hope you enjoyed this brief glimpse into Python. This is a language supported by a wide community and used by Google, NASA, Ubuntu and others. Give it a try for your next development project. Happy coding!

Ready to test your skills in Python? See how they stack up with this assessment from Smarterer. Start this Python test now.

 

Get our content first. In your inbox.

Contributor

Veronica Henry

is a writer, web developer and tech guru. Her 20 year IT career came to an end when her inner writer and entrepreneur inexplicably besieged her to give it all up. She is a self-proclaimed girl-geek and linux convert, who has held MCSE, GSEC and PMP certifications. In her dreams, she is a international best-selling sci-fi and fantasy author, but in the meantime, she now spends her days writing, managing her websites and wrecking havoc on her Ubuntu laptop.