Solution
Solution
Output
LONDON
NEW YORK
2.Write a function LeftShift(lst, x) in Python which accepts numbers in a list
(lst) and all the elements of the list should be shifted to left according to the
value of x.
Sample Input Data of the list lst = [20, 40, 60, 30, 10, 50, 90, 80, 45, 29]
where x = 3
Output lst = [30, 10, 50, 90, 80, 45, 29, 20, 40, 60]
Solution
def LeftShift(lst, x):
if x >= len(lst):
x %= len(lst)
shifted_lst = lst[x:] + lst[:x]
return shifted_lst
Output
Enter the list: [20, 40, 60, 30, 10, 50, 90, 80, 45, 29]
Enter the value of x: 3
[30, 10, 50, 90, 80, 45, 29, 20, 40, 60]
Enter the list: [55, 66, 77, 88, 99, 44, 33, 22, 11]
Enter the value of x: 4
[99, 44, 33, 22, 11, 55, 66, 77, 88]
3. Write a method EvenSum(NUMBERS) to add those values in the tuple of
NUMBERS which are even.
Solution
def EvenSum(NUMBERS):
even_sum = 0
for number in NUMBERS:
if number % 2 == 0:
even_sum += number
return even_sum
Output
Enter the tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
The sum of even numbers in the tuple is: 30
4. Write a program using user-defined function to calculate and display
average of all the elements in a user-defined tuple containing numbers.
Solution
def calculate_average(num):
total = 0
for element in num:
total += element
average = total / len(num)
return average
Output
Enter the tuple: (2, 4, 6, 8, 10)
The average of the elements in the tuple is: 6.0
5. A dictionary stu contains rollno and marks of students. Two empty lists
stack_roll and stack_mark will be used as Stack. Two functions Push_stu()
and Pop_stu() are defined and perform the following operation:
Push_stu(): It reads dictionary stu and adds keys into stack_roll and values
into stack_marks for all students who secured more than 60 marks.
Pop_stu(): It removes last rollno and marks from both list and prints
"underflow" if there is nothing to remove.
For example,
stu={ 1 : 56, 2 : 45, 3 : 78, 4 : 65, 5 : 35, 6 : 90 }
Values of stack_roll and stack_mark after push_stu() function:
[3, 4, 6] and {78, 65, 90}
#Solution
def Pop_na(Lnameage):
if Lnameage:
removed_name, removed_age = Lnameage.pop()
print("The name removed is", removed_name)
print("The age of person is", removed_age)
else:
print("Underflow - Nothing to remove")