How variable work in Pyton

July 31, 2010 at 9:40 am (Python)

Now lets enter into the programming.

The first thing that one has to know is how to define variables, so that those variables can be used in the programs in future.

In python, there is no need to declare a variable, Instead we define one whenever needed. python knows what operations suit for the values in that variable.

Lets make it known by writing some examples

In the python or IDLE prompt you can check them easily.

>>> a=10
>>> b=20
>>> a+b
30
>>>

The above variables are used a integers

>>> a=10+2j
>>> a.real
10.0
>>> a.imag
2.0
>>>

The above shows that we can use the concept of complex numbers too in our program.

>>> a='mohi'
>>> b=' devil'
>>> a+b
'mohi devil'
>>>

The same ‘+’ operator finds that the variable is a string and performs string concatenation.

>>> a=2
>>> a*3
6

multiplication operation is done for integers and for strings, look below.

>>> a='test '
>>> a*3
'test test test '

>>> a='test '
>>> (a*3).upper().strip()
'TEST TEST TEST'

You can change the cases too in a variable.

Now lets look at slicing concept

>>> a="lets check this"
>>> a[5:10]
'check'
>>>

The initial position starts from zero.

Now compare the flexibility of python. Isn’t it awesome?

Learn it, Work on it, Develop yourself :)

Advertisements

1 Comment

  1. Helen Dangote said,

    I hear alot about Python language but have not found the time to learn it. I will try it out this summer holiday. If there’s no need to define variables then that is a big plus.

    Thanks again 🙂

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: