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!

Tuesday, February 28, 2012

Executing a Python Program

Well from the last Tutorial, we learnt that Interactive Mode is where Python executes a given function or code on pressing the Enter Key,

But now we will see how to make a small program, and save it, and also how to execute the program.

To write a program, we either use a notepad or the inbuilt Python Text Editor... either way it works.

I hope everyone knows how to open a notepad, so I am skipping on it, The important thing is whenever we write a program in python, it has to be saved with an extension of .py

So if we are using the notepad, we write the program and save it as "name.py"

Now lets see how we write in the Inbuilt Python Text Editor, for this we open up the Python IDLE(the GUI) next we click on File, and then New(Shortcut Ctrl+N). This opens up the Text-Editor of Python, where we can write our program, but even here, after writing the program we need to save the program with a .py extension, or else python would not understand what we are trying to execute while executing.

We can save our program anywhere we want, as long as we have saved with as .py extension, but to make things easy, I create a folder on C Drive, where the Python is stored, and usually name it as "python programs" and all programs are saved inside the folder.

Lets make a small program.

In the Text-Editor type:

print "Program to check if i can execute this"
print "2+2: ", 2+2
print "I think If i have read the post correctly, it should execute without any error"

Now save the code, Click on File on the Text-Editor, Click on Save, and While Saving Dont forget to add the .py extension, lets save this as check.py.

To execute the program, we need to click on Run>>Run Module, For Older Versions, it would be Edit>>Run Script, Or Else You can use the shortcut which is hit the F5 key, this will execute the program.

After executing, we will see the display on the IDLE as

>>>
Program to check if i can execute this
2+2: 4
I think If i have read the post correctly, it should execute without any error
>>>

So I hope now we know how to Create a program and save the program, and also how to execute it.

I hope You Enjoyed this, and Dont forget to check the next post.

Thank You!

Modes of Python

Python has two modes:

1. IDLE -- The Graphical User Interface

2. CLI -- Python Command Line Interface

IDLE is used mostly for programming. Both IDLE and CLI mode has a prompt which looks like ">>>"

">>>" This symbol is the python way of telling you that you are in the Interactive Mode. In Interactive Mode, whatever you type is executed immediately, when on pressing the Enter key.

Ex:

>>>
>>> 2+2
4
>>>
>>> print "I am Learning Python"
I am Learning Python
>>>


So we see here that whatever we type in the ">>>" prompt, on pressing the Enter key, returns the value, and we dont need to save the program or code we typed in for executing the codes.

I hope This has been Informative for you, and Dont forget to check the Next Post..

Thank You!


Monday, February 27, 2012

Head Start For Python


What is Python?

Python is a High-Level Programming Language, Because of its strong structuring contents, it can be used to write clear, logical applications for small and large tasks.

What is Python’s Official Website?



Where can I download the python?

There are different versions of Python, this TuT will be based on 2 series, and i will use Python 2.7 for the TuTs, all the 2 series will work fine with the TuTs.


Make a Note of these important website before you start with Python.

If You have any problem, regarding download of python, leave a comment.

Please also mention your Operating System, and Hardware Configuration of the Computer, so that I can help you in a better way!

Thank You!