Introduction to Python.unlocked

  • Upload
    manasj

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 7/27/2019 Introduction to Python.unlocked

    1/5

    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 Jav

    other OOP languages. Python is an interpreted language because programs written in Python are executed by an interpre

    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 sc

    repeatedly. Programs can be saved into a separate file called a module or package. File name extension should

    given as ".py" or ".pyw".

    Platform independent: Python is cross platform language; its program can run on Windows and Unix-like systems s

    as Linux, BSD, and Mac OS X, simply by copying the file or files that make up the program to the target machine, w

    no compiling necessary. Large Standard Library: Python comes with a large standard library that supports many common programming ta

    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 ab

    memory management like allocating and de-allocating memory space.

    Exception Handling: Python Exceptions handling system provide a way to handle the exceptional circumstances (

    runtime errors) in our program.

    There are three main implementations of Python. CPython, IronPython and Jython. CPython is implemented in C language.

    the most widely used implementation of Python. When people talk about Python language, they mostly mean CPyth

    IronPython is implemented in C#. It is part of the .NET framework. Similarly, Jython is an implementation of the Pyt

    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 managem

    Interpreter Compiler

    Small Code Lengthy Code

    Platform Independent Platform Dependent

    Interactive mode available for single line code No interactive mode present

    http://cbsecsnip.in

    http://cbsecsnip.in/http://cbsecsnip.in/http://cbsecsnip.in/
  • 7/27/2019 Introduction to Python.unlocked

    2/5

    2

    Using Python:

    Python has two basic programming environments: interactive and script. We can use IDLE GUI IDE which comes with Pyt

    setup.

    Interactive Mode:Interactive mode is a command line environment called as Shell of Python which gives immediate re

    for each statement fed from the prompt. The >>> (prompt) is Python's way of telling you that you are in interactive mode

    here we can enter our one line statement.A sample interactive session:

    >>> 66>>> 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 follow

    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. Statement

    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.

    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

    http://cbsecsnip.in/http://cbsecsnip.in/http://cbsecsnip.in/
  • 7/27/2019 Introduction to Python.unlocked

    3/5

    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 docum

    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

    http://cbsecsnip.in/http://cbsecsnip.in/http://cbsecsnip.in/
  • 7/27/2019 Introduction to Python.unlocked

    4/5

    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 pro

    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 interact

    and script mode.

    IDE for PythonThere 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

    http://cbsecsnip.in/http://cbsecsnip.in/http://cbsecsnip.in/
  • 7/27/2019 Introduction to Python.unlocked

    5/5

    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 ofSyntax,Variables, Expressions and Statements.

    #Table 1 sourcehttp://stackoverflow.com/

    http://cbsecsnip in

    http://stackoverflow.com/http://stackoverflow.com/http://stackoverflow.com/http://cbsecsnip.in/http://cbsecsnip.in/http://stackoverflow.com/http://cbsecsnip.in/