Thursday, September 15, 2011

Collections

Before discussiing about the Collections in Python ,we all must know the answER of one very important question ,ie; What exactly we can do with a Collection?

well there are a some basic operations which normally perform using collections :
1. Add objects to the collection.
2. Remove objects from the collection.
3. Find out if an object (or group of objects) is in the collection.
4. Retrieve an object from the collection (without removing it).
5. Iterate through the collection, looking at each element (object) one
after another.


So without wasting any time let us directly discuss the types of Collection available in Python:
1. List
2. Tuple
3. Dictionary or Hash
4. Array or Vector
5. Stack
6. Bag
7. Set

There are several merits and demerits of using different Collections which we will be discussing in our next session with programs..
Have a good day...!

Wednesday, September 14, 2011

Boolean Operators

After dealing with String and Arithematic operators ,its time to cover some very important operators , Boolean Operators

Boolean values are sometimes known as "truth values" because they are used to test whether something is true or not. example we are having two Strings we want to know weather the String values provided are equal or not, if they are equal we transfer pur control from one location to another.

so like in all other programming languages, in Python also we are having all basic Boolean operators::

[A and B] ----> AND ---> True if A,B are both True, False otherwise.

[A or B] ----> OR ---> True if either or both of A,B are true. False if both A and B are false

[A == B] ----> Equality ---> True if A is equal to B

[A != B
A <> B] ----> Inequality --->True if A is NOT equal to B.

[not B] ----> Negation ---> True if B is not True


Note: the last one operates on a single value, the others all compare two values.

Working with Strings

In Python, strings can be represented in several ways:

With single quotes: 'Here is a string'

With double quotes: "Here is a very similar string"
With triple double quotes: """ Here is a very long string that can
if we wish span several lines and Python will
preserve the lines as we type them..."""
One special use of the latter form is to build in documentation for Python functions that we create ourselves - we'll see this later.

We can access the individual characters in a string by treating it as an array of characters. There are also usually some operations provided by the programming language to help
us manipulate strings - find a sub string, join two strings, copy one to another etc.

String operators

Operator Description
S1 + S2 Concatenation of S1 and S2
S1 * N N repetitions of S1


now lets create an example using the above mentioned string operators:

StringOperators.py
-------------------------
s1=' again'
s2= ' and again'
print (s1+ s2 ) # string concatenation

print (s1 * 3) # string repetition
print(' Again ' + (' and again' * 3) )
print(s1+(s2*3))


output:
again and again
again again again
Again and again and again and again
again and again and again and again


Notice that the last two examples produced the same output.

Modulo and Exponential arithematic operation


In last session i left out the two most important arithematic operators Modulo and the Exponential operator

M % N Modulo: find the remainder of M divided by N

M**N Exponentiation: M to the power N


so now we ill be developing a python program ModuloExponential.py which covers same.

so without wasting anymore time ,right click one the project icon ---> new --> PyDev module---> enter the name of module as ModuloExponential and click on OK.. now we are ready


ModuloExponential.py
-------------------

i1=3 # create an integer and assign it to i1
i2= 4 # create an integer and assign it to i1
i3 = i1**i2 # assign the result of 3 to the power 4 to i3

print(i3)

i4= i2 % i1 # assign the result of 4 mod 3 to i4
print(i4)

Output:
81
1

Was very simple program,hope u all got it easily..
now letz discuss some shortcut operators


Operator Example Description
M += N M = M + N
M -= N M = M - N
M *= N M = M * N
M /= N M = M / N
M %= N M = M % N

Diving into Python


Learning different programming languages ,technologies is like a passion for me and same i am doing this with Python as well ,thanks to Tata Consultancy Services for making us dive in
into Python straight away..
As i know except few most of you are from other streams , so considering yourself as Beginners itz better to start with very basics of Python.

The program which we are discussing now covers Arithematic operators and printing the complex calculated results in desired form..

Arithematic Operators we are available with are:
----------------------------------------------------------------
a) add(+)
b) subtract (-)
c) multiply (*)
d) divide (/)


let say the the name of our new python project is "PythonArithematic" , let me repeat how we can create this project in MyEclipse 8.6, hope you are all done with integration of
MyEclipse and PyDev plugin...if not please go throgh previous posts..

Creating PythonArithematic project::
--------------------------------------------------------------------
1. Go to File --> New ---> PyDev Project to start a wizard.

2. In the next window that appears, enter the name of project as "PythonArithemati" and select "python" and 3.0"; as the type. Make sure "create default 'src' folder and add it to the pythonpath?" is selected. Click Finish.

3. Create a new module ::
Select the project "PythonArithemati" which we just created and go to File ---> New ---> PyDev Module. This will launch a new PyDev Module Wizard where we should enter a name for your module as "Arithematic.py" . Leave the Package field blank and select Finish.

4. The file should be opened in the open space in the center of the workspace-the Editor view. (If not, right click on the Arithematic.py icon and select Open.) You will see a tab with the name of your file.


so ..wat next.... you are now availabe with everything ready ...letzz do some arithematic operations..

Arithematic.py
------------------
a=2
b=3
c=a+b
print(c) #addition
c= a-b
print(c) #subtraction

c=a*b
print(c) #multiplication

c= b/a #division
print(c)

#printing the resuls in desired format
print("the sum of %d and %d is %d" %(a,b,a+b))

Output:
---------
5
-1
6
1.5
the sum of 2 and 3 is 5



So we finished up performing addition,subtraction,multiplication and division operation.

>>>print("the sum of %d and %d is %d" %(a,b,a+b))
In this command the format string contains '%' markers within it. The letter 'd' after the % tells Python that a 'decimal number' should be placed there.
The values to fill in the markers are obtained from the values inside the bracketed expression following the % sign on its own.

There are other letters that can be placed after the % markers. Some of these include:

%s - for string
%x - for hexadecimal number
%0.2f - for a real number with a maximum of 2 decimal places
%04d - pad the number out to 4 digits with 0's
The Python documentation will give lots more...I will suggest everyone to please go through Python documentaion .

Tuesday, September 13, 2011

HelloWorld.py

Writing Fist Python program

Like all tutorials we too gonna start with HelloWorld.py


Like in Java we save files with ".java" as extension ,Python files are saved with .py as extension.

For writing Python we need to switch our perspective from java to PyDev ,we can do this by:

Go to Window → Open Perspective → Other and choose PyDev, then click OK. If you look at the upper right corner you will see that the perspective has changed from "Java" to "PyDev".



Creating PythonTest project::
--------------------------------------------------------------------
1. Go to File → New → PyDev Project to start a wizard.

2. In the next window that appears, enter the name of project as "PythonTest" and select "python" and 3.0"; as the type. Make sure "create default 'src' folder and add it to the pythonpath?" is selected. Click Finish.

3. Create a new module ::
Select the project "PythonTest" which we just created and go to File → New → PyDev Module. This will launch a new PyDev Module Wizard where we should enter a name for your module as "HelloWorld" . Leave the Package field blank and select Finish.

4. The file should be opened in the open space in the center of the workspace-the Editor view. (If not, right click on the HelloWorld.py icon and select Open.) You will see a tab with the name of your file.

5. lets Write and run the program ::
Simply type print('Hello, World!') into the file. You may remove the default doc comment or leave it there; Python ignores it.

press ctrl+s or Right click on the file and select Save to save HelloWorld.py.

Finally, choose the HelloWorld.py icon, and go to Run → Run As → Python Run to run our program.
(A quicker alternative is to right-click on the HelloWorld.py icon, and select Run As → Python Run, or press Ctrl+F11.)


Everyone have a Look at the bottom of your screen at the Console view and you will see the message you told the computer to print.ie; Hello, World!


Congratulations everyone we have successfully configured our MyEclipse for the Python developement..... we ill start learning the Basics of Python from the next session.

Have a Good Day..Narendra Soni..!!

Setting up the environment for Python development (Windows)

i am a Java guy and working on Python is just like an adventure to my right now...so just to simplify my tasks m going for the IDE..could be done using Netbeans as well but i am using MyEclipse 8.6 here...hope u must be well aware of it..


Its always better to be updated rather then working on prior versions ,so without wasting any time lets directly move on to the Python development environment set up..

step-1: First of all download and install the latest version of Python(m using Python 3.2.2) from

step-1*: If u dont wannaa work using IDE ,then u need to set the Environment variables as PYTHONPATH to the Lib,Include of the Python installation directory.

Step-2: Now download the Python plugin( called as PyDev) for MyEclipse from within the MyEclipse.
Launch MyEclipse --->go to help-->Install new Software-->Enter http://pydev.org/updates in the Work with: field. --->Select the PyDev for option ----> Click "Next" and "OK" to continue installing PyDev. -->

Restart IDE to apply the complete updation.


Step-3: now we need to set up Python interpreter ..
Go to Window → Preferences. In the Preferences window, expand PyDev and select Interpreter-Python. ------> Click "New..." and type Python32 for the Interpreter name. For the Interpreter executable, browse to your copy of Python (C:\Program Files\Python32\python.exe), and press Open. ---> Click "OK .

The Interpreter is now set up so that the code you write can be interpreted for the computer to run. You are now ready to start running code..

Introduction to Python


Python is a dynamic, interpreted language. Source code does not declare the types of variables or parameters or methods. This makes the code very short and flexible, and you lose the compile-time type checking in the source code. Python tracks the types of all values at runtime.

Python code does not declare the types of variables -- just assign values to them and start working with it. Python raises a runtime error if the code tries to read from a variable that has not been given a value. Like C++ and Java, Python is case sensitive so "a" and "A" are different variables. The end of a line marks the end of a statement, so unlike C++ and Java, Python does not require a semicolon at the end of each statement.
Here Comments begin with a '#' and extend to the end of the line.