Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python

The document contains multiple code snippets with different aims related to working with tuples in Python. The code snippets take tuples as input, perform various operations on the tuples like finding the largest/smallest number, counting frequencies, removing duplicates, and outputting the results.

Uploaded by

monishchess2468
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python

The document contains multiple code snippets with different aims related to working with tuples in Python. The code snippets take tuples as input, perform various operations on the tuples like finding the largest/smallest number, counting frequencies, removing duplicates, and outputting the results.

Uploaded by

monishchess2468
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NAME: MONISH NAIDU

CLASS:11B

Aim: To create Fibonacci series in a tuple


Input:
n=int(input("enter number:"))

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:

if len(i)>=2 and i[0]==i[-1]:

count+=1

print(count)

Output:
enter tuple("abc","121","tat","dad")

Aim: To find the second largest number in a tuple


Input:
t=eval(input("Enter tuple: "))

max,smax=0,0

for i in t:

if i>max:

max=i

if i>smax and i<max:

smax=i

print("Largest is ",max,"Second largest is ",smax)

Output:
Enter tuple: (1,2,3,40,10)

Largest is 40 Second largest is 10

Aim: To find even elements in a tuple


Input:
t=eval(input("Enter tuple: "))

for i in t:

if i%2==0:

print(i,end=" ")

Output:

Enter tuple: (1,2,6,2,35)

262

Aim: To find the largest/smallest number in a tuple


Input:
t=eval(input("Enter tuple: "))

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: "))

x=int(input("enter element to search for"))

for i in range(len(t)):

if t[i]==x:

print("Element found at ",i)

break

else:

print("Element not found")

Output:
Enter tuple: (1,2,3,4,5)

enter element to search for5

Element found at 4

Aim: To display frequency of each item in a tuple


Input:
t=eval(input("enter tuple"))

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

Aim: Given a tuple of cars storing car names as elements, cars


=("toyota","honda","tata","Ford","BMW","volkswagen","mercedes","ferarri","
porsche") print the names in the range 2 to 6 both included
Input:
cars=("toyota","honda","GM","Ford","BMW","volkswagen","mercedes","ferarri","porsche")

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

Aim: To create nested tuple to store marks in 3 subjects for 5 students


Input:
marks=((45,45,40),(35,40,38),(36,30,38),(25,27,20),(10,15,20))

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

1 Total: 113 Avg: 37.666666666666664

2 Total: 104 Avg: 34.666666666666664

3 Total: 72 Avg: 24.0

4 Total: 45 Avg: 15.0

Aim: To find median of tuple


Input:
t=eval(input("Enter tuple:"))

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

Aim: To input tuple of numbers and swap elements in even location


with elements at odd location
Input:

t=eval(input("Enter tuple: "))

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:

Enter tuple: (1,2,3,4,5)

(2, 3, 4, 5, 1)

Aim: To remove duplicates from a tuple

Input:

t=eval(input("Enter tuple: "))

t1=()
for i in t:

if i not in t1:

t1+=(i,)

print(t1)

Output:

Enter tuple: (1,1,1,2,3,3,4,4,4,2,2,4,2)

(1, 2, 3, 4)

Aim: To count the number of times a character appears in a dictionary


Input:
count=0

n=int(input("Enter number of key value pairs: "))

d={}

for i in range(n):

key=input("Enter key ")

value=input("Enter value ")

d[key]=value

ch=input("Enter character to be counted: ")

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 value plane

Enter key 2
Enter value car

Enter key 3

Enter value train

Enter character to be counted: a

Aim: To convert a number entered by user into corresponding number in words


Input:
n=int(input("Enter number"))

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

You might also like