Python Practice Loops1

In this class, we do python practice loops1 examples.

For Complete YouTube Video: Click Here

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(i<len(list1) and j<len(list2)):
         if(list1[i]<list2[j]):
             list3.append(list1[i])
             i+=1
             print("i=",i)
         else:
             list3.append(list2[j])
             j+=1
             print("j=",j)
     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 deeper understanding in 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 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(i<len(list1) and j<len(list2)) we can observe that they are looping in both the lists .

In order to loop in the lists they are using the variables i and j.

The if else statements shows they are comparing the elements in both the list and moving the smallest element to list3.

Comparing the element present in position i in list1 with element present in position j in list2.

The element which is smaller is moved to list3 and the corresponding variable is incremented.

While loop will execute till one of the list finished moving its elements to list3.

The final output is given below.

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 above program taking input list with in the list.

The input list within the list contain data about student details.

They are using nested loop.

The outer loop is moving in the list from end to start position.

The inner loop is moving in the list from start to the position mentioned by variable i present in outer loop.

This logic we had seen in Bubble sort. Click here.

The same logic they are using here to sort the elements based on 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 student name.