Unit 2
'''
3. Write a program to understand various methods of array class mentioned:
append, insert, remove, pop, index, tolist and count'''
import array as arr
a=arr.array('i',[])
n=int(input("Enter How Many Element You Eant : "))
for i in range(n):
a.append(int(input("Enter Element : ")))
print("Element in array are : ",a)
i,n=[int(j) for j in input("Enter Position of insert Element : ").split()]
a.insert(i,n)
print("After Insertion Element Are : ",a)
rem=int(input("Remove : "))
a.remove(rem)
print(f"After Removal of {rem} element are {a}")
print("Element Deleted Pop is : ",a.pop())
print("After deletion Element are : ",a)
temp=int(input("Enter Element Search in array : " ))
print(f"Element is {temp} is a Position of{a.index(temp)} in array")
lst=a.tolist()
print("Convrted array into list is : ",lst)
cnt=int(input("Enter Element to find total occurence in array : "))
print(f"{cnt} occured : {a.count(ant)} times")
'''
4. Write a program to sort the array elements using bubble sort technique. '''
import array as arr
a=arr.array('i',[])
n=int(input("enter How Many Element You want : "))
for i in range(n):
a.append(int(input("Enter Element : ")))
print("Before Swaping : ",a)
for i in range(n):
for j in range(0,n-i-1):
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j]
print("sorted list = ",a)
'''
Write a python program that removes any repeated items from a list so that each
item appears at most once. For instance, the list [1,1,2,3,4,3,0,0]
would become [1,2,3,4,0]
'''
l = [1,1,2,3,4,3,0,0]
print("old list :",l)
s= set(l)
l1 = list(s)
print("new list :",l1)
#Write a program to show variable length argument and its use
def fun(a,*b):
s=0
for i in b:
s += int(i)
print("total :",s+a)
fun(1,2)
print("-----------")
fun(1,2,3,4,5)
#Write a lambda/Anonymous function to find bigger number in two given numbers
x=lambda a,b: a if a>b else b
a = int(input("enter a: "))
b = int(input("enter b: "))
print("biggest no is :",x(a,b))
'''
Create a program name “employee.py” and implement the functions
DA, HRA, PF ITAX. Create another program that uses the function of
employee module and calculates gross and net salaries of an employee
'''
import ex as f
f.da(500)
f.hra(550)
f.pf(600)
f.itax(900)
'''
Write a program to create a list using range functions and perform
append, update and delete elements operations in it. '''
l = []
n = int(input("how manny no:"))
for i in range(n):
l.append(int(input("enter NO:")))
print(l)
l[1] = 22
print("afte Update ",l)
del l[2]
print("afte del ",l)
'''
Create a sample list of 7 elements and implement the List methods mentioned:
append, insert, copy, extend, count, remove, pop, sort, reverse and clear. '''
l=[]
n= int(input("how manny ho :"))
for i in range(n):
l.append(int(input("enter no:")))
print(l)
p= int(input("enter possion: "))
no= int(input("enter no : "))
print("afte inserted:",l)
l.insert(p,no)
print(l)
p= int(input("enter search no: "))
ind = l.index(p)
print("posion is :",ind)
'''
Write a program to accept elements in the form of a tuple and display
its minimum, maximum, sum and average
'''
t = (1,2,3,48,0)
print(t)
print(min(t))
print(max(t))
print(sum(t))
avg = sum(t)/len(t)
print(avg)
d1={}
n = int(input("how manny"))
for i in range(n):
a=input("enter name")
b=input("scor")
d1.update({a:b})
print("player list")
for i in d1.keys():
print(i)
#get() thi value return karse.. // -1 defualt che...
name=input("ebter playter name for his score")
run=d1.get(name,-1)
if run == -1:
print("not found")
else:
print(f"{name} - {run}")