Wednesday, February 29, 2012

Variables in Python

Variable is a place holder for Numbers or Characters, its like a temporary storage for any value.

How do we create a variable in Python?

For creating a variable in python, we define a letter and assign a value to it, for assigning we use the "=" operator, now this value can be defined by you or an user who runs the program.

lets look at an example, open the IDLE and type:

>>> x = 5
>>> x + 10
15
>>>

So here, we defined the value of variable x as 5, and instead of typing 5+10, we typed x + 10, and it gave a reult as 15.

Lets make a quick small program:

Open the IDLE, and Click on File and then click on new, so we get the Python's test editor, and then type

=========================

x = 15
y = 20
print "x = 15"
print "y = 20"
print "The addition of x and y is", x + y

=========================

Save this as add.py

and run the program.

Output:
=========================

>>>
x = 15
y = 20
The addition of x and y is 35
>>> 

=========================

Now we know how to use a variable in Python.

Hope this has been informative for you, and dont forget to check my next post.

Thank You!

0 comments:

Post a Comment