Python Programming Lab
Introduction to Python:
Python is a widely used general-purpose, high-level, interpreted, object-oriented
programming language.
• Python is open source.
• It is platform Independent.
• It is a scripted language.
• Python is older than java, R, Javascript
It's estimated that 8.2 million developers use Python.
It was created by Guido van Rossum in 1991 and further developed by the Python
Software Foundation. It was designed with an emphasis on code readability, and its
syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems
more efficiently.
• The implementation of Python was started in December 1989 by Guido Van Rossum
at CWI (Centre for Mathematics and Computer Science) in Netherland.
• In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to
alt.sources.
• It has been derived from many languages such as ABC, Modula-3, C, C++, Algol-68,
Small Talk, UNIX shell and other scripting languages.
• In 1994, Python 1.0 was released with new features like lambda, map, filter, and
reduce.
• Python 2.0 added new features such as list comprehensions, garbage collection
systems.
• On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed
to rectify the fundamental flaw of the language.
Page 1
, Python Programming Lab
Beginning with Python programming:
Finding the interpreter:
Before we start Python programming, we need to have an interpreter to interpret and
run our programs. http://ideone.com/ or http://codepad.org/ that can be used to run
Python programs without installing an interpreter.
Windows: There are many interpreters available freely to run Python scripts like
IDLE (Integrated Development Environment) that comes bundled with the Python
software downloaded from http://python.org/.
Linux: Python comes preinstalled with popular Linux distros such as Ubuntu and Fedora.
To check which version of Python you’re running, type “python” in the terminal
emulator. The interpreter should start and print the version number.
macOS: Generally, Python 2.7 comes bundled with macOS. You’ll have to manually install
Python 3 from http://python.org/.
Writing first program:
Just type in the following code after you starts the interpreter.
# program starts here
print(‘welcome to SVECW’)
# program ends here
Output: welcome to SVECW
Understanding the above code:
Line 1: # program starts here
In Python the comments begin with a #. This statement is ignored by the interpreter and
used for documentation purpose.
Line 2: print(‘welcome to SVECW’)
To print something on the console, print() function is used. This function also adds a
newline after our message is printed(unlike in C). Note that in Python 2, “print” is not a
function but a keyword and therefore can be used without parentheses. However, in
Python 3, it is a function and must be invoked with parentheses.
Page 2
, Python Programming Lab
Line 3: # program ends here
This is another comment like line 1.
Writing and Executing programs in Ubuntu:
Step 1: Open the terminal and type $ gedit p2.py (p2.py is file name)
Step 2: type the program in Text Editor
Step 3: execute the program with the command $ python3 p2.py
Page 3
, Python Programming Lab
We can see the output in above figure.
How to install Python:
Python installation is pretty simple, you can install it on any operating system such as
Windows, Mac OS X, Ubuntu etc. Just follow the steps
Local Environment Setup: Open a terminal window and type "python" to find out if it
is already installed and which version is installed.
Getting Python for Windows platform: Binaries of latest version of Python 3 (Python
3.9.6) are available in https://www.python.org/
The following different installation options are available.
Windows x86-64 embeddable zip file
Windows x86-64 executable installer
Windows x86-64 web-based installer
Windows x86 embeddable zip file
Windows x86 executable installer
Windows x86 web-based installer
Download the software and save to hard drive. Double click on the install file and install
software as per instructions. By default, IDLE will install in to the system as default IDE
(Integrated development Environment). After installing software it is very important to set
the path if path is not set by default. Above 3 versions are not necessary to set the path. It
will set automatically by default.
Setting Path at Windows:
To add the Python directory to the path for a particular session in Windows-
At the command prompt: type path %path%; C:\Python and press Enter.
Note: C:\Python is the path of the Python directory.
Page 4
Introduction to Python:
Python is a widely used general-purpose, high-level, interpreted, object-oriented
programming language.
• Python is open source.
• It is platform Independent.
• It is a scripted language.
• Python is older than java, R, Javascript
It's estimated that 8.2 million developers use Python.
It was created by Guido van Rossum in 1991 and further developed by the Python
Software Foundation. It was designed with an emphasis on code readability, and its
syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems
more efficiently.
• The implementation of Python was started in December 1989 by Guido Van Rossum
at CWI (Centre for Mathematics and Computer Science) in Netherland.
• In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to
alt.sources.
• It has been derived from many languages such as ABC, Modula-3, C, C++, Algol-68,
Small Talk, UNIX shell and other scripting languages.
• In 1994, Python 1.0 was released with new features like lambda, map, filter, and
reduce.
• Python 2.0 added new features such as list comprehensions, garbage collection
systems.
• On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed
to rectify the fundamental flaw of the language.
Page 1
, Python Programming Lab
Beginning with Python programming:
Finding the interpreter:
Before we start Python programming, we need to have an interpreter to interpret and
run our programs. http://ideone.com/ or http://codepad.org/ that can be used to run
Python programs without installing an interpreter.
Windows: There are many interpreters available freely to run Python scripts like
IDLE (Integrated Development Environment) that comes bundled with the Python
software downloaded from http://python.org/.
Linux: Python comes preinstalled with popular Linux distros such as Ubuntu and Fedora.
To check which version of Python you’re running, type “python” in the terminal
emulator. The interpreter should start and print the version number.
macOS: Generally, Python 2.7 comes bundled with macOS. You’ll have to manually install
Python 3 from http://python.org/.
Writing first program:
Just type in the following code after you starts the interpreter.
# program starts here
print(‘welcome to SVECW’)
# program ends here
Output: welcome to SVECW
Understanding the above code:
Line 1: # program starts here
In Python the comments begin with a #. This statement is ignored by the interpreter and
used for documentation purpose.
Line 2: print(‘welcome to SVECW’)
To print something on the console, print() function is used. This function also adds a
newline after our message is printed(unlike in C). Note that in Python 2, “print” is not a
function but a keyword and therefore can be used without parentheses. However, in
Python 3, it is a function and must be invoked with parentheses.
Page 2
, Python Programming Lab
Line 3: # program ends here
This is another comment like line 1.
Writing and Executing programs in Ubuntu:
Step 1: Open the terminal and type $ gedit p2.py (p2.py is file name)
Step 2: type the program in Text Editor
Step 3: execute the program with the command $ python3 p2.py
Page 3
, Python Programming Lab
We can see the output in above figure.
How to install Python:
Python installation is pretty simple, you can install it on any operating system such as
Windows, Mac OS X, Ubuntu etc. Just follow the steps
Local Environment Setup: Open a terminal window and type "python" to find out if it
is already installed and which version is installed.
Getting Python for Windows platform: Binaries of latest version of Python 3 (Python
3.9.6) are available in https://www.python.org/
The following different installation options are available.
Windows x86-64 embeddable zip file
Windows x86-64 executable installer
Windows x86-64 web-based installer
Windows x86 embeddable zip file
Windows x86 executable installer
Windows x86 web-based installer
Download the software and save to hard drive. Double click on the install file and install
software as per instructions. By default, IDLE will install in to the system as default IDE
(Integrated development Environment). After installing software it is very important to set
the path if path is not set by default. Above 3 versions are not necessary to set the path. It
will set automatically by default.
Setting Path at Windows:
To add the Python directory to the path for a particular session in Windows-
At the command prompt: type path %path%; C:\Python and press Enter.
Note: C:\Python is the path of the Python directory.
Page 4