Q 7 jrgeshestm teejthjkljklh srthklkltjsjkghsegbn tjhrhrthgkjerjklhbgebgjasb nerjks
P.I.P: - 7.1
Q1. Consider the following code: what is the value of ‘a’ at the end?
a = [1, 2, 3]
a [2] = 0
a [a [ 2 ] ] = 5
a [1:2] = []
Answer =
Value of ‘a’ at the end is:
[5, 0]
Q2. You have a list a = [1, 2, [3, 4], 5]. Now answer the following. Verify your answers in Python Shell.
|
(a) |
What is the length of a? |
4 |
|
(b) |
How many elements does it contain? What are their types? |
It contain 4 element. 3 integer type & 1 list. |
|
(c) |
How many integers? |
3 integer |
|
(d) |
What does len(a[2]) return? |
2 |
|
(e) |
What does a[len(a)] evaluate to? |
It give error. |
|
(f) |
What does a [-1 * len(a)] evaluate to? |
Output:- 1 |
Q4. Given a list List1 = [1, 2, 3].
Run the following two statements for the list List1 and explain how these two statements are different:
List1 += 4 # statement
List1 += [4] # statement
Explanation
Answer =
Because in statement 1 there is addition of list and integer which is not possible. While in statement 2 there is addition of two lists which is possible.
P.I.P: - 7.2
Q4. Write a program that returns the largest even number in the list of integers. If there is no even number in the input, print "No even element".
Examples:
With list[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9] it will print 6
With list(3, -1001] it will print "No even element" 20
With list[ 6, 18, 23, 20, 29] it will print 20
Write the program code.
Answer =
lst = eval (input("Enter the number list :- "))
newlst = []
for i in lst :
if i % 2 == 0 :
newlst += [ i ]
if newlst == [] :
print ("No even element")
else :
newlst.sort()
print ("largest even number in the list is :- ", newlst[-1])
Q5. Write a program that prints the sum of the even-indexed elements of L, minus the sum of the odd-indexed elements of L.
For example:
For list[1, 2, 3, 4, 5, 6] it should print -3 (that is, (1 + 3 + 5) - (2 + 4 + 6))
For list([1, 2, 3]) it should print 2 (that is, 1 - 2 + 3)
Answer =
L = eval (input("Enter the number list :- "))
even = odd = 0
for i in range (len(L)) :
if i % 2 == 0 :
even += L[ i ]
else :
odd += L[ i ]
print ("(sum of even-indexed elements) - (sum of odd-indexed elements) :-", even - odd)
Q6. Given a list of strings L, write a program below that prints a single string containing all of the elements of L concatenated (joined) together with a hyphen in between two elements.
Answer =
lst = eval (input("Enter a List :-"))
string = ""
for i in range (len(lst)) :
if i == len(lst) - 1 :
string += lst[i]
else :
string = string + lst[ i ] + "-"
print ("New word is :- ", string)
Q7. Predict the output:-
(a)
lst = [1, 2, 3]
myvar = lst[0]
lst[0] = 18
print (lst)
print (myvar)
(b)
myvar = 18
lst = [myvar, 2, 3]
myvar = 22
print (lst)
print (myvar)
(c)
a = [1, 2, [3, 4], 5]
print(len(a))
print(-1 * len(a))
print(a[2], len(a[2]))
(d)
a = [1, 2, 3]
a[2]=0
a[a[2]] = 5
a[1:2] = []
print(a)
(e)
a = [ 55, 44, 33, 22, 11, 0]
print(a[0] )
print(a[-1] )
print(a[a[0]] )
print( a[a[-1]])
print(a[a[a[a[2]+1]]] )
(f)
for x in [1, 2]:
for y in [3, x]:
print (x, y)
Answer =
Output:-
(a)
[18, 2, 3]
1
>>>
(b)
[18, 2, 3]
22
>>>
(c)
4
-4
[3, 4] 2
>>>
(d)
[5, 0]
>>>
(e)
55
0
Traceback (most recent call last):
File "C:\Desktop\Q7.py", line 4, in <module>
print(a[a[0]] )
IndexError: list index out of range
>>>
(f)
1 3
1 1
2 3
2 2
>>>
Q8. Write a program to find the median, from a given list.
Answer =
lst = eval(input ("Enter the List :- "))
lst.sort()
if len(lst) % 2 == 0 :
print ("Median :- ", lst[ int (len(lst) / 2 )])
else :
print ("Median :- ", lst[ int ((len(lst) + 1) / 2 )])
Q9. Write a program to find the mode, from a given list.
Answer =
lst = eval(input ("Enter the List :- "))
feq = []
for i in lst :
feq += [lst.count(i)]
feq.sort()
for i in lst :
if feq[-1] == lst.count(i):
print("Mode = ",i)
break
Q10. Write a program to find the range, from a given list, e.g., if the given list is:
23,9,14,2,28, 19, 3,15,9,25,2, 4, 9
Then your code should print 26.
Hint. Range is the difference between the largest and lowest number. In this case 2 is largest is 28 and lowest is 2. So 28 - 2 is 26.
Answer =
lst = eval(input ("Enter the List :- "))
lst.sort()
print ("Range is :- ", lst[ len(lst) - 1] - lst[ 0 ])
Q11. Cumulative sum of a list [a, b, c...] is defined as [a, a + b, a + b + c, ...). Write a program to input a list of number and then create another list from this list that contains cumulative sum of numbers in list1.
Answer =
lst = eval(input ("Enter the List :- "))
lst1 = [ ]
sum = 0
for i in lst :
sum += i
lst1 += [ sum ]
print(lst1)
tfghthjj
ReplyDeletejghhk
ReplyDeletetfguitgui
ReplyDelete