Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

L1.

py

"""

Python program to create list of odd number from n to m using range function

and print the elements of a list in forward and backward direction.

Find the length of the list.

"""

n=int(input(" Enter a starting limit:"))

m=int(input("Enter a ending limit:"))

if n<m:

l=list(range(n,m+1,2))

print("Length of list is",len(l))

print("List is:",l)

print("Elements of list in forward direction:")

for i in l:

print(i)

print("Elements of list in backward direction:")

for i in range(-1,-(len(l)+1),-1):

print(l[i])

l3.py

"""

python program to find a list with unique elements

"""

n=int(input("Enter a length of list:"))

l=[]

for i in range(n):

x=int(input("Enter a element:"))

l.append(x)

print("Entered list is:",l)

uq=[]

for i in l:
if i not in uq:

uq.append(i)

print("List with unique list:",uq)

l4.py

"""

Python program to read a list of student names in 3 sem

and a list of student names in 5 sem. find common names in both list

"""

n=int(input("Number of students in 3 sem:"))

s3=[]

print("Enter a student names of 3 sem:")

for i in range(n):

s3.append((input("Enter a name:")))

m=int(input("Number of students in 5 sem:"))

s5=[]

print("Enter a student names of 5 sem:")

for i in range(m):

s5.append((input("Enter a name:")))

print("students in 3 sem are:",s3)

print("students in 5 sem are:",s5)

comnames=[]

for i in s3:

if i in s5:

comnames.append(i)

print("Common names in both list are:",comnames)


l5.py

"""

Python program to read a n Employee details

which includes name, empid salary and designation.

Retrieve a particular employee details. And count

no of employee with each designation

"""

n=int(input("Enter a number of employees:"))

print("Enter Employee Details:")

emp=[]

for i in range(n):

emp.append(int(input("Entter a Employee id:")))

emp.append(input("Enter a employee Name:"))

emp.append(float(input("Enter a Salary:")))

emp.append(input("Enter a Desgination:"))

x=int(input("Enter a employee id:"))

for i in range(len(emp)):

if x==emp[i]:

print("Employee id is:",emp[i])

print("Employee Name is:",emp[i+1])

print("Employee Salary is:",emp[i+2])

print("Employee Designation is:",emp[i+3])

break

else:

print("Employee Record is not present")

des=[]

for i in range(3,len(emp),4):

if emp[i] not in des:

des.append(emp[i])
k=3

print(des)

for i in des:

print("No of employee with designation {} is {}".format(i,emp.count((emp[k]))))

k=k+4

l6.py

"""

Python program to read a list of at least three engineering courses

and for each course read at least three subjects belongs to that course.

Find total no of subjects that starts with letter p

"""

course=[]

n=int(input("Enter a no of engineering courses:"))

if n>=3:

for i in range(n):

course.append(input("Enter a course:"))

while True:

m=int(input("Enter a number of subjects:"))

if m>=3:

s=[]

for k in range(m):

s.append(input("Enter a subject:"))

course.append(s)

break

else:

print("minimum subjects must be 3:")

else:
print("Minimum course must be 3:")

print(course)

count=0

for i in range(1,len(course),2):

for j in course[i]:

if j.startswith('p') or j.startswith('P'):

count=count+1

print("Total number of courses that starts with letter p are:",count)

"""

Python program two read a matrix of order m and n

and find a transpose of matrix and print it in matrix form.

"""

m,n=input("Enter a number of rows and column of a matrix:").split(',')

mat=[]

m=int(m)

n=int(n)

print("Enter a elements of matrix:")

for i in range(m):

col=[]

print("Enter a",i+1,"row elements:")

for j in range(n):

col.append(input())

mat.append(col)

print("Elements of matrix before Transpose:")

for i in range(m):

for j in range(n):

print(mat[i][j],end=' ')

print()
print("Transpose of a matrix:")

for i in range(n):

for j in range(m):

print(mat[j][i],end=' ')

print()

t2.py

"""

Python program to create a tuple with n names

and print all names in a tuple in alphabetical order.

Find second minimum and maximum alphabetical ordered name

"""

n=int(input("Enter a length:"))

names=[]

for i in range(n):

names.append(input("Enter a name:"))

names=tuple(names)

print("A tuple with {} names{}".format(n,names))

names=('suma','ram','ravi')

names_s=sorted(names)

names_s=('ram','ravi','suma')

print("Names in a tuple in alphabetical order:")

for k in names_s:

print(k)

print("Second minimum alphabetical order name is:",names_s[1])

print("Second maximum alphabetical order name is:",names_s[-2])


t3.py

"""

Python program to create a tuple of n integer numbers

and convert it to a singleton tuples of values.

Example t=(1,2,3) must be converted as t=((1,),(2,),(3,))

"""

n=int(input("Enter a length:"))

l=[]

for i in range(n):

l.append(int(input("Enter a number:")))

print("Tuple is:",tuple(l))

x=[]

for k in l:

m=tuple((k,)) #m=k, m=tuple(1,) m=(1,)

x.append(m) #x=[(1,),(2,),(3,)]

print("Tuple converted into a singleton tuple", tuple(x))

t4.py

"""

Python program to read a tuple with n student details.

Where every element of a tuple is a tuple which contains name,usn,sem,marks for each student.

Sort the nested tuple on name of student

sort the nested tuple on marks

"""

n=int(input("Enter number of students:"))

stud=[]

for i in range(n):

det=[]

print("Enter a details of {} student".format(i+1))


det.append(input("Enter a name:"))

det.append(input("Enter a uns number:"))

det.append(input("Enter a semester:"))

det.append(float(input("Enter a marks:")))

det=tuple(det)

stud.append(det)

stud=tuple(stud)

print("Nested tuple with student details is:",stud)

stud_s=sorted(stud)

print("Sorted nested tuple is:",tuple(stud_s))

stud_s1=sorted(stud,key=lambda x:x[3])

print("Sorted nested tuple is:",tuple(stud_s1))

t5.py

"""

Python program to perform following operations on a tuple.

Inserting a new element at given position

Modifying a element at given position

Deleting a element from given position

"""

n=int(input("Enter a limit:"))

l=[]

for i in range(n):

l.append(int(input("Enter a number:")))

print("Created tuple is:",tuple(l))

print("1:insert 2: modify, 3:delete")

x=int(input("Enter a choice:"))

if x==1:

y=int(input("Enter a new element:"))

pos=int(input("Enter a position:"))
c=l

if pos<len(l):

l=l[:pos]

l.append(y)

l=l+c[pos:]

else:

l.append(y)

print("Tuple after inserting a new element:",tuple(l))

elif x==2:

y=int(input("Enter a new element:"))

pos=int(input("Enter a position:"))

c=l

if pos<len(l):

l=l[:pos]

l.insert(pos,y)

l=l+c[pos+1:]

else:

print("It is not in a range")

print("Tuple after modifcation :",tuple(l))

elif x==3:

pos=int(input("Enter a position:"))

if pos<len(l):

l=l[:pos]+l[pos+1:]

else:

print("It is not in a range")

print("Tuple after deletion:",tuple(l))

else:

print("Invalid chocie:")

You might also like