Next Tutorial

Drupal 7 Tutorial Part 16: Drupal 7 Reports & Logs Explained

Installing Python Interpreter -- Python Basics -Tutorial 2

Submitted by anilsagar on Sun, 04/11/2010 - 01:20

Hi Everyone,

Its time to play with python and get your hands on most powerful programming language. Before going to say hello to python we need to install python interpreter. You can installation files for various operating systems from here.

http://www.python.org/download/ Installing on windows :

Step 1: Download latest windows installation package from the above mentioned link.

Step 2: Run the installation file and follow instructions.

Step 3: Tada!! your python interpreter is installed and ready to use.

Go to programs >> python >>IDLE python GUI

Its time to say hello to python :

>>>"Hello World!" It just pings you back with the same.

Lets check out something simple

>>>my_name_is = "anil"                     # initializing a variable

>>> if my_name_is: # a simple if condition to check variable exist with some value or not

print "My name is initialised!!"

Output: My name is initialised!!

How to comment code ?? # sign is used to write comments , you can see the example in the above code.

 

Lets use python as our calculator by executing simple commands:

>>> 5+5 10

>>> (20-5*2)/5 2

>>>7/3 # integer division returns floor value 2 >>> 7/-3 -3

 

Lets assign some values to variables: >>> length = 20

>>> breadth = 10

>>>length*breadth 200

 

Multiple assignments in a single line: >>> x = y = z = 0

>>>x 0

>>>y 0

>>>z 0

Some floating point operations:

>>>7.0 / 2 3.5

 

Complex numbers also supported. imaginary numbers are denoted by J or j.

 

Complex number can be denoted using complex function as complex(real, imag) . You can extract real and imag from a complex variable like z as z.real, z.imag.

>>>1j * 1J -1+0J

>>> 3+3J*3 3+9J

>>>1J * complex(0,1) -1+0j

>>> z= 3 + 5J

>>>z.real 3

>>>z.imag 5

In interactive mode of python last printed value is stored in _ variable. Lets see an example.

>> >a =4

>>>a 4 #Last printed value 4 is assigned to _

>>> 10+_ = 14

# _ is replaced by 4 so 10 + 4 = 14

Strings: Python can also manipulate strings besides numbers. Strings are enclosed in either single quote or double quotes. Some examples:

>>>"Hello World"

>>>"I can't"

>>>"\"Hey\" Look at me"

Strings can be concatenated using + operator and repeated using * operator.

>>>"Cool"+"Buddy"

CoolBuddy

>>>"word"*4

wordwordwordword

Strings can be sub scripted like in C with indexes.

>>>alpha = "abcdef"

>>>alpha[2]

c

So the first character of string has index of 0. so a -- 0 b -- 1 c -- 2 d -- 3 e -- 4 f -- 5

>>>alpha[0:2]

ab

>>>alpha[2:4]

cd

>>>alpha[:2]

ab

>>>alpha[2:] cdef

Unlike c strings python strings cannot be changed. So the following operations are not possible.

>>>alpha[0] = A

Traceback (most recent call last): File "
", line 1,

in alpha[0] = 'A' TypeError: 'str' object does not support item assignment'

 

However we can use the strings to form new strings >>> alpha[0:] + "ghi"

abcdefghi I

 

ndices can be negative numbers to start from right.

>>>alpha[-1]

f

>>>alpha[-1:-3]

Y ou can find the length of a string using len function.

>>>len(alpha)

6

 

Cheers,

Anil Sagar

©2010 AnilSagar. All rights reserved. Drupal theme by Kiwi Themes.