New to Python check this

13 Oct

First download python from here  http://www.python.org/download/.

After installing open python in command line.

Try this one liners

>>> print "Hello, World!"
>>> 1 + 1
2
>>> 18294+449566
467860
(These are additions)
>>> 6-5
1
(Subtraction)
>>> 5**2
25
(Exponentials e.g. this one is 5 squared)

>>> print "1 + 2 is an addition"
1 + 2 is an addition
(the print statement, which writes something onscreen)

Variables

v = 1
print "The value of v is now", v
v = v + 1
print "v now equals itself plus one, making it worth", v
v = 51
word1 = "Good"
word2 = "Morning"
word3 = "to you too!"
print word1, word2

Loops

a = 0
while a < 10:
    a = a + 1
    print a
if a > 5:
    print "This shouldn't happen."
else:
    print "This should happen."

NOTE:

Indentation

One other point is that the code to be executed if the conditions are met, MUST BE INDENTED. That means that if you want to loop thenext five lines with a ‘while’ loop, you must put a set number of spaces at the beginning of each of the next five lines.

Functions

function_name(parameters)

Define your own function like this

def hello():
    print "hello"
    return 1234


Advertisements

One Response to “New to Python check this”

  1. brittoc 10/15/2010 at 9:30 am #

    more detail
    http://www.sthurlow.com/python/lesson01/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: