+
Python
MD.Rasheduzzaman ID:0910647044 MD. Mahabub Akram ID:0910
Introduction
Python is created in 1990 by Guido van Rossum
Python is a well worked out coherent dynamic programming language Designed to be easy to learn and master
1. Clean, clear syntax
2. very few keywords
Highly portable
1. runs almost anywhere
2. Uses machine independent byte-codes
Features
Clean syntax plus high-level data type
1.leads to fast coding
Uses white-space to delimit blocks
Variables do not need declaration
1. although not a type-less language
Productivity
Reduce development time
1.code is 2-10x shorter that C, C++, Java
Improved program maintenance
1.code is extremely readable
Less training 1.language is very easy to learn
Usability
Rapid prototyping
Web scripting Throw-way, ad hoc programming Steering scientific programming Extension language XML processing Database application
GUI application
A Glue language
Python vs. Java
Code 5-10 times more concise
Dynamic typing Much quicker development
1.no compilation phase
2.less typing
Python is slower but development is so much faster Python can be used with Java: Jython
Python Basic
Variable need no declaration
>>>a=1 >>>a 1
Variable name alone is an expression, so the result is printed Variables must be created before they can be used
>>>b Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> b NameError: name 'b' is not defined
Variables and Types
Objects always have a type
>>>a=1 >>>type(a) <type int> >>>a=hello >>>type(a) <type string>
Simple Data types
String
1.May hold any data, including embedded NULLs 2.Declared using either single, double, or triple quotes
>>> s=Hi there
>>>s Hi there >>>s=Embedded quote >>>s Embedded qoute
Simple Data Types
Integer object implemented using C longs
Float types implemented using C doubles Long Integers have unlimited size
1.limited only be available memory
>>>long = 1L << 64 >> long ** 5 2135987035920910082395021706169552114602704522 3566527699470416078222197257806405500229620869 36576L
High Level Data Types
Lists hold a sequence of items
1.May hold any object 2. Declared using square brackets
>>>l = [] #an empty list >>>l.append(1) >>>l.append(Hi there); >>>len(l) 2 >>>l [1, Hi there]
High Level Data Types
Dictionaries hold key-value pairs
1.Often called maps or hashes. Implemented using hash-table 2.Keys can any immutable objects, values
3.Declared using braces
>>> d={} >>d[0]=Hi there >>d[foo]=1 >>>len(d) 2 >>>d[foo] 1
Blocks
Blocks are delimited by indentation
1.Colon used to start a block 2.Tabs or spaces may be used
>>> if 1:
print true true
Looping
The for statement loops over sequences
>>> for ch in hello: print ch h e l l o >>> for i in range(3): print i 1 2 3
Functions
Functions are defined with the def statement:
>>>def foo(bar): return bar >>>
This defines a trivial function named foo that takes a single parameter bar The function object can be called:
>>>foo(3) 3
Class
Class are defined using the class statement
>>>class Foo: def __init__(self): self.member=1 def getMamber(self): return self.member >>>
The constructor has a special name __init__,while a destructor uses __del__ The self parameter is the instance Classes are instantiated using call syntax
>>>f=Foo() >>>f.getMember()
Modules
Most of pythons power comes from modules
Modules can be implemented either in Python, or in C/C++ Import statement makes a module available
>>>import string
>>> string.join( [Hi, there] ) Hi there >>>
Thank You