Académique Documents
Professionnel Documents
Culture Documents
1.Introduction
History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, ALGOL-68,
SmallTalk, and Unix shell and other scripting languages.
Python is now maintained by a core development team at the institute, although Guido van Rossum
still holds a vital role in directing its progress.
Python features
Python's feature highlights include:
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.
Easy-to-maintain: Python's success is that its source code is fairly easy-to-maintain.
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.
Expandable: 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.
Databases: Python provides interfaces to all major commercial databases.
GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh and the X
Window system of Unix.
Scalable:
Python provides a better structure and support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are
listed below:
Support for functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
Very high-level dynamic data types and supports dynamic type checking.
Supports automatic garbage collection.
It can be easily integrated with C, C++, ActiveX, CORBA and Java.
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the
same type of quote starts and ends the string.
The triple quotes can be used to span the string across multiple lines.
For example, all the following are legal
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""(Single quotes can also be used in place of
double quotes).
quote="'Hi'" #The string literal is 'Hi'
We can also write the same literal as shown below:
quote='\'Hi\''
Note: Single and double quotes have no difference while using as a literal.
Line Indentation
One of the first cautions programmers encounter when learning Python is the fact that there are no
braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are
denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be
indented with same amount of spaces
Block 1:
if True:
print "True"
else:
print "False"
However, the second block in this example will generate an error:
Block 2:
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"
Thus, in Python all the continuous lines indented with similar number of spaces would form a block.
Note: Use 4 spaces for indentation as a good programming practice.
Multi-Line Statements
Statements in Python typically end with a new line. Python does, however, allow the use of the line
continuation character (\) to denote that the line should continue. For example:
total = first_one + \
second_two + \
third_three
Statements contained within the [], {} or () brackets do not need to use the line continuation
character. For example:
days = ['Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday']
Problem statement
Write a program to get the following output?
$
$$$
$
Solution
The output would be as shown below,
print ' $'
print ' $'
print '$$$'
w
www
w
www
The first and last lines have tab as space between them.
3.How to write multiple statements on a single line?
4.Write a program to provide following output
hello 'hi' bye 'why'
5.Write a program to show documented comments in the Program.
Please attempt these above problems.
Click here to download the solutions.