Skip to main content

Posts

Showing posts from July, 2014

Advance Object Oriented programming in Python

The following program will illustrates the method overriding concept onf OOP. Here parent and child classes have defined with the same method name and signature. According to context the method would be executed. The actual meaning of overriding means ignoring the parent method when both have same method signature, and executing the derived class method. ===== METHOD OVERRIDING SAMPLE PROGRAM ===== class FirstClass: #define the super class def setdata(self, value): # define methods self.data = value # ‘self’ refers to an instance def display(self): print self.data class SecondClass(FirstClass): # inherits from FirstClass def display(self): # redefines display print 'Current Data = %s' % self.data x=FirstClass() # instance of FirstClass y=SecondClass() # instance of SecondClass x.setdata('Before Method Overloading') y.setdata('After Method Overloading') x.display() y.display() ===== Static and Class Method Sample Prog...

Regular Expressions in Python

This session about Python Regular expressions how we can work with patterns and the specific methods available in re module. match() group(), groups() search() compile() sub() Regular expression sample in Python Here is the sample for match function use. #!/usr/bin/python """ This program illustrates the usage of match, group functions in re module This also shows how to use re flags """ import re line = "Python Orientation course helps professionals fish best opportunities" m = re.match( r'(.*) helps (.*?) .*', line, re.I) if m: print "m.group() : ", m.group() print "m.group(1) : ", m.group(1) print "m.group(2) : ", m.group(2) else: print "Don't have match!!" The output >>> execfile('c:/pybin/rematch.py') m.group() : Python Orientation course helps professionals fish best opportunities m.group(1) : Python Orientation course m.group(2) : ...

Functions in Python

Heere we have experimenting with functions in Python, Functions can be defined for specific task, functions are prepared to reuse them. modular programming can be defined as breaking the bigger task into chunks of blocks this can be achived with the Python functions. Over all we are going to know in depth knowledge on how Python function is powerful then other structure programs. We have seen some built-in functions which you don't need to import any modules. str(), int(), float(), bool() -- type definitions type(), id(), dir() --  introspecting the functions len(), range() -- sequance collection related functions print(), input(), raw_input() -- input output functions Python Function overview Function definition  Calling Function in assignment Adding DocString to Function  Function Execution – Scope of variables The import and reload Defining inner functions Lambda functions Function definition structure 1. Procedure - which do not ha...

Importing modules

What is Python Module? When a Python shell starts it only has access to a basic python functions (“int”, “dict”, “len”, “sum”, “range”, ...) “Modules” that contain additional functionality more methods, more reusability and more sharability. Use “import” keyword to tell the Python shell to load a module. import os, sys Namespaces are one honking great idea -- let's do more of those! (Zen of Python) import this Why importing modules in Python? The major advantage comes when you split your program into several files for easier maintenance. How to import modules in Python A module can be imported by another program to make use of its functionality. This is how we can use the Python standard library as well You can import multiple modules. import module1[, module2[,... moduleN] Normally after module import we can use its containing functions by calling them with refering with the module name followed by period or dot(.) and the function/procedure name. import...

Files IO & Pickles

How to make this FILE process in Python?  The Standard Input, Standard Output are the two fileobjects genrally accessible from the operating system provided interfaces which allows us to change files. How to redirect this output... normally output is coming from print command in the Python. Can you send the data/text to a file? Yes we can using file object. File IO Process in Python The file open modes Python file open modes are same as C language by default it will be open in read mode. The open() method returns a file object or file pointing reference.  Syntax:  fileobject = open(file_name [, access_mode][, buffering])  where basic file operational mode can be 'r' (read), 'w'(write) or 'a'(append).  The default mode is 'r' that is read mode and the file can be ANSI Text file or binary file (b). They can be opened in combined mode ‘rb+’  That every file that you opened in the code must be closed. The close() method closes an ope...

Python Sequence Data Types: List, tuple

Python Datatypes by SujithKumar from Sujith Kumar In this I would like to discuss about Python data types. Usually Python don't required any variable declarations with datatype but we should aware when we are dealing with the variable values and how it stores what type of data. Let's jump into our exploration on Python data types. Python Mutable vs Immutable datatypes In simple way to understand this using id() function that returns address of the variables. Mutable data type means the variable stores the data at one address even different operations (+,-,/,*,%) on the variable. The content of objects of immutable types cannot be changed after they are created. Mutable objects bytearray list set dict Immutable objects int float long complex str tuple frozen set Lets test a int data type and a list data type to evaluate the immutable values with int data type while doing arthematic operation on it. Similarly list example shows how mutable t...

Python Classes and Objects

********  OBJECT ORIENTED PROGRAMMING IN PYTHON ******** Basics of Object Oriented Programming in Python from Sujith Kumar OBJECT: Objects are the basic run-time entities in an object-oriented system, They may represent a person, a place, a bank account, a table of data or any item that the program must handle. CLASS: A class is a special data type which defines how to build a certain kind of object.The class also stores some data items that are shared by all the instances of this class class student: “““A class representing a student ””” def __init__(self , n, a): self.full_name = n self.age = a def get_age(self): #Method return self.age The following program shows the How to initialize the members in Class, How to create Instances for particular Object and finally How to print existed data class Student: # Initializing the variables def __init__(self,name,age): self.fullname=name self.sage=age # Display the entered d...