#python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
Python Practice Loops1
In this class, we do python practice loops1 examples.
Practice Examples Loops
Q1) What is the output displayed by the program given below?
def function(list1,list2):
list1.sort()
list2.sort()
i=0
j=0
list3=[]
while(ilen(list1) and jlen(list2)):
if(list1[i]list2[j]):
list3.append(list1[i])
i+=1
else:
list3.append(list2[j])
j+=1
return list3
list1=[15,8,2,11,7,9]
list2=[32,1,9,7,20]
z=function(list1,list2)
print(z)The examples which we discuss here will help you in a deeper understanding of the concepts of python.
These placement training examples will help to improve your coding skills. Try to solve it on your own. Then check for a solution.
Below we will provide an intuition to analyze the code.
In the above program, They have taken two lists, List1 and List2.
list1=[15,8,2,1,7,9] list2=[32,1,9,7,20]
They sent those lists to function.
In the function, they sorted the elements in the list.
After sorting, the lists are in ascending order shown below.
list1=[1,2,7,8,9,15] list2=[1,7,9,20,32]
i=0, j=0, list3=[] variables taken in the program.
From the while loop statement while(ilen(list1) and jlen(list2)) we can observe that they are looping in both the lists.
To loop in the lists, they are using the variables i and j.
The if-else statement shows they compare the elements in both the list and moving the smallest to list3.
Comparing the element present in position i in list1 with the element present in position j in list2.
The smallest element is moved to list3, and the corresponding variable is incremented.
While loop will execute till one of the lists is finished moving its elements to list3.
The output given below is the final.
Output: [1,2,7,7,8,9,9,11,1].
Q2) What is the output displayed by the program given below?
list1=[["18kn1a0508","rajesh",25],["18kn1a0509","sindhu",22],["18kn1a0510","mallesh",28]]
n=len(list1)
for i in range(n-1,-1,-1):
for j in range(0,i):
if(list1[j][1]list1[j+1][1]):
temp=list1[j]
list1[j]=list1[j+1]
list1[j+1]=temp
print(list1)
The input taken by the above program is a list within the list.
The input list within the list contains data about student details.
They are using a nested loop.
The outer loop is moving in the list from end to start position.
The inner loop moves in the list from start to the position mentioned by the variable i present in the outer loop.
This program logic is the same as the Bubble sort program. Click here.
The same logic they are using here to sort the elements based on the name of the student.
The student name is in the first position in the nested list. so the condition is given list1[j][1]list1[j+1][1].
Output: [[“18kn1a0510″,”mallesh”,28],[“18kn1a0508″,”rajesh”,25],[“18kn1a0509″,”sindhu”,22]].
The output is the list sorted based on the student name.
Link for playlists:
/ @wisdomerscse
Link for our website: https://learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ learningmonkey01@gmail.com
On this page of the site you can watch the video online Python Practice Loops1 || Lesson 13 || Python Placements || Learning Monkey || with a duration of hours minute second in good quality, which was uploaded by the user Wisdomers - Computer Science and Engineering 29 May 2021, share the link with friends and acquaintances, this video has already been watched 131 times on youtube and it was liked by 8 viewers. Enjoy your viewing!