Thursday, August 29, 2013

Variables

What are variables?
A variable is something with a value which may change!
Its value may vary throughout the program!

People who are familiar with C and C++, will know the method to initialize a variable.
In C and C++, it is done as follows:
If i want to initialize a variable x with the value 3 which is of integer type,it is done as:
int x=3;
this stores the value 3 in the variable x which of on Integer type.

In Python, its a lot easier.
While initializing a variable with any value, we need not specify the data type of the variable.
Meaning, for the above example, the data type 'int' need not be specified.
In Python, we just have to type x=3!!
the Python interpreter itself will recognize the data type of the value entered and will initialize it to the variable!

Variables are used so that retrieving its value will be easier in the program.

Say we initialize the vaule of x as 3 by typing x=3



Now, This value of x can be made use of in any line of the program!
We can implement it this way..

To maybe print the contents of the variable x, we should type the following:


We can also perfom add,sub,mul and divide operations on the variable x as follows and also display the result:



Till now, we have only initialized variables with int value.
So type(x) would give us INT.


We can also store strings into variables, as given below:
name="XYZ"
This initializes the variable name with the value XYZ.
Strings must always be typed in quotes(" ").

So the value of the name variable can be displayed as follows:



This value of name can be accessed in any line of the program.

We'll talk more about variables in the coming Posts.


Data Types

For those of you who know c and c++, this will be a repeatation.
For the others, you will learn the different data types one can use in Python.

In our previous blog, we solved a few mathematical equations with numbers.

Every number will be of a certain Data Type.
To know a few,
1. INT data type
2. FLOAT data type
3. BOOL data type.

The INT data type is the integer data type.
It consists of whole numbers. That is numbers without a fractional part.
For eg: 2,3,4,5 are all INT type.

The FLOAT data type consists of numbers with fractional part.
For eg: 2.7,2.1,3.7.

The BOOL data type is used to check is the expression written in the paranthesis results in a true or a false value.
bool(<expression to be checked>)
For eg: bool(2<3) will return a TRUE value.
bool(2!=2) will return a FALSE value.
These can be used when some part of code must be executed only at certain conditions.

To check what data type a number belongs to, one can use the 'type' command in Python.

type(2) gives 'int' as the result.
type(3.7) gives float as the result.



The bool data type can be made use of in the following way:


NOTE: bool commands returns only TRUE or FALSE as the output.

These are the general Data Types in Python.
You will learn more Data Types in the following Blogs!
Stay Tuned!

Wednesday, August 28, 2013

Simple Equations

Now that we know how to print one line sentences on the terminal,
lets try and do something more.

Python can also be used to solve mathematical equations.
Lets start with the basic equations of all!
Lets use Python as a Calculator!

2+3 gives 5 on a Calculator,
Similarly "2+3" on the Python terminal gives 5 too!!
 to get this, we just type 
">>2+3"



Now press enter and check the output!



You can also try out various other simple equations as


You might have a doubt when it comes to the last two outputs..
Why 2 for 6/3 and 2.0 for 6.0/3.0.
This will be explained in the following Blogs.

Hello World

Once u have installed the terminal, u can try out your codes!

The prompt in the Idle looks like ">>>"
Next to this is where u start typing.

Now,Lets try out a simple code.
To print "Hello World"

To do this, we use the "print" command.
All you have to do is type 'print "Hello World"' next to the prompt.

Like this,



and then press the Enter key..
and there'll u have it!
"Hello World" will be printed on the Emulator.


So, what did we learn from this?

The command "Print" followed by a string in quotes(" ") prints the string on the Terminal.
Its as easy as this!
Have a try!

Basics

To run codes written in Python,
First u need to download the Python terminal Emulator.
There are two widely used emulator versions, 3.3.2 and 2.7.5.
The 2.7.5 version is simpler to use for learners.

Here is the link to download the Emulator:
http://www.python.org/getit/

Once u download that, you can use the Idle(Python GUI) to type your Python codes.
It'll look something like this.





Now you are all set to try out your Python codes!!!