# Python Sort || Python Code

Python is simple and vast programming language. Python Code is easy when contrasted with a wide range of dialects like java, C++ and so on .All we need to do is to deal with the indentation of the code .Here we give many basic examples for the beginners to illustrate the Python Sort by many different sorting algorithms and python while loop and many other examples to understand how this language works in easy and efficient way.

## Example#1:

Write a python code to sort the list in ascending order.
l = [12, 98, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 95, 46, 63, 21, 65, 48, 8, 15 , 85, 22, 28, 11, 83,
88, 11, 76, 6,83,27]

CODE:

i = 0
while i<len(l):
j = 0
while j<len(l)-i-1:
if l[j]>l[j+1]:
temp = l[j]
l[j]=l[j+1]
l[j+1]=temp
j+=1
i+=1
print l

OUTPUT:

[6, 6, 8, 11, 11, 11, 13, 15, 21, 22, 22, 27, 27, 28, 28, 46, 48, 63, 65, 66, 67, 76, 76, 83, 83, 83, 84, 85, 88, 95, 98]

## Example#2:

Write a python code to sort the list in descending order.
l = [13, 99, 6, 76, 11, 83, 27, 84, 28, 67, 66, 22, 96, 46, 63, 21, 65, 48, 8, 14 , 84, 22, 28, 11, 83,
87, 11, 76, 6,83,27]

CODE:

i = 0
while i<len(l):
j = 0
while j<len(l)-i-1:
if l[j]<l[j+1]:
temp = l[j]
l[j]=l[j+1]
l[j+1]=temp
j+=1
i+=1
print l

OUTPUT:

[99, 96, 87, 84, 84, 83, 83, 83, 76, 76, 67, 66, 65, 63, 48, 46, 28, 28, 27, 27, 22, 22, 21, 14, 13, 11, 11, 11, 8, 6, 6]

## Example#3:

Write a python code to check two lists, L1 and L2, are equal or not (their
corresponding elements are equal or not)
L1= [2, 6, 8, 8, 6, 42]
L2= [7, 4, 67, 89, 36, 2]
i = 0

CODE:

i = 0
match= True
while i<len(L1) and match:
if L1[i]!=L2[i]:
match = False
break
i+=1
if match:
print “Equal”
else:
print “Not Equal”

OUTPUT:

## Example#4:

Write a python code to add L1 and L2.
L1= [2, 6, 8, 8, 6, 42]
L2= [7, 4, 67, 89, 36, 2]

CODE:

new_L = L1[:]
i = 0
while i<len(L1):
new_L[i] = L1[i]+L2[i]
i+=1
print new_L

OUTPUT:

## Example#5:

Write a python code to add L1 and L2 by reading L1 in forward direction and L2 in  reverse order.
L1= [2, 6, 8, 8, 6, 42]
L2= [7, 4, 67, 89, 36, 2]

CODE:

new_L = L1[:]
i = 0
while i<len(L1):
new_L[i] = L1[i]+L2[len(L2)-i-1]
i+=1
print new_L

OUTPUT:

## Example#6:

Write a python code to take a string as input and display each character of given
# string along with is index. (Hint: a string is also a list of characters)

CODE:

string = raw_input(“Enter a string: “)
i = 0
while i<len(string):
print i, string[i]
i+=1

OUTPUT:

## Example#7

Write a python code that
# prints true if given character is present in list else print
# false e.g. print TRUE for any given character is present in sentence “This is ITC lab”. Take both
# sentence and character as input.

CODE:

string = raw_input(“Enter a string: “).lower()
char = raw_input(“Enter character to check for: “).lower()
i = 0
exist = False
while i<len(string) and not(exist):
if string[i] ==char[0]:
exist = True
i+=1
if exist:
print”Exists”
else:
print”Doesn’t Exists”

OUTPUT: