Introduction to Python


Contents

How to install Python on Linux
How to run a Python program
The basic of Python codes
Data expressions
Built-in functions in Python
Interactive input/output
Conditional statement for Python
Loop structure for Python
Creating your own functions
Python modules
Reading/writing files for Python
Error handling code
Object Oriented Programming (OOP)
Tkinter package for GNU programming


Object Oriented Programming (OOP)

Here is an example of the object oriented program for python. The method, __init__(), means to perform when being made an instance. (Note that you have to use an underscore twice.)
class Human(object):
   def __init__(self,name): #Object's variable
     self.name=name
   def intro(self): #Object's method
     print "My name is %s" % self.name

mike = Human("Mike")
don = Human("Don")

print mike.name
mike.intro()
don.intro()
$ Mike
$ My name is Mike
$ My name is Don

Inheritance is an important concept for OOP. The following is a simple example of inheritance of the above code.
class Human(object):
   def __init__(self,name):
     self.name=name
   def intro(self):
     print "My name is %s" % self.name

class MetaHuman(Human):
   def think(self):
     print "%s is a philosopher..." % self.name

mike = Human("Mike")
don = MetaHuman("Don")

mike.intro()
don.think()
$ My name is Mike
$ Don is a philosopher...



Back to Electronics Page

Back to Hiro's Physics Main