Case Study Based Question
Case Study Based Question
(a)Great India
(b) print(country[-7:-1]) (b)t Indi
(c) print(country[::2]) (c)GetIda
(d) print(country[len(country)-1]) (d)a
(e) print(2*country) (e)Great IndiaGreat India
(f) print(country[:3] + country[3:]) (f)Great India
Q.4 Find Output:
my_string = 'Jhunjhunu' Ans. Jhu
print(my_string[:3]) J@H@U@N@J@H@U@N@U@
for i in range(len(my_string)): Jhunjhunu
print(my_string[i].upper(),end="@") Njh
print()
print (my_string)
print (my_string[3:6])
Q.5 Write a Python Program to input a string to check whether it is a Palindrome string or not. (A
Palindrome string is that which is same from both ends like – NITIN, MALAYALAM, PULLUP)
Ans. s=input("Enter a word :")
print ("You entered :", s)
length=len(s)
rev=""
for i in range (-1,-length-1,-1):
rev=rev+s[i]
if s==rev:
print ("Yes, palindrome")
else:
print ("Not a palindrome")
Case Study based question
Q.1 Ravee is developing a program to greet users based on their input. It takes a name as input
and returns a greeting message in the format "Hello, [name]!".
Ans. user_name = input("Enter your name: ")
print(“Hello”,user_name,”!”)
Q.2 Gorank is tasked with processing a file name and extracting the file extension. Program takes
a file name as input and return the output of its extension.
Ans. file_name = input("enter the file name with extension: ")
if '.' in file_name:
print("The file extension of", file_name, "is", file_name.split('.')[-1])
else:
print("No extension found")
45
LIST
INTRODUCTION
Lists are used to store multiple items in a single variable. Lists are enclosed in square brackets and
the elements of the list are separated by commas. List is a mutable data type, means that the contents
of the list can be changed after it is created.
ex: - L = [12, 23, ‘A’, “45”]
L1 = [ ] # L1 is a blank list
L2 = list( ) # list( ) function is used to create list L2
L3 = [1,2,3,4,5] # L3 is an integer list
L4 = ["ABC", 15, False, 25.5, "male"] # L4 is a list with mix data types
L5 = [‘a’, [2,4,’b’], 56] # L5 is a nested list
INDEXING IN LIST
It is a process in which each element of a list has its index or can be accessed using its index. The first
element of the list has index 0, the next has index 1, and so on. The index of the last element will be
the length of the minus one (Forward indexing).
There is another way (Backward indexing) for indexing from last element (index = -1) to first
element (index = -length of the list)
LIST OPERATIONS
(1) Concatenation (+ Operator): Python allows us to join two or more lists using + operator.
ex: - >>> l1 = [1,3,5,7,9]
>>> l2 = [2,4,6,8,10]
>>> l3 = [‘abc’, “qwe”]
>>>l1 + l2 + l3 # + operator
[1,3,5,7,9,2,4,6,8,10, ‘abc’, “qwe”]
(2) Repetition/ Replication Operator (* operator): Python allows us to replicate a list using
repetition operator depicted by symbol ‘*’.
ex: - >>> str1 = ['Help']
>>> str1 * 4
[' Help ', ' Help ', ' Help’, '' Help’']
(3) Membership (IN/ NOT IN operator) :
Like strings, the membership operators IN checks, if NOT IN operator returns True if the
the element is present in the list then returns True, element is not present in the list, else it
46
else returns False. returns False.
ex: - >>>‘N’ in [‘K’, ‘V’, ‘S’ ] ex: - >>>‘N’ not in [‘K’, ‘V’, ‘S’ ]
False True
>>>‘V’ in [‘K’, ‘V’, ‘S’ ] >>>‘V’ not in [‘K’, ‘V’, ‘S’ ]
True False
List Slicing: - Like strings, the slicing operation can also be applied to lists. List elements can be
accessed in subparts.
Syntax- list_name[start:stop:step]
L = [2,7,9,10,13,15,17,19,23,25]
Slice Generates
L[4: ] [13, 15, 17, 19, 23, 25]
L[ : 3] [2,7,9]
L[2: 5] [9,10, 13]
L[4: : 2] [13, 17, 23]
L[2 : 7 : 2] [9, 13, 17]
L[ : : -1] [25, 23, 19, 17, 15, 13, 10, 9, 7, 2]
L[ : : -2] [25, 19, 15, 10, 7]
L[6 : 1: -2] [17, 13, 9]
L[-8 : 8 ] [9, 10, 13, 15, 17, 19]
L[3 : -2 : 2] [10, 15, 19]
L[6 : -9 : -2] [17, 13, 9]
(4) Traversing: - Traversing means iterating through the elements of a list, one element at a time.
ex: - >>>L1 = [1,2,3,4] 1
>>>for a in L1: 2
print(a) 3
4
(5) Modifying a List element/Mutability of a List: - The elements of a list can be modified by using
L[index]=new_value. Since a list can be modified in place, i.e. the existing list object is not
destroyed and recreated.
(6) List Comparisons: - The relational operators >, >=, <, <=, ==, != can be applied for comparison
between two lists results will come in True or False. The following rules are observed:
47
(a) For equality (==) operator: - Equality evaluates to True only when all the elements of one
list match all the elements of the other list and both the list have the same number of
elements.
(b) For all other operators - The elements are compared sequentially using the operator
under consideration.
(c) If two lists are of different lengths and if one of the list has an element at an index
position and the other list does not have an element at that position then the evaluation
proceeds as follows:
• something/somevalue > empty/nothing evaluates to True
• something/somevalue < empty/nothing evaluates to False
List Functions :-
Function Syntax Description
list( ) list(sequence) It returns a list created from the passed
arguments, which should be a sequence type
(string, list, tuple etc.). if no argument is passed, it
will create an empty list.
append( ) list.append(items) It adds a single item to the end of the list.
extend( ) list1.extend(list2) It adds one list at the end of another list.
insert( ) list.insert(index_no, value) It adds an element at a specified index
reverse( ) list.reverse( ) It reverses the order of the elements in a list.
index( ) list.index(item) It returns the index of first matched item from the
list.
len( ) len(list) Returns the length of the list i.e. number of
elements in a list
sort( ) list.sort( ) This function sorts the items of the list.
clear( ) list.clear( ) It removes all the elements from the list.
count( ) list.count(element) It counts how many times an element has
occurred in a list and returns it.
sorted( ) sorted(sequence,reverse= It returns a newly created sorted list; it does not
False) change in the original list.
pop( ) list.pop(index) It removes the element from the specified index
and also returns the element which was removed.
remove( ) list.remove(value) It is used when we know the element to be
deleted, not the index of the element.
48
max( ) max(list) Returns the element with the maximum value
from the list.
min( ) min(list) Returns the element with the minimum value
from the list
sum( ) sum(list) It returns the sum of elements of the list.
max( ) max(list) Returns the element with the maximum value
from the list.
min( ) min(list) Returns the element with the minimum value
from the list
sum( ) sum(list) It returns the sum of elements of the list.
MIND MAP OF LIST
>>>l.pop() >>>l.remove(20)
20
51
Ans. The similarity between Lists and Strings in Python is that both are sequences. The differences
between them are that firstly, Lists are mutable but Strings are immutable. Secondly, elements
of a list can be of different types whereas a String only contains characters that are all of
String type.
Q.6 Write a program to accept a list of numbers and find the sum of all the numbers in the list.
Q.7 WAP to search for an element in a given list of numbers.
Q.8 WAP to find the minimum element from a list of elements along with its index in the list.
Q.9 What will be the output of the following program:
l=[10,20,30,40,50,60]
for i in range(len(l)):
if(i%2==0):
print(l[i],end='#')
else:
print(l[i],end='@')
Q.10 What will be the output of following program:
odd = [1, 9]
odd.insert(1,3)
print(odd)
odd[2:2] = [5, 7]
print(odd)
Long Question
Q.1 Write a program that takes any two lists L and M of the same size and adds their elements
together to form a new list N whose elements are sums of the corresponding elements in L and M. For
instance, if L = [3, 1, 4] and M = [1, 5, 9], then N should equal [4,6,13].
Q.2 Write a Python program to input 10 numbers to store in the list and print the third largest
number. For example, if the entered numbers in the list are List are 36, 25, 14, - 951, 75, - 85,
654, 88, 9521, 657, then output will be The third largest number is : 654
Q.3 Take a list of 10 elements. Split it into middle and store the elements in two different lists.
E.g.- INITIAL list : 5 10 6 12 3 25 66 44 1 90 After splitting : 5 10 6 12 3
25 66 44 1 90
Q.4 Find and write the output of the following Python code :
52