Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction To Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 0

1

Lesson 1: Introduction to Python Programming Language


What is Python?
Python is a powerful high level object-oriented programming language created by Guido van Rossum, comparable to Java or
other OOP languages. Python is an interpreted language because programs written in Python are executed by an interpreter.
Python interpreter can be used in two ways: interactive mode and script mode.
Python name was taken from Monty Pythons Flying Circus comedy series.
Some features of Python:
Versatility: Used in a wide range of applications - from web design and game programming to scientific research.
Free: Python is Free to use and its code can modified as it is an Open Source.
Interactive way: Python's interactive mode makes it easy to test short snippets of code.
Traditional way: Script mode of Python gives programmers flexible and traditional way to store and reuse their script
repeatedly. Programs can be saved into a separate file called a module or package. File name extension should be
given as ".py" or ".pyw".
Platform independent: Python is cross platform language; its program can run on Windows and Unix-like systems such
as Linux, BSD, and Mac OS X, simply by copying the file or files that make up the program to the target machine, with
no compiling necessary.
Large Standard Library: Python comes with a large standard library that supports many common programming tasks
such as connecting to web servers, searching text with regular expressions, reading and modifying files.
Garbage Collection: Automatic memory management system freed Python programmer from bothering about
memory management like allocating and de-allocating memory space.
Exception Handling: Python Exceptions handling system provide a way to handle the exceptional circumstances (like
runtime errors) in our program.
There are three main implementations of Python. CPython, IronPython and Jython. CPython is implemented in C language. It is
the most widely used implementation of Python. When people talk about Python language, they mostly mean CPython.
IronPython is implemented in C#. It is part of the .NET framework. Similarly, Jython is an implementation of the Python
language in Java. Jython program is translated into the Java bytecode and executed by the JVM (Java Virtual Machine).
Here we will use CPython.
Difference between Python and C++
Python C++
No Pointer Extensive use of Pointers
Auto Memory Management (Garbage Collection) Programmers intervention required for memory management
Interpreter Compiler
Small Code Lengthy Code
Platform Independent Platform Dependent
Interactive mode available for single line code No interactive mode present
http://cbsecsnip.in



2
Using Python:
Python has two basic programming environments: interactive and script. We can use IDLE GUI IDE which comes with Python
setup.
Interactive Mode: Interactive mode is a command line environment called as Shell of Python which gives immediate result
for each statement fed from the prompt. The >>> (prompt) is Python's way of telling you that you are in interactive mode and
here we can enter our one line statement.
A sample interactive session:
>>> 6
6
>>> pr i nt ( 5*5)
25
>>> " hel l o" * 3
' hel l ohel l ohel l o'
Interactive mode is good for statements shown in above example but it is difficult to manage code like shown in following
example
>>>x=9
>>>i f x%2 == 1:
pr i nt " OK"
pr i nt " Fi ne"
In Python indentation is very important and grouping of statements are done on basis of their indentation. Statements at
same indentation are grouped together in a single block.
If we try to enter the above code in interactive mode, we will get error
>>> x=9
>>>i f x%2 == 1:
pr i nt " OK"
pr i nt " Fi ne"
Synt axEr r or : i nval i d synt ax

So it is wise choice to go for Script Mode.

Script Mode: Script mode is the mode where the scripted and finished .py files are run in the Python interpreter. For
programming/scripting in Python this mode is the only best way.
Now we can start writing our first Python program/script in IDLE environment.

Start IDLE (Pyhon GUI) from Start Menu


Fig. 1
http://cbsecsnip.in



3
After starting IDLE from start menu, IDLE open its window


Fig. 2

To start writing Python program Select File-> New Window from menu bar and a new Untitled Python document
window will open.


Fig. 3

This is the code window where we can start writing code, so let us start with our very first program







http://cbsecsnip.in



4


Fig. 4

Now select Run-> Run Module from menu bar or just press F5 IDLE will ask you to save the code in a file, give a proper
name of the file but dont forget to end the name of file with .py or .pyw


Fig. 5

If everything is goes OK then output will be shown on Python Shell window as shown below



So now we know lot about Python and also how to use IDLE environment for writing script/program in both the interactive
and script mode.
IDE for Python
There are many 3rd party IDEs are available for Python. Some popular Python IDEs comparison are shown in below Table 1
IDEs
Features
CP C/F AC MLS PD EM SC SI BM LN UML CF CT UT UID DB RAD
Idle Y F Y
Komodo Y C/F Y Y Y Y Y Y Y Y Y Y Y Y
NetBeans Y F Y Y Y Y Y Y Y Y Y Y Y Y
PyDev(Eclipse) Y F Y Y Y Y Y Y Y Y Y Y Y Y
Wing Y C/F Y Y Y Y Y Y Y Y Y Y Y
http://cbsecsnip.in



5
#Table 1
Acronyms used:
CP - Cross Platform
C - Commercial
F - Free
AC - Automatic Code-completion
MLS - Multi-Language Support
PD - Integrated Python Debugging
EM - ErrorMarkup
SC - Source Control integration
SI - Smart Indent
BM - Bracket Matching
LN - Line Numbering
UML - UML editing / viewing
CF - Code Folding
CT - Code Templates
UT - Unit Testing
UID - GUI Designer (for example, Qt, Eric, ..)
DB - integrated database support
RAD - Rapid application development support
L - Linux
W - Windows
M Mac

Lesson 2 -> in depth knowledge of Syntax, Variables, Expressions and Statements.



























#Table 1 source http://stackoverflow.com/
http://cbsecsnip.in

You might also like