Q1. Write a program which checks whether given element exists within a tuple.
= int(input("Enter Tuple Length : "))
n = []
numbers
for _ in range(n):
= int(input("Enter List Item (Integer) : "))
temp
numbers.append(temp)
=tuple(numbers)
tprint(t)
= int(input("Enter Tuple Item to Search : "))
item 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)
= int(input("Enter the value"))
a = int(input("Enter the value"))
b
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:
=[]
marksdef getData (self,name,rn,age,gender,m1,m2,m3):
= name
student.name = rn
student.rn = age
student.age = gender
student.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)
= input("Enter the Name of the Student:")
name = input("Enter the Roll Number:")
rn = input("Enter the Age:")
age = input("Enter the Gender:")
gender = int(input("Enter the marks in first subject:"))
m1 = int(input("Enter the marks in second subject:"))
m2 = int(input("Enter the marks in third subject:"))
m3
= student()
s1
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