Friday, December 18, 2009


Simple example python script:

name = raw_input(" What is your pet name? ")
print "Hello, " + name + "!"

If you run this (remember to save it first), you should see the following prompt
in the inter-preter window: What is your pet name?

Enter your pet name (for example, Bunny) and press Enter.
You should get something like this:

What is your pet name? // You enter your pet name: Bunny //
Hello, Bunny!

Wednesday, November 25, 2009


Python Collection Data Types:

A sequence type is one that supports the membership operator (in), the size function (len()), slices ([]), and is iterable.

Python provides five built-in sequence types: bytearray, bytes, list, str, and tuple.

Some other sequence types are provided in the standard library, most notably, collections.namedtuple.

Thursday, July 23, 2009


Python Width and Precision:

A conversion specifier may include a field width and a precision. 
The width is the minimum number of characters reserved for a formatted value. The precision is (for a numeric conver-sion) the number of decimals that will be included in the result or (for a string conversion) the maximum number of characters the formatted value may have.

These two parameters are supplied as two integer numbers (width first, then precision), 
separated by a . (dot). Both are optional, but if you want to supply only the precision, 
you must also include the dot:

>>> '%10f' % pi      # Field width 10
'  3.141593'

>>> '%10.2f' % pi    # Field width 10, precision 2
'      3.14'
>>> '%.2f' % pi      # Precision 2
'3.14'
>>> '%.5s' % 'Guido van Rossum'
'Guido'

You can use an * (asterisk) as the width or precision (or both). 
In that case, the number will be read from the tuple argument:

>>> '%.*s' % (5, 'Guido van Rossum')
'Guido'

Python Simple Conversion:

The simple conversion, with only a conversion type, is really easy to use:

>>> 'Using str: %s' % 99L
'Using str: 99'

>>> 'Using repr: %r' % 99L
'Using repr: 99L'

>>> 'Price of eggs: $%d' % 42
'Price of eggs: $42'

>>> 'Hexadecimal price of eggs: %x' % 42
'Hexadecimal price of eggs: 2a'

>>> from math import pi
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'

>>> 'Very inexact estimate of pi: %i' % pi
'Very inexact estimate of pi: 3'

Tuesday, June 30, 2009


Python String Formatting Conversion Types :

Conversion                      Type Meaning

d, i   Signed integer decimal
o   Unsigned octal
u   Unsigned decimal
x           Unsigned hexadecimal (lowercase)
X   Unsigned hexadecimal (uppercase)
e   Floating-point exponential format (lowercase)
E   Floating-point exponential format (uppercase)
f, F   Floating-point decimal format
g   Same as e if exponent is greater than –4 or less than precision
G   Same as E if exponent is greater than –4 or less than precision;
c   Single character (accepts an integer or a single character string) 
r   String (converts any Python object using repr)
s           String (converts any Python object using str)

Monday, May 25, 2009

Concatenating Strings : 

>> "Let's say " '"Hello, world!"'

Simply written two strings, one after the other, and Python automatically concate-nates them (makes them into one string).

This mechanism isn’t used very often, but it can be useful at times.

However, it works only when you actually write both strings at the same time, directly following one another:

>>> x = "Hello, "

>>> y = "world!"

>>> x y

SyntaxError: invalid syntax

It is just a special way of writing strings, not a general method of concat-enating them.

How, then, do you concatenate strings? Just like you add numbers:

>>> "Hello, " + "world!"

'Hello, world!'

>>> x = "Hello, "

>>> y = "world!"

>>> x + y

'Hello, world!'

Single-Quoted Strings and Escaping Quotes :

Strings are values, just as numbers are:

>>> "Hello, world!"

'Hello, world!'

There is one thing that may be a bit surprising about this example, though:

When Python printed out our string, it used single quotes, whereas we used double quotes.

What’s the difference? Actually, there is no difference:

>>> 'Hello, world!'

'Hello, world!'

Here, we use single quotes, and the result is the same. So why allow both?

Because in some cases it may be useful:

>>> "Let's go!"

"Let's go!"

>>> '"Hello, world!" she said'

'"Hello, world!" she said'

Python Strings :

The raw_input and "Hello, " + name + "!"

Let’s go for the "Hello" part first and leave raw_input for later.

The first program in this chapter was simply print "Hello, world!"

But what is "Hello, world!"? It’s called a string (as in “a string of characters”).

Strings are found in almost every useful, real-world Python program and have many uses.

Their main use is to represent bits of text, such as the exclamation “Hello, world!”

Wednesday, April 22, 2009

Running Python Scripts from a Command Prompt :

Actually, there are several ways to run your programs. First, let’s assume that you have a DOS 
window or a UNIX shell prompt before you, and that the directory containing the Python exe-
cutable (called python.exe in Windows, and python in UNIX) or the directory containing the 
executable (in Windows) has been put in your PATH environment variable.

Also, let’s assume that your script from the previous section (hello.py) is in the current directory. Then you can execute your script with the following command in

Windows: C:\>python hello.py

OR

UNIX: $ python hello.py

As you can see, the command is the same. Only the system prompt changes.

Note : If you don’t want to mess with environment variables, you can simply specify the full path of the Python interpreter.

In Windows, you might do something like this: C:\>C:\Python25\python hello.py

Saving and Executing Your Programs : 

First of all, you need a text editor, preferably one intended for programming. (If you use 
something like Microsoft Word, which I don’t really recommend, be sure to save your code as 
plain text.)

With IDLE, you can simply create a new editor window with File >> New Window.

Another window appears, without an interactive prompt.

Whew! Start by entering the following:

print "Hello, world!"

Now select File >> Save to save your program (which is, in fact, a plain text file).

Be sure to put it somewhere where you can find it later. You might want to create a directory where you put all your Python projects, such as C:\python in Windows.

In a UNIX environment, you might use a directory like ~/python. Give your file any reasonable name, such as hello.py. The .py ending is important.

Tuesday, March 31, 2009

Python Lexical Structure :

Following is a simple Python program. It shows the basic structure of many Python scripts follows: 

  1. Initialize variables (lines 1 – 3). 

  2. Do some processing (lines 4 – 5).

  3. Make decisions and perform actions based on those decisions (lines 6 – 10). 

name = “Jim”

age = 42

highschoolGPA = 3.89

enteredName = raw_input(“Enter your name: “)
   
print “\n\n”
   
if name == “Jim”:
  print “Your age is “, age
  print “You had a”, highschoolGPA, “GPA in high school”
  if (highschoolGPA > 3):

  print “You had better than a 3.0 GPA...good job!”  


Tuesday, January 13, 2009

Python Number and Expressions :

The interactive Python interpreter can be used as a powerful calculator.

Try the following : >>> 2 + 2

This should give you the answer 4. That wasn’t too hard.

Well, what about this : >>> 53672 + 235253 = 288925

Still not impressed? Admittedly, this is pretty standard stuff. (I’ll assume that you’ve used 
a calculator enough to know the difference between 1+2*3 and (1+2)*3.)

All the usual arith- metic operators work as expected—almost. There is one potential trap here, and that is integer division (in Python versions prior to 3.0) : >>> 1/2 = 0

Python Interactive Interpreter :

When you start up Python, you get a prompt similar to the following:

Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 

[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin.

Type "help", "copyright", "credits" or "license" for more information. >>> 

■Note: The exact appearance of the interpreter and its error messages will depend on which version you are using.

It’s an interactive Python interpreter.

Just try the following :

>>> print "Hello, world!"

When you press the Enter key, the following output appears : Hello, world! >>>

Data Science - 103 (Kapil Sharma)

Cloud Computing Basic:-  In this the PC is on service provider data center and secuirty maintenance and upgrades are done by the service pro...