Web2py – Python Language


Web2py – Python Language


”;


Python can be defined as a combination of object-oriented and interactive language. It is an open source software. Guido van Rossum conceived python in the late 1980s.

Python is a language similar to PERL (Practical Extraction and Reporting Language), which has gained popularity because of its clear syntax and readability.

The main notable features of Python are as follows −

  • Python is said to be relatively easy to learn and portable. Its statements can be easily interpreted in a number of operating systems, including UNIX-based systems, Mac OS, MS-DOS, OS/2, and various versions of Windows.

  • Python is portable with all the major operating systems. It uses an easy to understand syntax, making the programs, which are user friendly.

  • It comes with a large standard library that supports many tasks.

Scripting Languages

From the above diagram, it is clearly visible that Python is a combination of scripting as well as programming language. They are interpreted within another program like scripting languages.

Versions of Python

Python has three production-quality implementations, which are called as CPython, Jython, and IronPython. These are also termed as versions of Python.

  • Classic Python a.k.a CPython is a compiler, interpreter and consists of built-in and optional extension modules which is implemented in standard C language.

  • Jython is a Python implementation for Java Virtual Machine (JVM).

  • IronPython is designed by Microsoft, which includes Common Language Runtime (CLR). It is commonly known as .NET

Starting Up

A basic Python program in any operating system starts with a header. The programs are stored with .py extension and Python command is used for running the programs.

For example, python_rstprogram.py will give you the required output. It will also generate errors, if present.

Python uses indentation to delimit blocks of code. A block starts with a line ending with colon, and continues for all lines in the similar fashion that have a similar or higher indentation as the next line.

# Basic program in Python
print "Welcome to Python!n"

The output of the program will be −

Welcome to Python!

Indentation

Indentations of the programs are quite important in Python. There are some prejudices and myths about Python”s indentation rules for the developers who are beginners to Python.

The thumb rule for all the programmers is −

“Whitespace is significant in Python source code.”

Leading whitespace, which includes spaces and tabs at the beginning of a logical line of Python computes the indentation level of line.

Note

  • The indentation level also determines the grouping of the statements.

  • It is common to use four spaces i.e. tab for each level of indentation.

  • It is a good policy not to mix tabs with spaces, which can result in confusion, which is invisible.

Python also generates a compile time error if there is lack of indentation.

IndentationError: expected an indented block

Control Flow Statements

The control flow of a Python program is regulated by conditional statements, loops and function calls.

  • The If statement, executes a block of code under specified condition, along with else and elif(a combination of else-if).

  • The For statement, iterates over an object, capturing each element to a local variable for use by the attached block.

  • The While statement, executes a block of code under the condition, which is True.

  • The With statement, encloses a code block within the context manager. It has been added as a more readable alternative to the try/finally statement.

# If statement in Python
   x = int(raw_input("Please enter an integer: ")) #Taking input from the user
if x<0:
   print "1 - Got a negative expression value"
   print x
else:
   print "1 - Got a positive expression value"
   print x
print "Good bye!"

Output

sh-4.3$ python main.py
Please enter an integer: 4
1 - Got a positive expression value
4
Good bye!

Functions

The statements in a typical Python program are organized and grouped in a particular format called, “Functions”. A function is a group of statements that perform an action based on the request. Python provides many built-in functions and allows programmers to define their own functions.

In Python, functions are values that are handled like other objects in programming languages.

The def statement is the most common way to define a function. def is a single-clause compound statement with the following syntax −

def function-name (parameters):statement(s)

The following example demonstrates a generator function. It can be used as an iterable object, which creates its objects in a similar way.

def demo ():
   for i in range(5):
      yield (i*i)
	
for j in demo():
   print j

Output

sh-4.3$ python main.py
0
1
4
9
16

Special Attributes, Methods, and Operators

The attributes, methods, and operators starting with double underscore of a class are usually private in behavior. Some of them are reserved keywords, which include a special meaning.

Three of them are listed below −

  • __len__

  • __getitem__

  • __setitem__

The other special operators include __getattr__ and __setattr__, which defines the get and set attributes for the class.

File I/O Functions

Python includes a functionality to open and close particular files. This can be achieved with the help of open(), write() and close() functions.

The commands which help in file input and output are as follows −

Sr.No Command & Functionality
1

open()

It helps in opening a file or document

2

write()

It helps to write a string in file or document

3

read()

It helps in reading the content in existing file

4

close()

This method closes the file object.

Example

Consider a file named “demo.txt”, which already exists with a text “This is a demo file”.

#!/usr/bin/python
# Open a file
fo = open("demo.txt", "wb")
fo.write( "Insering new line n");
# Close opend file
fo.close()

The string available after opening the file will be −

This is a demo file
Inserting a new line

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *