Python Advance Programs by Devansh Mishra.

HERE ARE SOME ADVANCE PYTHON PROGRAMS MOST USEFUL FOR PYTHON BEGINNER'S--


1. Displaying Number Pyramid


rows = int(input('Enter the number of rows'))

for i in range(rows):
    for j in range(i):
        print(i, end=' ')
    print('')
     


2.Prgram to find numbers which are divisible by 7 and multiple of 5 in the range 1500 & 2700

        

nl=[]
for x in range(1500, 2701):
    if (x%7==0) and (x%5==0):
        nl.append(str(x))
print (','.join(nl))
     
3. Bubble Sort using Python


import sys

def bubble_sort(arr): 
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                temp = arr[j]
                arr[j] = arr[j+1]
                arr[j+1] = temp 
    return arr

if __name__=='__main__':

    arr = [2, 1, 9, 3, 7, 5, 6, 4, 8, 0]
    print("Sorted array: ")
    print(bubble_sort(arr))

     
 4. Insertion Sort using Python


import sys

def insertion_sort(arr): 
    n = len(arr)
    for i in range(1, n):
        key = arr[i]
        j = i-1
        while j >= 0 and key < arr[j]:
            arr[j+1] = arr[j]
            j -= 1 

        arr[j+1] = key
        
    return arr

if __name__=='__main__':

    arr = [24, 17, 66, 33, 72, 47, 68, 41, 105, 30]
    print("Sorted array: ")
    print(insertion_sort(arr))

     
 5. Selection Sort


import sys

def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_index = i
        for j in range(i+1, n):
            if arr[min_index] > arr[j]:
                min_index = j
        if i != min_index:
            temp = arr[i]
            arr[i] = arr[min_index]
            arr[min_index] = temp 
    return arr
    
if __name__=='__main__':

    arr = [21, 15, 96, 37, 72, 54, 68, 41, 85, 30]
    print("Sorted array: ")
    print(selection_sort(arr))

     
6. Linear Search using Python


import sys

def linear_search(arr, num_find):
    position = -1
    for index in range(0, len(arr)):
        if arr[index] == num_find:
            position = index
            break

    return (position)

if __name__=='__main__':
    
    arr = [10, 7, 2, 13, 4, 52, 6, 17, 81, 49]
    num = 13
    found = linear_search(arr, num)
    if found != -1:
        print('Number %d found at position %d'%(num, found+1))
    else:
        print('Number %d not found'%num)
     
7. Implementation of stack 

        

stack = []

stack.append('p')
stack.append('r')
stack.append('q')

print('Initial stack')
print(stack)

(stack.pop())

print('\nAfter Poped')
print(stack)
 
     
8. Implementation of queue 

        

que = []  
  
que.append('USA')  
que.append('INDIA')  
que.append('RUSSIA')  
  
print(que)  

print(que.pop(0))  
 
     
 9. Addition of two matrices

        

A = [[2,7,9],
    [7 ,8,6],
    [4 ,8,9]]

B = [[5,9,1],
    [9,7,4],
    [2,5,1]]

addition = [[0,0,0],
            [0,0,0],
            [0,0,0]]

for i in range(len(A)):
   for j in range(len(A[0])):
       addition[i][j] = A[i][j] + B[i][j]

print("Addition Of Matrix :")
for r in addition:
    print(r) 
     

10. program to find the repeated items of a tuple.


        

tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7 
print(tuplex)
count = tuplex.count(4)
print(count)


11. program to reverse items of a tuple

        

my_tuple = (1,2,8, 789,"Codemic")
my_tuple = my_tuple[::-1]
print(my_tuple)


12. Python program to add , update, delete element in a tuple

        

contact = {} 

inputLis = ["1", "cool 123456789", 
   "y", "2", "cool", "n"] 
indi = -1


def input(): 
 global indi 
 indi += 1
 print(inputLis[indi]) 
 return inputLis[indi] 

def delete(): 
 global contact 
 print("Enter the contact" "\n" " name to be deleted") 

 name = input().strip() 

 if name in contact: 
  del(contact[name]) 
  print("Contact Deleted !\n") 
 else: 
  print("Contact not found !\n") 

 print("Do you want to perform more" "\n" 
  " operations? (y / n)") 

 choice = input().strip() 
 if choice == "y": 
  main() 
 
def update(): 
 global contact 
 print("Enter the contact name" "\n" " to be updated - ") 

 name = input().strip() 

 if name in contact: 
  print("Enter the new" "\n" " contact number - ") 
  phone = int(input()) 

  contact[name] = phone 

  print("Contact updated\n") 
 else: 
  print("Contact not found !\n") 

 print("Do you want to perform " "\n" "more operations? (y / n)") 

 choice = input().strip() 
 if choice == "y": 
  main() 

def search(): 
 global contact 
 print("Enter the name to be searched - ") 

 name = input().strip() 

 if name in contact: 
  print("Contact Found !") 
  print(name, contact[name]) 
 else: 
  print("Contact not found !\n") 


 print("Do you want to perform more" "/n"
  " operations? (y / n)") 

 choice = input().strip() 
 if choice == "y": 
  main() 
 
def store(): 
 print("\n\nEnter the name" "\n" " and phone number"+ "\n" " separated by space - ") 

 name, phone = map(str, \ input().strip() \ .split(" ")) 

 global contact 
 if name in contact: 
  print("Contact Already exists !\n") 
 else: 
  contact[name] = phone 
  print("Contact Stored !") 

 print("Do you want to perform more"\ 
  " operations? (y / n)") 

 choice = input().strip() 
 if choice == "y": 
  main() 

 
def main(): 
 print("Please choose any choice"\ " from below -\n\n\n") 
 print("Store Contact number (1)") 
 print("Search Contact number (2)") 
 print("Update Contact number (3)") 
 print("Delete Contact number (4)") 

 choice = int(input()) 

 choice_dict = { 
  1: store, 
  2: search, 
  3: update, 
  4: delete 
 } 

 choice_dict[choice]() 


if __name__ == "__main__": 

main() 


 13. PROGRAM to implement add, update, delete, search, display all students functions for a file. Store rollno, name, totalmarks of a student


        

class Student: 
 def __init__(self, name, rollno, m1, m2): 
  self.name = name 
  self.rollno = rollno 
  self.m1 = m1 
  self.m2 = m2 
  
 def accept(self, Name, Rollno, marks1, marks2 ): 
  ob = Student(Name, Rollno, marks1, marks2 ) 
  ls.append(ob) 
  
 def display(self, ob): 
   print("Name : ", ob.name) 
   print("RollNo : ", ob.rollno) 
   print("Marks1 : ", ob.m1) 
   print("Marks2 : ", ob.m2) 
   print("\n")  
    
 def search(self, rn): 
  for i in range(ls.__len__()): 
   if(ls[i].rollno == rn): 
    return i  
         
 def delete(self, rn): 
  i = obj.search(rn) 
  del ls[i] 

 def update(self, rn, No): 
  i = obj.search(rn) 
  roll = No 
  ls[i].rollno = roll; 
   
ls =[] 

obj = Student('', 0, 0, 0) 

print("\nOperations used, ") 
print("\n1.Accept Student details\n2.Display Student Details\n" /
"3.Search Details of a Student\n4.Delete Details of Student" /
 / "\n5.Update Student Details\n6.Exit") 

obj.accept("A", 1, 100, 100) 
obj.accept("B", 2, 90, 90) 
obj.accept("C", 3, 80, 80) 
  
print("\n") 
print("\nList of Students\n") 
for i in range(ls.__len__()):  
 obj.display(ls[i]) 
    
print("\n Student Found, ") 
s = obj.search(2) 
obj.display(ls[s]) 
  
obj.delete(2) 
print(ls.__len__()) 
print("List after deletion") 
for i in range(ls.__len__()):  
 obj.display(ls[i]) 
   
obj.update(3, 2) 
print(ls.__len__()) 
print("List after updation") 
for i in range(ls.__len__()):  
 obj.display(ls[i]) 
   
print("Thank You !") 



Comments

Popular posts from this blog

SWAG ANIMATION WITH SOUND

Chandrayan 3 Mission Special :Animation

Python Basic Programs By Devansh Mishra.