+
Python
[Link] 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
[Link] 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
[Link] is 2-10x shorter that C, C++, Java
Improved program maintenance
[Link] is extremely readable
Less training [Link] 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
[Link] compilation phase
[Link] 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
[Link] hold any data, including embedded NULLs [Link] 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
[Link] only be available memory
>>>long = 1L << 64 >> long ** 5 2135987035920910082395021706169552114602704522 3566527699470416078222197257806405500229620869 36576L
High Level Data Types
Lists hold a sequence of items
[Link] hold any object 2. Declared using square brackets
>>>l = [] #an empty list >>>[Link](1) >>>[Link](Hi there); >>>len(l) 2 >>>l [1, Hi there]
High Level Data Types
Dictionaries hold key-value pairs
[Link] called maps or hashes. Implemented using hash-table [Link] can any immutable objects, values
[Link] using braces
>>> d={} >>d[0]=Hi there >>d[foo]=1 >>>len(d) 2 >>>d[foo] 1
Blocks
Blocks are delimited by indentation
[Link] used to start a block [Link] 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): [Link]=1 def getMamber(self): return [Link] >>>
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() >>>[Link]()
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
>>> [Link]( [Hi, there] ) Hi there >>>
Thank You