0% found this document useful (0 votes)
7 views7 pages

CS HHW

The document contains a series of Python programming exercises focused on string and list operations, including indexing, slicing, and modifying lists. It also includes tasks for counting characters, checking title case, removing characters, summing digits, and finding elements in lists. Additionally, it covers concepts like appending, extending lists, and removing duplicates.

Uploaded by

piwile2631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

CS HHW

The document contains a series of Python programming exercises focused on string and list operations, including indexing, slicing, and modifying lists. It also includes tasks for counting characters, checking title case, removing characters, summing digits, and finding elements in lists. Additionally, it covers concepts like appending, extending lists, and removing duplicates.

Uploaded by

piwile2631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Consider the following string mySubject:


mySubject = "Computer Science"
What will be the output of the following string operations?

(i) print(mySubject[0:len(mySubject)])
Ans- Computer Science

(ii} print(mySubject[-7:-1])
Ans- Scienc

(iii) print(mySubject[::2])
Ans- Cmue cec

iv. print(mySubject[len(mySubject)-1])
Ans- e

v. print(2*mySubject)
Ans- Computer ScienceComputer Science

vi. print(mySubject[::-2])
Ans- eniSrtpo

vii. print(mySubject[:3] + mySubject[3:])


Ans- Computer Science

viii. print(mySubject.swapcase())
Ans- cOMPUTER sCIENCE

ix. print(mySubject.startswith('Comp'))
Ans- True

x. print(mySubject.isalpha())
Ans- False

2. Consider the following string myAddress:


myAddress = "WZ-1,New Ganga Nagar,New Delhi"
What will be the output of following string operations :

i. print(myAddress.lower())
Ans wz-1,new ganga nagar,new delhi

ii. print(myAddress.upper())
Ans WZ-1,NEW GANGA NAGAR,NEW DELHI

iii. print(myAddress.count('New'))
Ans 2
iv. print(myAddress.find('New'))
Ans 5

v. print(myAddress.rfind('New'))
Ans 21

vi. print(myAddress.split(','))
Ans ['WZ-1','New Ganga Nagar','New Delhi']

vii. print(myAddress.split(' '))


Ans ['WZ-1,New','Ganga','Nagar,New','Delhi']
viii. print(myAddress.replace('New','Old'))
Ans WZ-1,Old Ganga Nagar,Old Delhi

ix. print(myAddress.partition(','))
Ans ('WZ-1',','New Ganga Nagar,New Delhi')

x. print(myAddress.index('Agra'))
Ans Value error will occur mentinoning that substring was not found.

3. Write a program to input line(s) of text from the user. Count the total number
of
characters in the text (including white spaces), total number of alphabets, total
number of
digits, total number of special symbols and total number of words in the given
text. (Assume
that each word is separated by one space).

st=input("Enter String:")

char=0
alphabets=0
digits=0
space=0
symbols=0

for ch in st:
if ch.isalpha():
alphabets +=1
elif ch.isdigit():
digits +=1
elif ch.isspace():
space +=1
else:
symbols +=1
print("Characters:",char)
print("Alphabets:",alphabets)
print("Digits:",digits)
print("Words:",space+1)
print('Symbols:",symbols)

4. Write a program to input a string with more than one word and check whether it
is in title
case or not, if yes print message “In title case” otherwise convert to title case
and print this
string. (Title case means that the first letter of each word is capitalised)

st=input("Enter string:")
if st==st.title():
print("In title case")
else:
print(st.title())

5. Write a program which takes two input one is a string and other is a character.
The function should create a new string after deleting all occurrences of the
character from
the string and print the new string.

ST=input("Enter string:")
ch=input("Enter character")
print(ST.replace(ch,''))

6.Input a string having some digits. Write a program to print the sum of digits
present in
this string.

k=str(input("Enter number:"))
Sum=0
for i in k:
Sum += int(i)
print("Digits sum:",Sum)

7. Write a program that takes a sentence as an input where each word in the
sentence is
separated by a space. The program should replace each blank with a hyphen and then
print
the modified sentence.

Sentence=input("Enter sentence:")
print(Sentence.replace(" ","-"))

List

1. What will be the output of the following statements?


i. list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)

Output: [10,12,26,32,65,80]

ii. list1 = [12,32,65,26,80,10]


sorted(list1)
print(list1)

Output: [12,32,65,26,80,10]

iii. list1 = [1,2,3,4,5,6,7,8,9,10]


list1[::-2]
list1[:3] + list1[3:]

Output: [10,8,6,4,2]
[1,2,3,4,5,6,7,8,9,10]

iv. list1 = [1,2,3,4,5]


list1[len(list1)-1]

Output: 5

2. Consider the following list myList. What will be the elements of myList after
the following
two operations:
myList = [10,20,30,40]
i. myList.append([50,60])

Output: [10,20,30,40,[50,60]]

ii. myList.extend([80,90])
Output: [10,20,30,40,[50,60],80,90]

3. What will be the output of the following code segment:


myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(myList)):
if i%2 == 0:
print(myList[i])

