Python

From IT Jalakam - Unofficial IT syllabus for grade 10 students

Jump to: navigation, search

Python is, in short, a scripting language. It is similar in function to Perl .it can be run on almost any platform . You can get Python IDE free at http://www.python.org/download/

Python is a scripted, i.e. interpreted, language. Unlike C, which has a compiler, we have in Python the interpreter .

Python Prompt looks something like >>> Where we enter all commands.


Hello World

if you've ever read a programming tutorial before you know they all have some sort of program that just prints "Hello, World!" or something on the screen. Unfortunately, this tutorial is no different. Alright, here goes:

   hello = "Hello, World!"
   print hello

and save it as a .py file


Input

In our Hello, World! example above, we already saw one way of printing output, namely the (self-explanatory) print statement. print works pretty much like it does in perl, printing a string or any sort of variable or expression out on the screen. Anything you enter with print automatically has a newline, "\n", appended to it.

   >>> print "She's a witch!"
   She's a witch!
   >>> print 5 + 3
   8
   >>> a = "Burn her!"
   >>> print a
   Burn her!
   >>> #Don't worry if you don't know what this does; it's covered in the Intermediate section
   ... print "Yeah, b" + a[1:]
   Yeah, burn her!


If you don't want a newline appended, as is sometimes the case, simply add a ',' to the end of the line with your print statement. Note that this only works in non-interactive mode, so you need to make a new file.py or something. Try this on for size:

  1. !/usr/bin/python
   a = 5
   print "The value of a is",
   print a,
   print "!"


Output:

   The value of a is 5 !


There's a few more ways you can put (non-string) variables in a print statement. You can use a comma, which will put a space around the variable. The final main way is to convert your variable to a string first, which is done simply by putting it in some backwards quotes (``). Then just append this to your string like you would any other string variable or value (see above). Check out these examples:

   >>> a=5
   >>> #Note the spacing around the number
   ... print "A duck costs $", a, "."
   A duck costs $ 5 .
   >>> #Same thing, different way of putting it in
   ... print "A duck costs $" + `a` + "."
   A duck costs $5.


For those of you who know what stderr is (or stdout and stdin for that matter), yes, you can access it directly in Python. This does however use a bit more of an advanced technique, so to really understand what's going on here you should read File I/O and Modules. Anyway, try out this script:

   #!/usr/bin/python


   import sys #Didn't I tell you to read about modules?
   sys.stderr.write( "This is some error text\n" ) #You do need a \n here since
   #you're writing directly to the file descriptor (READ FILE I/O)


Run it, it looks more or less normal, just prints "This is some error text". Now try running it as "python test.py > /dev/null" (Unix) or "test.py > nul" (Dos/Win). What's this? The output is still there, even though you redirected it to /dev/null or nul, the wastebasket of output. Instead of writing to the standard output (stdout), which is what print does, here you wrote to the standard error (stderr), which is separate from stdout (even though they appear on the same screen) and informs you of any errors your program might encounter.

  Referances :
   # Python.org. A must-see.
   # Current Python docs. Gotta read them.
Personal tools
Create a book