In this program, we will sort a list using for loop in Python.
Steps to solve the program
- Take a list as input.
- Use for loop with range function to iterate over list values.
- Use a nested for loop to iterate over a value and remaining values of the list.
- If the selected value is greater than any value from the list then assign that element to a variable.
- Change the value of the grater value by smaller value and smallervalue by greater value.
- Print the output.
l = [6,8,2,3,1,0,5]
for i in range(len(l)):
for j in range(i,len(l)):
if l[i]>l[j]:
temp=l[i]
l[i]=l[j]
l[j]=temp
print(l)
Output :
[0, 1, 2, 3, 5, 6, 8]