18-04-2011, 04:09 PM
[attachment=12320]
PYTHON
INTRODUCTION
Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python's elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
The first version was developed by GuidoVan Rossum in 1991
Why not Perl?
When compared to Perl, Python programs are definitely simpler, clearer, easier to write and hence more understandable and maintainable.
If you didn't know already, Perl is another extremely popular open source interpreted programming language.
Features of Python
Simple
Easy to Learn
Free and Open Source
High-level Language
Portable
Interpreted
Object Oriented
Extensible
Embeddable
Extensive Libraries
BASICS
Literal Constants
Numbers
1. Integers
2. floating point
3 . complex numbers
Strings
Variables
Identifier Naming
àRules
Data Types
Objects
In PYTHON we doesnot declare variables we will just assign to them and go
Operators
Note that you can evaluate the expressions given in the examples using the interpreter interactively. For example, to test the expression 2 + 3, use the interactive Python interpreter prompt:
>>> 2 + 3
5
>>> 3 * 5
15
>>>
Operators mainly used
Control Structures
The if statement
The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-block), else we process another block of statements (called the else-block). The else clause is optional
à elif(elseif)
The while Statement
The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what is called a looping statement. A while statement can have an optional else clause.
Control Sturctures
The for loop
The for..in statement is another looping statement which iterates over a sequence of objects i.e. go through each item in a sequence
Example
for i in range(1, 5):
print(i)
else:
print('The for loop is over')
Control Structures
The break Statement
The break statement is used to break out of a loop statement i.e. stop the execution of a looping statement, even if the loop condition has not become False or the sequence of items has not been completely iterated over.
The continue Statement
The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.
Functions
Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times. This is known as calling the function. We have already used many built-in functions such as len and range.
Functions are defined using the def keyword. After this keyword comes an identifier name for the function, followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the line. Next follows the block of statements that are part of this function.
EXAMPLE FOR FUNCTION
def sayHello():
print('Hello World!') # block belonging to the function
# End of function
sayHello() # call the function
FUNCTIONS
The return Statement
The return statement is used to return from a function i.e. break out of the function.
Local variables
Global variables
Data Structures
There are four built-in data structures in Python
1.list,
2.tuple,
3.dictionary and
4.set.
CLASSES AND OBJECTS
class Person:
pass # An empty block
p = Person()
We create a new class using the class statement and the name of the class. This is followed by an indented block of statements which form the body of the class. In this case, we have an empty block which is indicated using the pass statement.
Next, we create an object/instance of this class using the name of the class followed by a pair of parentheses.
OBJECT METHODS
classes/objects can have methods just like functions except that we have an extra self variable
class Person:
def sayHi(self):
print('Hello, how are you?')
p = Person()
p.sayHi()
The __init__ method
The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print('Hello, my name is', self.name)
p = Person('Swaroop')
p.sayHi()
Input Output
The input() function takes a string as argument and displays it to the user. Then it waits for the user to type something and press the return key. Once the user has entered, the input() function will then return that text
Print() function helps you to output the text,values of variables
Files
You can open and use files for reading or writing by creating an object of the file class and using its read, readline or write methods appropriately to read from or write to the file. The ability to read or write to the file depends on the mode you have specified for the file opening. Then finally, when you are finished with the file, you call the close method to tell Python that we are done using the file.
Example of File
poem = '''\
Programming is fun
When the work is done if you wanna
make your work also fun:
use Python!
'''
f = open('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
f = open('poem.txt') # if no mode is specified, 'r'ead mode is assumed default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print(line, end='')
f.close() # close the file
Handling Exceptions
We can handle exceptions using the try..except statement. We basically put our usual statements within the try-block and put all our error handlers in the except-block.
try:
text = input('Enter something --> ')
except EOFError:
print('Why did you do an EOF on me?')
except KeyboardInterrupt:
print('You cancelled the operation.')
else:
print('You entered {0}'.format(text))
Handling Exceptions
Try .. Finally
Suppose you are reading a file in your program. How do you ensure that the file object is closed properly whether or not an exception was raised? This can be done using the finally block
The with statement
Acquiring a resource in the try block and subsequently releasing the resource in the finally block is a common pattern. Hence, there is also a with statement
The difference here is that we are using the open function with the with statement - we leave the closing of the file to be done automatically by with open.
APPLICATIONS
Web and Internet Development
àCGI scripts
Database Access
àCustom and ODBC interfaces
Desktop GUIs
Scientific and Numeric
Network Programming
àSocket interface
Game and 3D Graphics
CONCLUSION
A language will become wide spread if it is simple easy to use and also serves its purpose well ,As PYTHON has all the above mentioned features it is accepted by most of the programmers…