0% found this document useful (0 votes)
15 views2 pages

Python Tuple Manipulation Programs

The document contains 6 programs demonstrating different operations that can be performed on tuples in Python. The programs cover converting a tuple to a string, checking if an element exists in a tuple, converting a list to a tuple, checking if all elements in a tuple are the same, finding the sum of even and odd indexed elements in a tuple, and summing the numeric elements in a variable number of arguments.

Uploaded by

shreyas soni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Python Tuple Manipulation Programs

The document contains 6 programs demonstrating different operations that can be performed on tuples in Python. The programs cover converting a tuple to a string, checking if an element exists in a tuple, converting a list to a tuple, checking if all elements in a tuple are the same, finding the sum of even and odd indexed elements in a tuple, and summing the numeric elements in a variable number of arguments.

Uploaded by

shreyas soni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Progra Write a Python program to convert a tuple to a string

m 1

Source print('Shreyas soni\n0827CS191224')


Code def convertTuple(tup):
str = ''
for item in tup:
str = str + item
return str
tuple = ('h', 'a', 'p', 'p', 'y')
str = convertTuple(tuple)
print(str)

Output Shreyas soni


0827CS191224
happy
Progra Write a Python program to check whether an input element
m 2
exists within a tuple.
Source print('Shreyas soni\n0827CS191224')
Code tuplex = ("p","y","t","h","o","n")
print("t" in tuplex)
print("r" in tuplex)

Output Shreyas soni


0827CS191224
True
False
Progra Write a Python program to convert a list to a tuple
m 3

Source print('Shreyas soni\n0827CS191224')


Code list_a = [5, 10, 15, 20, 25, 30]
print(list_a)
tuple_x = tuple(list_a)
print(tuple_x)

Output Shreyas soni


0827CS191224
[5, 10, 15, 20, 25, 30]
(5, 10, 15, 20, 25, 30)
Progra Define a function to check if all items in the tuple are the same.
m 4

Source print('Shreyas soni\n0827CS191224')


Code tup = ('Mon','Mon','Mon','Mon')
# Result from count matches with result from len()
result = [Link](tup[0]) == len(tup)
if (result):
print("All the elements of tuple are Equal")

else:
print("Elements are not equal")
Output Shreyas soni
0827CS191224
All the elements of tuple are Equal
Progra Write a function that takes a tuple of numbers. Return a two
m 5
element list, containing (respectively) the sum of the even
indexed numbers and the sum of the odd-indexed numbers. So
calling the function as even_odd_sums([10, 20, 30, 40, 50, 60]),
you’ll get back [90, 120].
Source print('Shreyas soni\n0827CS191224')
Code def even_odd_sums(list_a):
evens = sum(list_a[0::2])
odds = sum(list_a[1::2])
return [evens, odds]
a=([10,20,30,40,50,60])
ans=even_odd_sums(a)
print(ans)

Output Shreyas soni


0827CS191224
[90, 120]
Progra Write a function, sum_numeric , that takes any number of
m 6
arguments. If the argument is or can be turned into an integer,
then it should be added to the total. Arguments that can’t be
handled as integers should be ignored. The result is the sum of
the numbers. Thus, sum_numeric(10, 20, 'a', '30', 'bcd') would
return 60 . Notice that even if the string 30 is an element in the
list, it’s converted into an integer and added to the total.
Source print('Shreyas soni\n0827CS191224')
Code def sum_numeric(*args):
if not args:
if type(args[0]) is int:
return args
output = 0
for item in args:
if type(item) is int:
output+=item
elif [Link]():
output+=int(item)
return output
a=sum_numeric(10,20,'a','30','bcd')
print(a)

Output Shreyas soni


0827CS191224
60

You might also like