Python Programming for Beginners

If you want to outsmart the Spanish Inquisition, learn Python. This article is a practical introduction to writing non-trivial applications in Python.
Working with Pipes

Many command-line tools let us create pipes for processing data, and it is a good idea to consider implementing this functionality in your own scripts. Pipes allow us to read from the standard input and write to the standard output of our script, as well as read from the standard output and write to the standard input of other commands.

Everything we need to implement pipes in our scripts is stored in the os and sys modules. Let's teach our script to read data from its own standard input (represented by sys.stdin) and copy it, unchanged, to its own standard output (sys.stdout):

#! /usr/local/bin/python
import sys
sys.stdout.write(sys.stdin.read())

This works well, but doesn't allow us to modify the data appearing on the script's standard input. This can be achieved in several ways, depending on how much data you want to process at one time. Listing 10 reads one line at a time and inserts # at the beginning of each line.

Listing 10

If you use the read(n) method instead of readline, you can set the number of bytes to be read from the standard input. Listing 11 reads 256 bytes at a time.

Listing 11

A slightly different approach is needed when you want to read the whole file at one go. We use the sub function from the re module to perform a simple substitution. See Listing 12.

Listing 12

That's about all the basic knowledge needed to work with the standard input and output of our script. However, Python can read the standard output of external pipes or write to their standard input. This time, we'll need to use the os module and its popen function.

Listing 13 writes to the standard input of the pipe sed 's/-/+/g' > output one hundred lines of text, each containing the - string. The data passed to the pipe is then processed by sed and ends up as one hundred lines with +++. You can read from a pipe, too. Listing 14 shows you how.

Listing 13

Listing 14

Writing to the System Log

Listing 15

If you develop applications that you want to keep an eye on and leave a trace of their activity in the system log in a way similar to many daemons running on a typical Linux system, you can do so with the syslog function located in the syslog module. To enable writing to system logs, import the syslog module and add calls to the syslog.syslog function at those points needing to be documented in the system log. See Listing 15 for an example.

Listing 16

To see the output from your script, open another X terminal window or switch to another console and type

tail -f /var/log/messages

to reveal what your script has just been doing. The output looks like Listing 16.

Remember that if you send the same message to the system log several times in a row, it will be buffered until a different one arrives in the system log buffer. It will appear there only once, and the next line in the system log will indicate how many times it was repeated. This bit of code,

#! /usr/local/bin/python
import syslog
# some code
for a in ['a', 'b', 'c']:
        syslog.syslog('Hello from Python!')

will generate the following results:

Jan  20 00:04:33 localhost python: Hello from Python! Jan  20 00:04:49
localhost last message repeated 2 times
Don't treat the system log like a trash can where you can send any kind of garbage; write only the most important information to it.

Reading Environment Variables

Some scripts may need to access information stored in one or more environment variables. Their values at the time your script is executed are stored in the os.environ dictionary, available after you import the os module. Here is the script that prints out all the environment variables set at the time your script executed.

#! /usr/local/bin/python
import os
for a in os.environ.keys():
        print a, ' = ', os.environ.[a]

If you are interested in checking for a particular value and using it in your own script, use this bit of code to get you started.

#! /usr/local/bin/python
import os
if os.environ['USER']:
        print 'Hello, '+os.environ['USER']
Listing 17

If you want to modify the value of a particular environment variable while your script is running, use Listing 17 as a guide.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I have been getting into

Anonymous's picture

I have been getting into Python coding lately. Thank you for this great tutorial.

Easy to understand

Raja Jee's picture

This one is very easy to understand guide for the beginners if you have some programming exposure in any other language.

I wanted to thank you for

Anonymous's picture

I wanted to thank you for this great read!together as one new years eve 2011 tickets! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

Your post really cool. I glad

Marcus Everding's picture

Your post really cool. I glad to be here. I enjoyed reading your articles and if allowed I want to bookmark your posts.......

copy wii games|copy xbox 360 games

I am a Newbee here.I would

4gl program language's picture

I am a Newbee here.I would like to know who was the creator of this language
4gl program language

Interesting article i admit.

cheap buffalo bills tickets's picture

Interesting article i admit. i am a rusty reader to your site *^* i will before long replace my home page with your web site.

Great Samples

RadarRabbit's picture

