Module 1
Module 1
1
© Nex-G Exuberant Solutions Pvt. Ltd.
Introduction
• Its design emphasizes code readability, and its syntax allows programmers to
express concepts in fewer lines of code than would be possible in languages such as
C++ or Java].
• The language provides constructs intended to enable clear programs on both a small
and large scale
• One of the friendly things about Python is that it allows you to type directly into the
interactive interpreter—the program that will be running your Python programs. You
can access the Python interpreter using a simple graphical interface called the
Interactive Development Environment (IDLE).
2
© Nex-G Exuberant Solutions Pvt. Ltd.
What is python
• Python's simple, easy to learn syntax emphasizes readability and therefore reduces the
cost of program maintenance.
• Python supports modules and packages, which encourages program modularity and
code reuse.
• The Python interpreter and the extensive standard library are available in source or
binary form without charge for all major platforms and can be freely distributed.
3
© Nex-G Exuberant Solutions Pvt. Ltd.
Is Python a “Scripting Language”?
• Generally “script” is used to describe a simple top-level file and “program” to refer to a
more sophisticated multiline application.
• Because the term “scripting language” has so many different meanings to different
observers, some would prefer that it not be applied to Python at all.
4
© Nex-G Exuberant Solutions Pvt. Ltd.
Properties of Python
Shell tools
• Python is a tool for coding operating-system-oriented scripts. Such programs are often
launched from console command lines and perform tasks such as processing text files
and launching other programs.
• Python programs can and do serve such roles, but this is just one of dozens of common
Python application domains. It is not just a better shell-script language.
5
© Nex-G Exuberant Solutions Pvt. Ltd.
Properties of Python
Control language
• To others, scripting refers to a “glue” layer used to control and direct (i.e., script) other
application components. Python programs are indeed often deployed in the context of
larger applications. For instance, to test hardware devices, Python programs may call out
to components that give low-level access to a device. Similarly, programs may run bits of
Python code at strategic points to support end-user product customization without the
need to ship and recompile the entire system’s source code.
• Python’s simplicity makes it a naturally flexible control tool. Technically, though, this is
also just a common Python role; many (perhaps most) Python programmers code
standalone scripts without ever using or knowing about any integrated components.
6
© Nex-G Exuberant Solutions Pvt. Ltd.
Properties of Python
Ease of use
• Probably the best way to think of the term “scripting language” is that it refers to a simple
language used for quickly coding tasks. This is especially true when the term is applied to
Python, which allows much faster program development than compiled languages like C+
+. Its rapid development cycle fosters an exploratory, incremental mode of programming
that has to be experienced to be appreciated.
7
© Nex-G Exuberant Solutions Pvt. Ltd.
Why Python?
• Software quality : Python makes it easy to write software that can be understood, reused, and
modified. By design, Python implements a deliberately simple and readable syntax and a highly
coherent programming model
• Developer productivity: Python is optimized for speed of development. It's easy to write
programs fast in Python, because the interpreter handles details you must code explicitly in more
complex, lower-level languages. Things such as type declarations, storage layout, memory
management, common task implementations, and build procedures are nowhere to be found in
Python scripts
• Program portability: Most Python programs run without modification on nearly every computer
system in use today on Windows, Linux.
• Component integration : Programs written in Python can be easily mixed with and can script
other components of a system.
8
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Use in various Fields
• Python code can invoke C/C++ libraries and can be called from C/C++.
For example, integrating a C library into Python enables Python to test and launch the
library's component, and embedding Python in a product enables onsite customizations to
be coded without having to recompile the entire product.
• It can integrate with Java and .Net components, and it can communicate over frameworks
such as COM. It also can interface with devices over serial ports, and can interact over
networks with interfaces like SOAP, XML-RPC, and COBRA.
9
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Use in various Fields
• SWIG and SIP code generators can automate much of the work needed to link compiled
components into Python.
10
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Use in various Fields
• For general database, Python gives us interfaces to all RDBS - Sybase, Oracle, MySQL,
PostgreSQL, and SQLite.
Python in GUI
• Python's simplicity and rapid turnaround make it a good match for graphical user interface
programming. Python comes with a object-oriented interface to the Tk GUI API
called tkinter which allows Python to implement portable GUIs with native look and feel.
11
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Use in various Fields
12
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Use in various Fields
– Django
– TurboGears
– web2.py
– Pylons
– Zope
– WebWare
13
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Implementations
1. CPython
• Classic Python (often just called Python) is the fastest, most up-to-date, most solid and
complete implementation of Python. Therefore, it can be considered the "reference
implementation" of the language.
• CPython is a compiler, interpreter, and set of built-in and optional extension modules, all
coded in standard C.
• CPython can be used on any platform where the C compiler complies with the ISO/IEC
9899:1990 standard (i.e., all modern, popular platforms
14
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Implementations
2. Jython
• Jython is a Python implementation for any Java Virtual Machine (JVM) compliant with
Java 1.2 or better.
• With Jython, you can use all Java libraries and frameworks. For optimal use of Jython,
you need some familiarity with fundamental Java classes.
• You do not have to code in Java, but documentation and examples for existing Java
classes are couched in Java terms, so you need a nodding acquaintance with Java to
read and understand them.
• You also need to use Java supporting tools for tasks such as manipulating .jar files and
signing applets
15
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Implementations
3. IronPython
• With IronPython, you can use all CLR libraries and frameworks. In addition to Microsoft's
own implementation, a cross-platform implementation of the CLR (known as Mono) works
with other, non-Microsoft operating systems, as well as with Windows. For optimal use of
IronPython, you need some familiarity with fundamental CLR libraries.
• You do not have to code in C#, but documentation and examples for existing CLR libraries
are often couched in C# terms, so you need a nodding acquaintance with C# to read and
understand them. You also need to use CLR supporting tools for tasks such as making
CLR assemblies
16
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Features
Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language in a relatively short period of time.
A broad standard library: One of Python's greatest strengths is the bulk of the library is very
portable and cross-platform compatible on UNIX, Windows and Macintosh.
Interactive Mode: Support for an interactive mode in which you can enter results from a
terminal right to the language, allowing interactive testing and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the same interface
on all platforms.
17
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Features
Extendable: You can add low-level modules to the Python interpreter. These modules
enable programmers to add to or customize their tools to be more efficient.
Scalable: Python provides a better structure and support for large programs than shell
scripting.
Very high-level dynamic data types and supports dynamic type checking..
It can be easily integrated with C, C++, COM, ActiveX, CORBA and Java
18
© Nex-G Exuberant Solutions Pvt. Ltd.
Who Uses Python Today
• Python is also being applied in real revenue-generating products by real companies. For
instance:
• Google makes extensive use of Python in its web search systems, and employs
Python’s creator.
• Google’s popular App Engine web development framework uses Python as its
application language.
19
© Nex-G Exuberant Solutions Pvt. Ltd.
Who Uses Python Today
• EVE Online, a Massively Multiplayer Online Role Playing Game (MMORPG), makes
extensive use of Python.
• Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM use Python for hardware
testing.
• And so on. Probably the only common thread amongst the companies using Python today
is that Python is used all over the map, in terms of application domains. Its general-
purpose nature makes it applicable to almost all fields, not just one. In fact, it’s safe to
say that virtually every substantial organization writing software is using Python, whether
for short-term tactical tasks, such as testing and administration, or for long term strategic
product development
20
© Nex-G Exuberant Solutions Pvt. Ltd.
Comparing Python to Other Languages
Java
• Python programs are generally expected to run slower than Java programs, but they
also take much less time to develop. Python programs are typically 3-5 times shorter than
equivalent Java programs.
• Python has built-in high-level data types and dynamic typing. A Python programmer
wastes no time declaring the types of arguments or variables, and Python's powerful
polymorphic list and dictionary types, for which rich syntactic support is built straight into
the language, find a use in almost every Python program.
• Because of the run-time typing, Python's run time must work harder than Java's.
• For these reasons, Python is much better suited as a "glue" language, while Java is
better characterized as a low-level implementation language
21
© Nex-G Exuberant Solutions Pvt. Ltd.
Comparing Python to Other Languages
Javascript
Perl
• Python and Perl come from a similar background (Unix scripting, which both have long
outgrown), and sport many similar features
• Perl emphasizes support for common application-oriented tasks, e.g. by having built-in
regular expressions, file scanning and report generating features. Python emphasizes
support for common programming methodologies such as data structure design and
object-oriented programming, and encourages programmers to write readable code by
providing an elegant but not overly cryptic notation.
22
© Nex-G Exuberant Solutions Pvt. Ltd.
Comparing Python to Other Languages
Tcl
• However, Tcl, which traditionally stores all data as strings, is weak on data structures, and
executes typical code much slower than Python.
• Tcl also lacks features needed for writing large programs, such as modular namespaces
23
© Nex-G Exuberant Solutions Pvt. Ltd.
Comparing Python to Other Languages
C++
• No memory management. You don't have to explicitly allocate memory for new
variables, and you don't have to explicitly free memory you're done with. The interpreter
will allocate memory for you and free it when it's safe to do so.
• High-level native data types. Strings, tuples, lists, sets, dictionaries, file objects and
more are built-in. As an example, {"x": "y"} defines a dictionary (hash table) with string "x"
as a key and string "y" as its value.
• Especially clean, straightforward syntax. This is a major goal of the Python language.
Programmers familiar with C and C++ will find the syntax familiar yet much simpler
without all the braces and semicolons
•In 1991 python 0.9.0 was published and reached the masses through alt.sources
25
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Timeline
bypassed however)
26
employees programming© “literate”
Nex-G Exuberant Solutions Pvt. Ltd.
Python Timeline
28
© Nex-G Exuberant Solutions Pvt. Ltd.
Which version of Python
Python 2.7
• Current version on Eniac, so we’ll NOT use it, because it is deprecated.
• Last stable release before version 3
• Implements some of the new features in version 3, but fully backwards compatible
Python 3
• Released in 2008
• Many changes (including incompatible changes)
• Much cleaner language in many ways
• Strings use Unicode, not ASCII
• A lot of important third-party libraries are compatible with Python 3
Disadvantages:
• Python isn't the best for memory intensive tasks.
• Python is interpreted language & is slow compared to C/C++ or java.
• Python not a great choice for a high-graphic 3d game that takes up a lot of CPU.
• Python is evolving continuously, with constant evolution there is little substantial
documentation available for the language.
30
© Nex-G Exuberant Solutions Pvt. Ltd.
Commenting in Python
# Comments:
• Hash-mark comments are the most basic way to document your code.
• Such comments are accessible only in your source files, though; to code comments that
are more widely available, you’ll need to use docstrings.
• In fact, current best practice generally dictates that docstrings are best for larger
functional documentation (e.g., “my function/method does this”), and # comments are best
limited to smaller code documentation.
31
© Nex-G Exuberant Solutions Pvt. Ltd.
Some important functions/attributes
• The built-in dir function is an easy way to grab a list of all the attributes available inside an
object (i.e., its methods and simpler data items). It can be called on any object that has
attributes. For example, to find out what’s available in the standard library’s sys module,
import it and pass it to dir >>> import sys
>>> import sys
>>> dir(sys)
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__',
'__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__',
'__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache',
'_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework',
'_getframe', '_git', '_home', '_xoptions', 'addaudithook', 'api_version', 'argv', 'audit',
'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder',
'call_tracing', 'callstats', 'copyright’,…]
32
© Nex-G Exuberant Solutions Pvt. Ltd.
Some important functions/attributes
Docstrings:
• Available by using __doc__ attribute
• Syntactically, such comments are coded as strings at the tops of module files and function
and class statements, before any other executable code (# comments are OK before
them). Python automatically stuffs the strings, known as docstrings, into the __doc__
attributes of the corresponding objects
33
© Nex-G Exuberant Solutions Pvt. Ltd.
Getting help with pydoc
• The pydoc module automatically generates documentation from Python modules. The
documentation can be presented as pages of text on the console, served to a Web
browser, or saved to HTML files.
• The built-in function help() invokes the online help system in the interactive interpreter,
which uses pydoc to generate its documentation as text on the console. The same text
documentation can also be viewed from outside the Python interpreter by running pydoc
as a script at the operating system’s command prompt.
• at a shell prompt will display documentation on the sys module, in a style similar to the
manual pages shown by the Unix man command. The argument to pydoc can be the
name of a function, module, or package, or a dotted reference to a class, method, or
function within a module or module in a package
34
© Nex-G Exuberant Solutions Pvt. Ltd.