Python Slip 14

d208f87fca274f3e834740644d6e0ac6

Q1. Write a program which checks whether given element exists within a tuple.

n = int(input("Enter Tuple Length : "))
numbers = []

for _ in range(n):
    temp = int(input("Enter List Item (Integer) : "))
    numbers.append(temp)

t=tuple(numbers)
print(t)
item = int(input("Enter Tuple Item to Search : "))
if item in t:
    print("Item Present in Tuple")
else:
    print("Item Not Present in Tuple")

OUTPUT

Enter Tuple Length : 5
Enter List Item (Integer) : 6
Enter List Item (Integer) : 2
Enter List Item (Integer) : 3
Enter List Item (Integer) : 4
Enter List Item (Integer) : 15
(6, 2, 3, 4, 15)
Enter Tuple Item to Search : 5
Item Not Present in Tuple

Q2. Write a Python program to find the greatest common divisor (gcd) of two integers.

def gcd(a, b):
    if(b == 0):
        return a
    else:
        return gcd(b, a % b)

a = int(input("Enter the value"))
b = int(input("Enter the value"))
  
print("The gcd of",a," and ",b," is : ", end=" ")
print(gcd(a, b))

OUTPUT

Enter the value60
Enter the value28
The gcd of 60  and  28  is :  4

Q2. Define a class Student having members – rollno, name, age, gender. Create a subclass called ―Test with member marks of 3 subjects. Create three objects of the Test class and display all the details of the student with percentage.

class student:
    marks=[]
    def getData (self,name,rn,age,gender,m1,m2,m3):
        student.name = name
        student.rn = rn
        student.age = age
        student.gender = gender
        student.marks.append(m1)
        student.marks.append(m2)
        student.marks.append(m3)

    def displayData(self):
        print("Name is ",student.name)
        print("Roll No. is ",student.rn)
        print("Age is ",student.age)
        print("Gender is ",student.gender)
        print("Marks are ",student.marks)
        print("Total marks are ",self.total())
        print("Percentage is ",round(self.per(),2))

    def test(self):
        print("Marks in 1st subject ",student.marks[0])
        print("Marks in 2nd subject ",student.marks[1])
        print("Marks in 3rd subject ",student.marks[2])

    def total(self):
        return (m1+m2+m3)

    def per(self):
        return ((m1+m2+m3)/3)

name = input("Enter the Name of the Student:")
rn = input("Enter the Roll Number:")
age = input("Enter the Age:")
gender = input("Enter the Gender:")
m1 = int(input("Enter the marks in first subject:"))
m2 = int(input("Enter the marks in second subject:"))
m3 = int(input("Enter the marks in third subject:"))

s1 = student()
s1.getData(name,rn,age,gender,m1,m2,m3)
s1.test()
s1.displayData()

OUTPUT

Enter the Name of the Student:Rahul
Enter the Roll Number:101
Enter the Age:24
Enter the Gender:Male
Enter the marks in first subject:68
Enter the marks in second subject:78
Enter the marks in third subject:88
Marks in 1st subject  68
Marks in 2nd subject  78
Marks in 3rd subject  88
Name is  Rahul
Roll No. is  101
Age is  24
Gender is  Male
Marks are  [68, 78, 88]
Total marks are  234
Percentage is  78.0

0 Comments:

Post a Comment