Python
Python
CLASS:11B
t=(0,1)
for i in range(0,n):
t+=(t[i]+t[i+1],)
print(t)
Output:
(0, 1, 1, 2, 3, 5, 8)
Aim: To count the number of strings where the string length is 2 or more and the
first and last character are same from a given list of strings.
Input:
t=eval(input("enter tuple"))
count=0
for i in t:
count+=1
print(count)
Output:
enter tuple("abc","121","tat","dad")
max,smax=0,0
for i in t:
if i>max:
max=i
smax=i
Output:
Enter tuple: (1,2,3,40,10)
for i in t:
if i%2==0:
print(i,end=" ")
Output:
262
max,min=0,t[0]
for i in t:
if i>max:
max=i
if i<min:
min=i
print(min,max)
Output:
Enter tuple: (1,2,3,4,5,6)
16
Aim: To input a tuple of elements, search for a given element in the tuple.
Input:
t=eval(input("Enter tuple: "))
for i in range(len(t)):
if t[i]==x:
break
else:
Output:
Enter tuple: (1,2,3,4,5)
Element found at 4
t1=()
count=0
i=0
for i in t:
if i not in t1:
t1+=(i,)
for k in t1:
for j in t:
if k==j:
count+=1
print(k,":",count)
count=0
Output:
enter tuple(1,1,1,2,2,2,3,4,2,4,5,6)
1:3
2:4
3:1
4:2
5:1
6:1
L=["one","two","three","four","five"]
for i in range(1,6):
print(L[i-1],":",cars[i])
Output:
one : honda
two : tata
three : Ford
four : BMW
five : volkswagen
sum,avg=0,0
for i in range(5):
sum,avg=0,0
for j in marks[i]:
sum=sum+j
avg=sum/3
print(i,"Total:",sum,"Avg:",avg)
Output:
0 Total: 130 Avg: 43.333333333333336
l=list(t)
l.sort
t1=tuple(l)
if len(t1)%2==0:
median=(t[(len(t1)//2)-1]+t[len(t1)//2])/2
else:
median=t[len(t1)//2]
print(median)
Output:
Enter tuple:(4,42,2,3,4,5)
2.5
l=list(t)
x=l[0]
for i in range(len(t)-1):
l[i],l[i+1]=l[i+1],l[i]
l[-1]=x
t1=tuple(l)
print(t1)
Output:
(2, 3, 4, 5, 1)
Input:
t1=()
for i in t:
if i not in t1:
t1+=(i,)
print(t1)
Output:
(1, 2, 3, 4)
d={}
for i in range(n):
d[key]=value
for j in d:
for k in d[j]:
if k==ch:
count+=1
print(count)
Output:
Enter number of key value pairs: 3
Enter key 1
Enter key 2
Enter value car
Enter key 3
d={1:"One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nine",10:"Ten"}
for i in d:
if i==n:
print(d[i])
break
Output:
Enter number6
six