Section 2 Python Programming
Section 2 Python Programming
2.0 INTRODUCTION
This is the lab course, wherein you will have the hands on experience. You have
studied the support course material (MCS-201 Programming in C & Python). In this
part, Python programming under ANACONDA and Google Colabs environments
are provided illustratively. A list of programming problems is also provided at the
end of each session. Please go through the general guidelines and the program
documentation guidelines carefully.
2.1 OBJECTIVES
After completing this lab course you will be able to:
develop the logic for a given problem;
recognize and understand the syntax and construction of Python code;
gain experience of Python as Object Oriented Pprogramming Llanguage;
know the steps involved in executing the python code;
••understand how to access and use modules;
understand function declaration and definition;
feel more confident about writing your own functions;
be able to write some simple output on the screen as well as in the files;
be able to write some complex programs;
be able to apply all the concepts that have been covered in the theory
course; and
know the alternative ways of providing solution to a given problem.
18
Lab You may seek assistance in doing the lab exercises from the concerned Python
Programming Lab
lab instructor. Since the assignments have credits, the lab instructor is
obviously not expected to tell you how to solve these, but you may ask
questions concerning the Python language or a technical problem.
For each program you should add comments above each function in the
code, including the main function. This should also include a description
of the function written, the purpose of the function, meaning of the
argument used in the function and the meaning of the return value (if
any).
The comment block above the main function should describe the purpose
of the program. Proper comments are to be provide where and when
necessary in the programming.
As soon as you have finished a lab exercise, contact one of the lab
instructor / in-charge in order to get the exercise evaluated and also get
the signature from him/her on the Observation book.
The total no. of lab sessions (3 hours each) are 10 and the list of
assignments is provided session-wise. It is important to observe the
deadline given for each assignment.
Metrics Python C
It is a high-level, general-purpose
C is general-purpose procedural
Introduction & interpreted programming
programming language.
language.
Being compiled programming
Being Interpreted programming
language its execution speed is
language its execution speed is
Speed faster then that of the interpreted
slower then that of the compiled
programming language (i.e.
programming language (i.e. C).
Python).
Number of lines of code written Program syntax of C is quite
Usage in Python is quite less in complicated in comparison to
comparison to C Python.
Declaration of Variable type is
In C, declaration of variable type
not required, they are un-typed
is must, and it is done at the time
Declaration and a given variable can be stuck
of its creation, and only values of
of variables different types of values at
that declared type must be
different instances during the
assigned to the variables.
execution of any python program.
Error debugging is simple, as it
takes only one instruction at a Being compiler dependent
time. Compilation and execution language, error debugging is
Error
is performed simultaneously. difficult in C i.e. it takes the
Debugging
Errors are instantly shown, and entire source code, compiles it
execution stops at that instruction and finally all errors are shown.
only.
the same function can be used by Function renaming mechanism is
Function
two different names i.e. it not supported by C i.e. the same
renaming
supports the mechanism of function cannot be used by two
mechanism
function renaming different names.
Syntax of Python programs is The syntax of a C program is
Complexity Quite simple, easy to read, write comparatively harder than the
and learn syntax of Python.
Memory- It supports automatic garbage In C, the Programmer has to
20 management collection for memory explicitly do the memory
Lab management. management.
2.4 Python
Programming Lab
Python is a General-Purpose C is generally used for hardware
WORKIN
Applications programming language can be related applications where you
G WITH used for web design, AI, ML etc. need efficiency.
PYTHON Built-in Library of built-in functions is Library of built-in functions is
functions quite large in Python. quite limited in C
Now, we are having a
Implementing Gives ease of implementing data Explicit implementation of
bit of clarity about the Data structures with built-in insert, functions is requited for the
Python as a Structures append functions. implementation of data structures
programming language,
Pointers functionality is not
from the past sections Pointers Pointers are frequently used in C.
available in Python.
we learned about the
various libraries and frameworks of Python. Now, we need to work on the
IDEs (Integrated Development Environments) of Python, there are many
IDEs like Jupyter Notebook, Spyder, VS Code, R Programming etc., all are
collectively available in Anaconda (Anaconda is a free and open-source
distribution of the Python and R programming languages for scientific
computing (data science, machine learning applications, large-scale data
processing,predictiveanalytics, etc.)), or you may also go for the cloud
versions Like Google Colab Notebook, where you need not to have high
configuration hardwares, only internet is required and through your gmail
account you can work on Python using Jupyter notebook.
We will discuss in brief, some of the ways to work with Python, you may
choose any or try all and other options too.
1) Just browse for https://www.python.org and perform following steps :
a) Download the latest version of Python for the operating system
installed on your computer, as in my case its windows 64 bit, so I
downloaded python-3.7.7 (python-3.7.7-amd64.exe) from
https://www.python.org/downloads/release/python-377/
b) Now Run this exe file and install the Python, just click next and go
ahead, till the setup installation is finished
c) Finally you are having an interface for Python programming
21
C and Python Lab
Pro
22
Lab a) Click the Download button on the webpage of Python
https://www.anaconda.com/ Programming Lab
23
C and Python Lab
Pro
3) Many a times the learners may not be equipped with the systems
having latest hardware configuarations, as desired for the installation
of Python, or their might be compatibility issues with operating
syatem or may be due to any reason you are not able to install and
start your work with Python. Under such circumstances the solution is
Google Colab Notebook (https://colab.research.google.com
/notebooks/ welcome.ipynb), use this and just login with your gmail
account and start your work on Jupyter Notebook, as simple as that.
24
Lab Just click file option and select new workbook, and new Jupyter notebook Python
will open in Google Colab, now you may start your work Programming Lab
print(‘Hello world!’)
x = 2020
print(x)
output
Hello World!
2020
25
C and Python Lab 1. Write a program in python, that calculates thevolume of a sphere (given
4 3. Pro
as πr ), with radius r entered by the user. (Assume π = 3.14)
3
26
c) sum of two matrices
Lab 12. Write a program in Python to display the Fibonacci Sequences up to nth Python
term where n is provided by the user Programming Lab
Session - 5
13. Write a function in Python to find the factorial of a number(n), where n
is provided by the user
14. Write a function in Python to calculate sum, difference, product of the
two numbers passed as an argument to the function.
15. Write a function in Python which will accept a number within the range
of 1 and 20, and find whether it is a prime number or not. If it is a prime
number, function should return True and if not False.
Note that prime numbers are numbers which are divisible only by 1 and
by the number it-self. What is the argument type that you use here?
Session - 6
16. Write Lambda function and Normal function in Python to get the average
of two numbers.
17. Write a Python program to sort a list of tuples using Lambda.
18. Write program in Python to perform following file operations
a) display file contents
b) Create file and add contents to it.
c) read, write and append using with statement
d) to copy a file from ‘original.txt’ to ‘duplicate.txt’
e) to delete a file
Session - 7
19. Develop a module named calculator.py in Python, which contain three
function – factorial(n) : to find factorial of a number, Fibonacci(n): to
find Fibonacci series up to given number, n. Perform following tasks:
a) Import the developed module
b) call the functions available in the module
i) to generate Fibonacci series upto a number, and
ii) to determine factorial of a number entered by user
20. Create a package in Python name Interest and within this package create
two modules simple.py and compound.py. The simple.py module
contains function Simple_Interest() and compound.py module contains
function Compound_Interest().
Session - 8
27
C and Python Lab 21. Write Python class to :
Pro
a) to convert an integer to a roman numeral
b) to convert a roman numeral to an integer.
22. Write a Python class which has two methods get_String and print_String.
get_String accept a string from the user and print_String print the string
in upper case
23. Write a Python class named Circle constructed by a radius and two
methods which will compute the area and the perimeter of a circle.
Session - 9
24. Write a python code to read a dataset (may be CSV file) and print all
features i.e. columns of the dataset. Determine the descriptive statistics
i.e. Maximum, Minimum Mean Median, Count, Variance,Standard
Deviation etc. of the numeric features like age, salary etc., may be
present in the dataset.
25. Write a Python program to read a given CSV file
a) having tab delimiter.
b) as a list.
c) as a dictionary.
d) print the content of the columns
26. Write a Python program to write a Python dictionary to a csv file. After
writing the CSV file read the CSV file and display the content.
27. Write a Python program to write a Python list of lists to a csv file. After
writing the CSV file read the CSV file and display the content.
Session -10
28. Write Python Program to perform following tasks
a) Create a database TRAINING_DB
b) Set connection with mysql.connector.connect.
c) Create a table EMP_TRAINING in database TRAINING_DB with
following data
FIRST_NAME,LAST_NAME,AGE,GENDER,INCOME.
d) change table structure / (add, edit, remove column of a table) at run
time
i) add a column address in the EMP_TRAINING table.
ii) execute SQL INSERT statement to create a record into
EMP_TRAINING table
28
Lab iii) run the query to updates all the records having GENDER Python
as 'M', and increase AGE of all the males by one year. Programming Lab
iv) delete all the records from EMP_TRAINING Table where AGE
is less than 18 .
29