Output: 1
3
5
7
9

4. What will be the output of the following code segment:


a. myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)

Output: [1,2,3]

b. myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)

Output: [6,7,8,9,10]

c. myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)

Output: [2,4,6,8,10]

5. Differentiate between append() and extend() functions of list, explain with


example.

Append() function refers to adding any but single element at the last of a
list,whereas extend() function is used to add all the elements of one list into
another list.
For example we take a list,
lst=[12,3,4,5,64,32,46]

Now, if we want to add 16 to lst,we use append function.


lst.append(16)

Output will be [12,3,4,5,64,32,46,16]

Now if we want to add all the elements of say lst1(another list) to lst then we use
extend function.
lst1=[3,5,23,241,325356,133,5323]
lst.extend(lst1)

Output will be [12,3,4,5,64,32,46,16,3,5,23,241,325356,133,5323]

6. Consider a list:
list1 = [6,7,8,9]
What is the difference between the following operations on list1:
a. list1 * 2
b. list1 *= 2
c. list1 = list1 * 2

Operation (a) will result in the duplication of all elements of the list1 and
result in the following output as: [6,7,8,9,6,7,8,9]

Operation (b) and (c) are the same operations,it's just that operation (b) is the
sliced form of operation(c).
These two operations or sort of a single operation will change the list1 and assign
the list with repeated elements i.e. [6,7,8,9,6,7,8,9]
to list1.

7. The record of a student (Name, Roll No., Marks in five subjects and percentage
of
marks) is stored in the following list:
stRecord = ['Raman','A-36',[56,98,99,72,69],78.8]
Write Python statements to retrieve the following information from the list
stRecord.
a) Percentage of the student
Ans: print(stRecord[3])

b) Marks in the fifth subject


Ans: list1=stRecord[2]
print(list1[4])

c) Maximum marks of the student


Ans: list1=stRecord[2]
print(max(list1))

d) Roll no. of the student


Ans: print(stRecord[1])

e) Change the name of the student from ‘Raman’ to ‘Raghav’.


Ans: stRecord[0]="Raghav"
print(stRecord)

8. Write a program to enter a list and an element, find the number of times an
element
occurs in the list without using inbuilt methods.

list1=list(eval(input("Enter elements")))
Element=int(input("Enter the digit or number you want to find:"))
count=0
for i in range(0,len(list1)):
if list1[i]==Element:
count += 1
print(count)

9. Write a program to read a list of n integers (positive as well as negative).


Create two new
lists, one having all positive numbers and the other having all negative numbers
from the
given list. Print all three lists.

list1=list(eval(input("Enter elements")))
list2=[]
list3=[]

for i in range(0,len(list1)):
if list1[i]<0:
list2.append(list1[i])
elif list1[i]>0:
list3.append(list1[i])
print(list1)
print(list2)
print(list3)

10. Write a program that prints the largest element of the list passed as
parameter.

list1=list(eval(input("Enter elements:")))
print(max(list1))

11. Write a program to return the second largest number from a list of numbers.

list1=list(eval(input("Enter elements:")))
list1.sort(reverse=True)
print(list1[1])

12. Write a program to read a list of n integers and find their median. You can use
an builtin function to sort the list.

list1=list(eval(input("Enter elements:")))
list1.sort(reverse=False)
print(list1)
k=len(list1)
for i in range(0,k):
if k%2==0:
median=(list1[(k//2)-1]+list1[((k+2)//2)-1])/2
else:
median=list1[((k+1)//2)-1]
print(median)

13. Write a program to read a list of elements. Modify this list so that it does
not contain any
duplicate elements, i.e., all elements occurring multiple times in the list should
appear only
once.

list1=list(eval(input("Enter elements:")))
list2=[]
for i in range(0,len(list1)):
if list1[i] not in list2:
list2.append(list1[i])
print(list2)

14. Write a program to read elements of a list.


a) The program should ask for the position of the element to be deleted from the
list. Write
a code to delete the element at the desired position in the list.

list1=list(eval(input("Enter elements:")))
print("Original list:",list1)
k=int(input("Enter Index value:"))
del list1[k-1]
print("Modified list:",list1)

b) The program should ask for the value of the element to be deleted from the list.
Write
code to delete the element of this value from the list.

list1=list(eval(input("Enter elements:")))
print("Original list:",list1)
k=int(input("Enter Index value:"))
list1.remove(k)
print("Modified list:",list1)

15. Read a list of n elements. Pass this list to a function which reverses this
list in-place
without creating a new list.

list1=list(eval(input("Enter elements:")))
print(list1[::-1])

You might also like