Assignment # 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Assignment

#4
1.Write a program to find the maximum element from a tuple of numbers entered by
the user along with the index of the largest element.

Code:
t=eval(input("Enter the tuple: "))
x=max(t)
print("The largest element:",x)
for i in range(len(t)):
if t[i]==x:
print("The index of largest element is:",i)

Output:
Enter the tuple: (1,30,99,875,764)
The largest element: 875
The index of largest element is: 3
>>>
2.Write a program to create two lists from a tuple which stores the names and marks
of students, depending on their datatype.
For example, if the tuple T = ("Aryan",90,"Saksham",83,"Ishan",66) the lists created
should be as follows:
List1 = ["Aryan","Saksham","Ishan"]
List2 = [90,83,66]
The output should be:
Aryan - 90
Saksham - 83
Ishan - 66

Code:
t=eval(input("Enter the tuple: "))
l=[]
f=[]
for i in t:
if type(i)==(int or float):
l.append(i)
elif type(i)== str:
f.append(i)
for x in range(len(l)):
print(f[x],"-",l[x])

Output:
Enter the tuple: ("Aryan",90,"Saksham",83,"Ishan",66)
Aryan - 90
Saksham - 83
Ishan - 66
>>>
3.Write a program to input a tuple T and print the second largest element in the
tuple.

Code:
t=eval(input("Enter the values: "))
b=[]
a=max(t)
for i in t:
if i<a:
b.append(i)
x=max(b)
print("The second largest number is",x)

Output:
Enter the values: (28,95,68,75,46,83)
The second largest number is 83
>>>
4.Write a program to count the frequency of values in a tuple.
For example, if the tuple T = (1,2,3,4,2,3,5,6,1)
Output:
1 - Frequency 2
2 - Frequency 2
3 - Frequency 2
4 - Frequency 1
5 - Frequency 1
6 - Frequency 1

Code:
t=eval(input("Enter the tuple: "))
f=[]
for i in t:
if i not in f:
f.append(i)
for x in f:
print(x,"- Frequency",t.count(x))

Output:
Enter the tuple: (1,2,3,4,2,3,5,6,1)
1 - Frequency 2
2 - Frequency 2
3 - Frequency 2
4 - Frequency 1
5 - Frequency 1
6 - Frequency 1
>>>
5.Write a program to input an integer n and create a tuple with n terms of the
Fibonacci series.

Code:
n=eval(input("Enter the integer: "))
f=[0,]
a,b=0,1
for i in range(n):
a,b=b,a+b
f.append(a)
print(tuple(f))

Output:
Enter the integer: 20
(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,
4181, 6765)
>>>
6.Write a program which inputs an integer x and an integer n and creates a tuple
containing x, x2, x3,x4,x5….. xn . Also display the tuple so created.

Code:
x=eval(input("Enter a number: "))
n=int(input("Enter an integer: "))
l=[]
for i in range(1,n+1):
l.append(x**i)
print(tuple(l))

Output:
Enter a number: 2
Enter an integer: 6
(2, 4, 8, 16, 32, 64)
>>>
7.Write a program which creates a tuple containing the squares of all integers from 1
through N using a for loop.

Code:
n=int(input("Enter an integer: "))
l=[]
for i in range(1,n+1):
l.append(i**2)
print(tuple(l))

Output:
Enter an integer: 26
(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324,
361, 400, 441, 484, 529, 576, 625, 676)
>>>

You might also like