Python Programming
Python Programming
Prerequisite:
1. Programming Concepts
Objective:
1. Python is a modern language primarily useful for writing compact codes for programming in
Web development on the server side, data analytics, AI and scientific computing, as well
production tools and game programming. This course discusses the fundamentals and advanced
programming of Python to harness its ability for modern requirements for computing.
Contents:
Teaching Module
Unit Topics
Hrs. Weightage
No.
1 Introduction to Python Programming: 02 5%
Condition: If, If. Else and nested if, Loops: For, while loops,
Nested loops, Enumerate, Break, Continue Statement.
Course Outcomes:
CO-2 To be able to understand the various data structures available in Python 2,3
programming language and apply them in solving computational problems.
CO-3 To be able to do text filtering with regular expressions in Python 4,5
CO-5 To be able to user inbuilt library for the real time application. 5,6,7
List of Experiments
1 Create a program that asks the user to enter their name and their age. Print out a message addressed
to them that tells them the year that they will turn 100 years old.
3 Ask the user for a number. Depending on whether the number is even or odd, print out an
appropriate message to the user. Hint: how does an even / odd number react differently when
divided by 2?
Self:
If the number is a multiple of 4, print out a different message
6 Take two lists a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] and
write a program that returns a list that contains only the elements that are common between the lists
(without duplicates). Make sure your program works on two lists of different sizes. Write this using
at least one list comprehension. Self: Randomly generate two lists to test this.
7 Ask the user for a string and print out whether this string is a palindrome or not. (Using string
reversal and for loops).
8 Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write
one line of Python that takes this list and makes a new list that has only the even elements of
this list in it.
Self:
Create list contains square of all odd numbers from range 1 to 10
Create list contains power of 2 from 1 to 8
Create list contains prime and non-prime in range 1 to 50
9 Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input),
compare them, print out a message of congratulations to the winner, and ask if the players
want to start a new game)
Remember the rules:
Rock beats scissors, Scissors beats paper, Paper beats rock
10 Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the
number, then tell them whether they guessed too low, too high, or exactly right.
Self:
Keep the game going until the user types “exit”
11 Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a
new list of only the first and last elements of the given list. For practice, write this code inside
a function.
13 Write a program that asks the user how many Fibonacci numbers to generate and then
generates them. Take this opportunity to think about how you can use functions. Make sure to
ask the user to enter the number of numbers in the sequence to generate. (Hint: The Fibonacci
sequence is a sequence of numbers where the next number in the sequence is the sum of the
previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)
14 Write a program (function!) that takes a list and returns a new list that contains all the
elements of the first list minus all the duplicates.
Self:
Write two different functions to do this - one using a loop and constructing a list, and another
using sets.
15 Write a program (using functions!) that asks the user for a long string function. Containing
multiple words. Print back to the user the same string, except with the words in backwards
order. For example, say I type the string: My name is Michele
Then I would see the string: Michele is name My
shown back to me
16 Write a Python class named Circle constructed by a radius and two methods which will
compute the area and the perimeter of a circle.
17 Implement Multiple Inheritance. Python supports classes inheriting from other classes. The
class being inherited is called the Parent or Superclass, while the class that inherits is called
the Child or Subclass. How can we define the order in which the base classes are searched?
when executing a method? (Hint: MRO: Method Resolution Order)
Major Equipment:
Books Recommended: -
1. www.nptel.ac.in
2. https://docs.python.org/3/tutorial/
3. www.cpu-world.com