Aug 27, 2010

Python

Python is an interpreted programming language. By just tuping python you can start programming.
When you type,
$python
It willl display as;
>>>
here you can type your program.
To exit from it, you can type Crlt+d or quit().

# is used to comment

Interpreter can be used as a calculator.
>>> (25+5)/5
6

The equal sign in used to assign values to a variable
>>>a=100 # assign 100 to a
>>>b=2*5 # assign 2*5 (10) to b
>>>a+b
110

complplex numbers are also supported, like
>>>complex(3,5)+complex(5,-5)
10+0j

In the interactive mode, the last printed output is asigned to the _ variable. So you can use,
>>>100+50
>>> _/3 # this work as 150/3
50
to forword last output to some other task.

Strings in python
>>>word="hello \n world"
>>>print word
hello
world

If you are deviding the string in to two parts you should use a \ like
>>>word="hello \
...world"
>>>echo word
hello world

String concatination
>>>word='A'+'Help'
>>>print word
AHelp

String repeat
>>>word='-Help-'
>>>print word*3
-HELP--HELP--HELP-

String indexing
>>> word='HELP'
>>>word[2]
'L'
Note: The string index is starting from [0].

Some special thing
>>>word='colombo'
>>>word[2]
'l'
>>>word[:2] # will give [0]and [1]
'co'
>>>word[2:] # will give [2], [3], [4]..
'lombo'
>>>word[:2]+word[2:]

No comments:

Post a Comment

Your comments are always welcome ...