1. What would be the output of the following Python code?
list = ['a', 'b', 'c', 'd', 'e']print (list[::2])print (list[::-1])
Ans:-
['a', 'c', 'e']
['e', 'd', 'c', 'b', 'a']
2. Write a program to sort a list of filenames based on the extension.
files = ["test.py", "program.c", "sample.pl", "hello.cpp"]#Expected output ['program.c', 'hello.cpp', 'sample.pl', 'test.py']
Ans:-
output: ['program.c', 'hello.cpp', 'sample.pl', 'test.py']
3. Complete the Body of the get_negative function
def get_negatives(array):passmylist=[0,-1,2,3,-4,5,6,-7,8]print(get_negatives(mylist))mylist2=[0,1,2]print(get_negatives(mylist2))mylist3=[-1,-9,-4]print(get_negatives(mylist3))mylist4=[]print(get_negatives(mylist4))
Ans:-
1. Method1
def get_negatives(array):lst = []for x in array:if x<0:lst.append(x)return lst
2. Method2
def get_negative(array):
return [x for x in array if x<0]
4. what would be the output of following ?
print([x for x in range(20) if x%3 == 0])
Ans:-
[0, 3, 6, 9, 12, 15, 18]
5. What would be the output of the following code?
class Parent():
def __init__(self):
print(10)
def SayHi(self):
print("Hi")
class Child(Parent):
def __init__(self):
super().__init__()
print(20)
def SayHello(self):
self.SayHi()
print("Hello")
p = Parent()
p.SayHi()
c = Child()
c.SayHello()
Ans:-
10
Hi
10
20
Hi
Hello
Copy the code and execute- analyze the flow of this code. This question is important in terms of your basic understanding of the OOP concept called inheritance.