Thanks, teaching a workshop next week and I am glad that I found your post on Python!

SEO Training

Environmental variables lost when exiting Listing 17

SZ's picture

I tried using os.environ[] as in Listing 17. It worked while I stayed withing the python script. Once I left the python script all my environment variables returned to their original values. How do I use python to change environment variables? Do I have to use a different language?

thnx

jitendra shah's picture

very nice tutorial

Thanks

Karen's picture

Nice read! I have a question though, I can't seem to get your RSS feed to work right in google chrome, is it on my end?
Detox

investment in leeds

parlingtom's picture

Python Programming for Beginners it's rally true. I saw some feature of this python program excellent. I hope in near future they will add more integration on the use of this program. I feel good to read this article about python program.investment in leeds.

Don't overlook functional programming

cmnorton's picture

This was a helpful article.

One important educational thing I have found with Python is there is a need to jump further into it than one might when undertaking learning a new language. I feel this way, because you can learn Python as a C/C++/Java developer, and Python is so powerful, it will oblige. However, you might be writing inefficient code.

Mark Pilgrim's Dive Into Python and Dive Into Python 3 are the only books that take a full shovelful of Python and present it all to you, so you start using generators and iterators early on. My early attempts have thankfully been corrected in forums, so that I am not writing C code in Python, but start to write procedural and functional code in python.

Linux needs a new CLI/GUI scripting/programming language

Anonymous's picture

Python always looks like a great way to start programming but when you toss in the lack of easy to use GUI elements - it becomes cumbersome or native fileSystem support (so you have to tweak with it in order to do something that a one line DOS batch script can do).

Linux really needs an all-purpose, easy to use scripting/programming language akin to some of those found on Windows systems.

Rebol might be the answer - we'll see how version 3.0 comes along. It's also suppose to be cross-platform (some high costs if you want to compile, access DBs).

Would love to see something like AutoHotkey, Autoit, RapidBatch or even a better type of HTA (just to name a few) for Linux.

No offense intended (I know this is an old post too), but people today want to be effective right away. There are multiple solutions for other platforms (Windows for sure, don't know about Mac). Why does Linux keep reinventing the same wheel that really doesn't go anywhere?

There is not even a

Anonymous's picture

There is not even a comparison between Windows and Linux when it comes to programming. Linux is much better. Everyone who has much knowledge on the subject knows this.

nothing new needed

Anonymous's picture

Python has excellent toolkits (Gtk, wx, Qt), visual GUI designers and IDEs, and huge libraries. It's so good that many Windows applications are written in it, and many people choose Linux because scripting is so poor on Windows. If you want something more scripting oriented, you have Tcl/Tk, Expect, Bash, and Perl. Instead of HTA, you get Firefox and xulrunner (depending on what level of control you want). All of those are copiously documented. AutoHotkey, Autoit, and RapidBatch don't even come close, and Windows is comparatively poor when it comes to scripting.

....

ciqoff's picture

\"Linux really needs an all-purpose, easy to use scripting/programming language akin to some of those found on Windows systems.\"

Well, I don't about you, but perl, python and bash(if you may) seems to be the all-purpose, easy to use scripting/programming language for most linux users (not windows).
Not to be rude, but why compare Windows with Linux? Okay, it may be a little too apparent that your a Windows person and a Linux critic. I can tell with you using the words "easy" and "GUI" with at most prevalence.

Perhaps, Linux isn't reinventing the same wheel as you may have perceived it, but rather modifies or creates a new version of the existing wheel. So, instead of having a poorly crafted wheel made out of rock as it was stereotypically first invented, the wheel is crafted to specific needs through current technology and resources at hand. Hopefully, you understand the analogy.

Regards

GUI solutions for Python

Anonymous's picture

Answering my own comment above...

I've been looking at a number of programming and scripting languages of late. Came across 'Titanium' which is a cross-platform solution for Windows, Linux, OSX as well as iPhone and Android. It's able to work with JS, Python, PHP and a host of other softTech.

More info here:
http://en.wikipedia.org/wiki/Appcelerator_Titanium
http://www.appcelerator.com/products/

The License is Apache Public License v2. Looks interesting but I haven't worked with it yet - downloading the pieces now.

somebody need to write Ruby

AprilCoolsDay's picture

somebody need to write Ruby version of this article.

how to make the smooth signal (average the signal) in python?

mut's picture

hi.. i've a problem with my real time signal .. its no smooth ... anyone can help me how to do an averaging in python.. tqvm

some corrections

AprilCoolsDay's picture

some corrections for Listing 2: options.py

At lines 15-17, the string should be triple quoted.
Line 18, indentation level is not the same as the previous print line.

Also none of the following works:

$ ./options.py -ahello

$ ./options.py -a hello

Binary to Decimal

Anonymous's picture

Can anyone help me write a code to convert a binary number into a decimal number

Binary to decimal convertor

Abidos's picture

This:

decimal_number = 0
st_binary_number = "1000111"

n = len(st_binary_number)
pwr = pow(2,n-1)
for i in range(n):
if (st_binary_number[i]=='1'):
decimal_number += pwr
pwr = pwr >> 1

print decimal_number

converts the binary "1000111" into decimal 71 ;)

