Programming in Python
Programming in Python
Year of
CST PROGRAMMING IN Category L T P Credit
Introduction
362 PYTHON
PEC 2 1 0 3 2019
Preamble: The objective of the course is to equip the learners to develop multi-module software
solutions for real world computational problems using Python. It encompasses the Python
programming environment, syntax, data representations, intermediate level features, GUI
programming, Object Oriented Programming and data processing. This course lays the
foundation to develop modular software solutions including complex interactive applications,
network applications, and data-driven intelligent applications.
Course Outcomes: After the completion of the course the student will be able to
Write, test and debug Python programs (Cognitive Knowledge level:
CO1
Apply)
Illustrate uses of conditional (if, if-else and if-elif-else ) and
CO2 iterative (while and for) statements in Python programs. (Cognitive
Knowledge level: Apply)
Develop programs by utilizing the Python programming constructs such as Lists,
CO3
Tuples, Sets and Dictionaries. (Cognitive Knowledge level: Apply)
Develop graphical user interface for solutions using Python libraries. (Cognitive
CO4
Knowledge level: Apply)
Implement Object Oriented programs with exception handling. (Cognitive
CO5
Knowledge level: Apply)
Write programs in Python to process data stored in files by utilizing
CO6
Numpy, Matplotlib, and Pandas. (Cognitive Knowledge level: Apply)
258
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
PO1
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO11 PO12
0
CO1 ✔ ✔ ✔ ✔ ✔
CO2 ✔ ✔ ✔ ✔
CO3 ✔ ✔ ✔ ✔ ✔ ✔
CO4 ✔ ✔ ✔ ✔ ✔ ✔
CO5 ✔ ✔ ✔ ✔ ✔ ✔
CO6 ✔ ✔ ✔ ✔ ✔ ✔ ✔
Assessment Pattern
Remember 20 20 20
259
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
Understand 35 35 35
Apply 45 45 45
Analyse
Evaluate
Create
Mark Distribution
150 50 100 3
260
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
contains 2 questions from each module of which a student should answer any one. Each question
can have a maximum of 2 sub-divisions and carries 14 marks.
SYLLABUS
Module -1 (Programming Environment and Python Basics) (6 hours)
Getting started with Python programming – Interactive shell, IDLE, iPython Notebooks,
Detecting and correcting syntax errors, How Python works. The software development process –
A case study. Basic coding skills – strings, assignment, and comments, Numeric data types and
character sets, Expressions, Using inbuilt functions and modules. Control statements – Iteration
with for/while loop, Formatting text for output, A case study, Selection structure (if-else, switch-
case), Conditional iteration with while, A case study, Testing control statements, Lazy evaluation.
Module -2 (Building Python Programs) (8 hours)
Strings and text files – Accessing characters, substrings, Data encryption, Strings and number
system, String methods, Text files, A case study on text analysis. Design with Functions –
Functions as Abstraction Mechanisms, Problem solving with top-down design, Design with
recursive functions, Managing a program’s namespace, Higher-Order Functions. Lists - Basic list
Operations and functions, List of lists, Slicing, Searching and sorting list, List comprehension.
Work with tuples. Sets. Work with dates and times, A case study with lists. Dictionaries -
Dictionary functions, dictionary literals, adding and removing keys, accessing and replacing
values, traversing dictionaries, reverse lookup. Case Study – Data Structure Selection.
Module -3 (Graphics) (7 hours)
Graphics – Terminal-based programs, Simple Graphics using Turtle, Operations, 2D Shapes,
Colors and RGB Systems, A case study. Image Processing – Basic image processing with inbuilt
functions. Graphical User Interfaces – Event-driven programming, Coding simple GUI-based
programs : Windows, Labels, Displaying images, Input text entry, Popup dialog boxes,
Command buttons, A case study.
Module -4 (Object Oriented Programming) (7 hours)
Design with classes - Objects and Classes, Methods, Instance variables, Constructor, Accessor
and Mutator, Data-Modeling Examples, Structuring classes with inheritance and polymorphism.
Abstract classes, Interfaces, Exceptions - Handle a single exception, handle multiple exceptions.
Module -5 (Data Processing) (9 hours)
The os and sys modules, NumPy - Basics, Creating arrays, Arithmetic, Slicing, Matrix
Operations, Random numbers. Plotting and visualization. Matplotlib - Basic plot, Ticks, Labels,
and Legends. Working with CSV files. – Pandas - Reading, Manipulating, and Processing Data.
Introduction to Micro services using Flask.
261
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
Text Books:
1. Kenneth A Lambert., Fundamentals of Python : First Programs, 2/e, Cengage Publishing, 2016
2. Wes McKinney, Python for Data Analysis, 2/e, Shroff / O’Reilly Publishers, 2017
3. Flask: Building Python web services, Jack Stouffer, Shalabh Aggarwal, Gareth Dwyer,
PACKT Publishing Limited, 2018
Reference Books:
1. Zed A Shaw, Learn Python 3 The Hard Way, Addison-Wesley, 2017
2. Allen B. Downey, Think Python: How to Think Like a Computer Scientist, 2/e, Schroff, 2016
3. Michael Urban and Joel Murach, Python Programming, Shroff/Murach, 2016
4. Charles Severance. Python for Informatics: Exploring Information,
What would be the output if ‘break’ is replaced with ‘continue’ in the above code fragment?
Course Outcome 3(CO3):
1. Given is a list of of words, wordlist, and a string, name. Write a Python function which takes
wordlist and name as input and returns a tuple. The first element of the output tuple is the
number of words in the wordlist which have name as a substring in it. The second element of
262
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
the tuple is a list showing the index at which the name occurs in each of the words of the
wordlist and a 0 if it doesn’t occur.
2. What is the value of L after you run the code below?
L = ["life", "answer", 42, 0]
for thing in L:
if thing == 0:
L[thing] = "universe"
elif thing == 42:
L[1] = "everything"
263
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
QP CODE: PAGES:
Reg No:_______________
Name:_________________
PART-B
(Answer any one full question from each module)
264
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
11. (a) Write a Python code to check whether a given year is a leap year or not (6)
[An year is a leap year if it’s divisible by 4 but not divisible by 100
except for those divisible by 400].
(b) What are the possible errors in a Python program. Write a Python (8)
program to print the value of 22n+n+5 for n provided by the user.
OR
12. (a) Write a Python program to find the value for sin(x) up to n terms using the (6)
series
where x is in degrees
(b) Write a Python code to determine whether the given string is a Palindrome (8)
or not using slicing. Do not use any string function.
13. (a) Write a Python code to create a function called list_of_frequency that takes a (5)
string and prints the letters in non-increasing order of the frequency of their
occurrences. Use dictionaries.
(b) Write a Python program to read a list of numbers and sort the list in a non- (9)
decreasing order without using any built in functions. Separate function
should be written to sort the list wherein the name of the list is passed as the
parameter.
OR
14. (a) Illustrate the following Set methods with an example. (6)
i. intersection( ) ii. Union( ) iii. Issubset( ) iv. Difference( ) v. update( ) vi.
discard( )
(b) Write a Python program to check the validity of a password given by the (8)
user.
The Password should satisfy the following criteria:
1. Contains at least one letter between a and z
2. Contains at least one number between 0 and 9
3. Contains at least one letter between A and Z
4. Contains at least one special character from $, #, @
5. Minimum length of password: 6
265
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
17. (a) How can a class be instantiated in Python? Write a Python program to (10)
express the instances as return values to define a class RECTANGLE with
parameters height, width, corner_x, and corner_y and member functions to
find center, area, and perimeter of an instance.
(b) Explain inheritance in Python. Give examples for each type of inheritance. (4)
OR
18. (a) Write a Python class named Circle constructed by a radius and two methods (6)
which will compute the area and the perimeter of a given circle
(b) Write Python program to create a class called as Complex and implement (8)
__add__( ) method to add two complex numbers. Display the result by
overloading the + Operator.
19. (a) Write a Python program to add two matrices and also find the transpose of (8)
the resultant matrix.
(b) Given a file “auto.csv” of automobile data with the fields index, company, (6)
body-style, wheel-base, length, engine-type, num-of-cylinders, horsepower,
average-mileage, and price, write Python codes using Pandas to
1) Clean and Update the CSV file
2) Print total cars of all companies
266
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
20. (a) Write Python program to write the data given below to a CSV file. (5)
(b) Given the sales information of a company as CSV file with the following (9)
fields month_number, facecream, facewash, toothpaste, bathingsoap,
shampoo, moisturizer, total_units, total_profit. Write Python codes to
visualize the data as follows
1) Toothpaste sales data of each month and show it using a scatter plot
2) Face cream and face wash product sales data and show it using the
bar chart
Calculate total sale data for last year for each product and show it using a
Pie chart.
(14X5=70)
Teaching Plan
1.5 Control statements – Definite Iteration with for loop, Formatting text for 1 hour
output, Selection structure (if-else, switch-case), Conditional iteration with
267
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
Lists - Basic list Operations and functions, List of lists, Slicing, Searching
2.5 1 hour
and sorting list, List comprehension.
2.6 Work with tuples. Sets. Work with dates and times, A case study with lists. 1 hour
3.3 Image Processing – Basic image processing with inbuilt functions. 1 hour
3.5 Coding simple GUI-based programs : Windows, Labels, Displaying images, 1 hour
Coding simple GUI-based programs : Input text entry, Popup dialog boxes,
3.6 1 hour
Command buttons
268
Downloaded from Ktunotes.in
COMPUTER SCIENCE AND ENGINEERING
4.1 Design with classes : Objects and Classes, Methods, Instance Variables 1 hour
269
Downloaded from Ktunotes.in