Login
Password

Forgot your password?

Python Language

By Edited Jan 21, 2016 0 0

LANGUAGE EVOLUATION

Python is Intended use in application, scripting and web development and pursues language paradigms imperative, object-oriented, functional, aspect-oriented and reflective nowadays. Python is a language which is rapidly changed and evaluated through the different versions. Language changes are done using a formally documented procedure, starting with a Python Enhancement Proposals (PEP). Inventor of the Python is Guido van Rossum in the late 1980s by at CWI in the Netherlands. This attempt is to successor to the ABC programming language capable of exception handling and interfacing with the Amoeba operating system.

Version 0.9.0 - Initially released in 1991. A module system. Consists class with inheritance, exception handling, and functions.

Version 1.0 - Released in Jan,1994 . The major new features were the functional programming tools lambda, map, and filter and reduce.

Version 1.4 - The basic features included were keyword arguments, built in support for complex numbers, and basic form of data hiding by data mangling (A technique used to resolve the problem of needing unique names for resolving programming entities).

Version 1.6 and 2.0 - Released in 2000. Consists major features from SET language, Haskell and garbage collection based on the reference cycles.

Version 3.x - Some of the major changes from Python 3.0 and above,

1. Unify the str/unicode types, representing text, and introducing a separate immutable bytes type and a mostly corresponding mutable buffer type, which both represent arrays of bytes.

2. Remove backward-compatibility features, including old-style classes, integer-truncating division, string exceptions, and implicit relative imports.

Main objectives for developing the Language

- Necessity to develop an interpreter, object oriented, portable, extensible and embeddable language.

- Necessity to have a language with improved readability.

- Necessity to have a language which share characteristics of both high level scripting and programming language.

targeted Application Domains

Python is visible in many application domains rather narrow down to a particular application domain.

Some of domains,

- Web and internet Development/application.

- Database.

- GUI development.

- Video development.

- Scientific and numeric computing.

- Networking development.

- Graphics development.

- A language for beginners.

Type of the meta languages

Lists the type of the meta languages that is used to define the syntax and the semantics of the language components.

1. Python

2. C

Some of the modules of language are written in python and some are in C.

Implementation Model

1. Python compiler/parser produces byte code acceding to the source code written. An implicit operation.

2. Then the virtual machine interprets the byte code.

Some alternative implementations,

- CPython - The reference implementation .

- IronPython - Python for .NET and Mono platforms.

- Jython - Python coded in Java.

- Parrot - Virtual machine being developed mainly as the runtime for Perl 6, but with the intent to also support dynamic languages like Python, Ruby, Tcl, etc.

- PyPy - Python coded in Python.

- Stackless Python - Python with co routines

Structure of THE PROGRAMS

Python programs can be written in a text file with .py extension. Some of supportive IDEs are standard Text editor, NetBeans, PyDev in Eclipse platform, Spyder. Program structure is,

1. Expressions embedded in statement and process objects.

2. Statements code the logic of a program's operation using expression.

3. Collection of statement is bundled into a Module.

4. Collection of modules creates a Python program.


Special features introduced by the language

- Statement groups by indentation. Ignored curly braces or begin-end blocks concept.

- Small number of powerful data types.

- Interactive Interpreter: Interact with the interpreter directly to write the programs from the Python prompt.

- Beginner's Language: Supports the development from simple text processing to WWW browsers to games.

Syntactic structure of the comments

Two styles.

1. Single line comments

# Single line comment.

2. Multiline comments

" " "

Multiline comments…….

Line 2

" " "

Syntactic Structure of the object names

Object is a group of attributes which consist of type of the object, fields, methods and base classes and again another object. An object has a identity, attributes, relationships (base class, derived class), and a name which is a label given to a memory cell(s).Name is an optional attribute which do not compulsory to declare a object and can use the object just using a name and here after the object is identified by the compiler and type is context sensitive. Names exist in Namespaces (collection of names, object reference etc.) such as module, instance or function's local namespaces.

- Maximum length of a name: unlimited in length.

- Case Sensitivity: Case sensitive.

- Keywords or Reserved words: All the keywords are reserved words.

- Name form: The uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9.(ASCII range (U+0001..U+007F) and some additional characters.)

Scope and lifetime of the variables

A variable is a pigeonhole in the memory, which has a designate, where you can store values.

Key points related to Python variables are

1. Not strongly typed.

2. Do not have to be explicitly declared to reserve memory space.

The declaration happens automatically when you assign a value to a variable.

Scope of the Variables

The scope of a variable is the portion of the program where the variable is valid or "known".

1. Global variables

2. Local variables

Local variables are only accessible inside the function; on the other hand, global variables are accessible inside and outside of the functions. See the following example program and expected result from the execution.

E.g :

Sample code 1 :

global_var = 'test1'

def ex1():

local_var = 'test2'