Simpler code :-)

Anonymous's picture

binary_number = "1000111"
decimal_number = int(binary_number, 2)

print decimal_number # Prints 71

typo in line

Nick Jarboe's picture

print a, ' = ', os.environ.[a]
should be
print a, ' = ', os.environ[a]

Console I/O

Abhijeet Oundhakar's picture

Hi, could you tell me what's the Python function for reading from the console similar to scanf or cin? Thanks

Console I/O answer

Fyorl's picture

I believe it is:
var = raw_input('Enter a value: ')

Input for python

sugam sharma's picture

This ll take input and return in the form of string.
To get the inputin int form input is enough.
Though you can convert int from string just by writing
int(string)
where String is that String which is to be converted into int.

samples

bob's picture

I find the best way to learn a language is by looking at examples.

There are lots of tutoria and plenty of "first" examples (ie hello world) but is there a location where I can find several more stepped examples in python, of increasing difficulty and scope? Or at least be pointed at some sites who's applications make good examples?

I can google "python download" or go to sourceforge; but those are mostly not written to be teaching tools and make too big a jump from "hello world" to "how to compute the answer to life, the universe and everything".

re: Samples

crowmag's picture

There is a lot of simple example code here ...

http://www.java2s.com/Code/Python/CatalogPython.htm

slightly more complex code examples, and not so useless as the title suggests here...

http://www.uselesspython.com/

then there's the cookbook...

http://aspn.activestate.com/ASPN/Cookbook/Python/

and the Vaults of Parnassus...

http://py.vaults.ca/apyllo.py

that should keep ypu busy for a while.

Code Correction

Teguh Iskanto S.'s picture

I've spotted an error on one of your code list inside this article :

#! /usr/local/bin/python
import os
for a in os.environ.keys():
print a, ' = ', os.environ.[a] ----> !!!

Since 'os.environment' is a 'dictionary' or like 'associative array' in PERL then It should have been written like this :

#! /usr/local/bin/python
import os
for a in os.environ.keys():
print a, ' = ', os.environ[a] -----> !!!!

Hope this helps
rgds
TI

Just what I was looking for

Anonymous's picture

Just what I was looking for as a beginner to python, but not to UNIX or programming in general.

A couple of typos that may help people
In listing 1
at the top try (for better portability)
#! /usr/bin/env python
near the bottom
don't should be don\'t

how to handle a non-standard interrupt?

caminoix's picture

how can i define my own keyboard interrupts and then handle them?
i'd like to write a program that would change, say, ctrl-:-o to "o umlaut" (like in german). is it at all possible with python?

HELP!

Shawna's picture

My friend and I are stuck in python and we're wondering if anyone is online to help us out! AIM Shawnaloo or MSN Shawnaloo@hotmail.com Thank you!

command line hanling, how to check if there is an error

Anonymous's picture

i wanna read 3 ints from the command line,
check to see if there are 3 inputs
how?

Re: command line hanling, how to check if there is an error

Anonymous's picture

try looking at the module sys, especially sys.arv. it really is handy when it comes to command line arguments.

Re: command line hanling, how to check if there is an error

Anonymous's picture

make that sys.argv. sorry.

sys.argv

ian's picture

Example of using command arguments:
user@host$ script.py arg1 arg2

import sys
var = sys.argv [2]
print var
#will print arg2