print global_var

print local_var

ex1()

print global_var

print local_var # this gives an error

Output :

test1

test2

test1

Traceback (most recent call last):…………

……………name 'local_var' is not defined

More clarification of scoping of variables

Declare and set a variable in a function with the same name as a global variable is meant for creating a new local variable Same with setting a variable in the inner function creates a new local variable instead of modifying the outer variable.

Sample code 2:

var = 'test1'

def ex2():

var = 'test2'

print 'inside the function var is ', var

ex2()

print 'outside the function var is ', var

Output :

inside the function var is test2

outside the function var is test1

To set the global variable inside a function, it needs to use the global statement making the inner variable to have module scope.

Sample code 3:

var = 'test1'

def ex3():

global var

var = 'test2'

print 'inside the function var is ', var

ex3()

print 'outside the function var is ', var

Output :

inside the function var is test2

outside the function var is test2

However, using the global keyword won't work in this case. global cause a variable to have module scope, but I want my variable to have the scope of the outer function.

Sample code 4:

def ex6():

var = 'test1'

def inner():

global var

var = 'test2'

print 'inside inner, var is ', var

inner()

print 'inside outer function, var is ', var

ex6()

Output :

inside inner, var is test2

inside outer function, var is test1

If a name is declared global, then all references and assignments go directly to the middle scope containing the module's global names. To rebind variables found outside of the innermost scope, the nonlocal statement can be used.

Sample code 5 :

def scope_test():

def do_local():

letter= "local letter"

def do_nonlocal():

nonlocal letter

letter= "nonlocal letter"

def do_global():

global letter

letter= "global letter"

letter= "test letter"

do_local()

print("After local assignment:", letter)

do_nonlocal()

print("After nonlocal assignment:", letter)

do_global()

print("After global assignment:", letter)

scope_test()

print("In global scope:", letter)

Output :

After local assignment: test letter

After nonlocal assignment: nonlocal letter

After global assignment: nonlocal letter

In global scope: …..

Scoping for nested functions: The inner function can access both var_outer and var_inner and the outer function cannot access var_inner.

Sample code 6:

def ex4():

var_outer = 'test1'

def inner():

var_inner = 'test2'

print var_outer

print var_inner

inner()

print var_outer

print var_inner # this gives an error

ex4()

Output :

test1

test2

test1

Traceback (most recent call last)……………

………global name 'var_inner' is not defined

Lifetime of the Variables

The lifetime of a variable is time from creation to destruction. It represents the period of time during which it can hold a value in the memory.

A variable whose memory location is created when the block in which it is declared is entered and exists while the block is active, and then it is destroyed when the block is exited.

a. Local Variables

A variable that exists from the point at which the program begins execution and continues to exist during the duration of the program.

a. Global variables

b. Local variables can be declared to be static if a function to remember values between function calls.

Control Structures

Conditional constructs add in decision making into programs determining the sequence in which a program will execute instructions.

Python control structures:

1. IF, ELIF, ELSE Statements

IF: The logical expression is evaluated with the provided data and a decision is made based on the result of the evaluation.


ELSE: An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. At most one else statement following single if.

ELIF: Allows checking multiple expressions for truth value and executing a block of code as soon as one of the conditions evaluates to true. 0…n number of ELIF are allowed following single if.

2. WHILE and FOR Loop Statements :

Loop construct that causes a section of a program to be repeated a certain number of times.


WHILE: Loop continues until the logical expression becomes false.

FOR: Loop iterate over the items of any sequence such a list or a string. If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statements(s) block is executed until the entire sequence is ended.

3. Loop control statements: break, continue, else and pass.

BREAK: Terminates the current loop and resumes execution at the next statement and can be used in both while and for loops.

CONTINUE: Returns the control to the beginning of the loop skipping the remaining statements in the current iteration of the loop. Continue can be used in both while and for loops.

ELSE: If the else statement is used with for or while loops, the else statement is executed when the loop terminates. In case of for loop, the else statement is executed when the loop has exhausted iterating the list. In context of while loop the else statement is executed when the condition becomes false.

PASS: Nothing happen executing statement pass. It is used in the place where a statement is required syntactically but you do not want any command or code to execute.

Sample code 6:

for letter in 'tes':

if letter == 'e':

pass

print 'This is pass block'

print 'Current Letter :', letter

Output :

Current Letter : t

This is pass block

Current Letter : e

Current Letter : s



Advertisement

Comments

Add a new comment - No HTML
You must be logged in and verified to post a comment. Please log in or sign up to comment.

Explore InfoBarrel

Auto Business & Money Entertainment Environment Health History Home & Garden InfoBarrel University Lifestyle Sports Technology Travel & Places
© Copyright 2008 - 2016 by Hinzie Media Inc. Terms of Service Privacy Policy XML Sitemap

Follow IB Technology