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

Wed 4-03-2025 Python

The document contains Python programming exercises that demonstrate various functions, including reversing a tuple, converting a list of tuples into a dictionary, and removing empty tuples from a list. It also includes tasks for calculating the cube and factorial of a number using four different methods each. Sample outputs for each task are provided to illustrate the expected results.

Uploaded by

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

Wed 4-03-2025 Python

The document contains Python programming exercises that demonstrate various functions, including reversing a tuple, converting a list of tuples into a dictionary, and removing empty tuples from a list. It also includes tasks for calculating the cube and factorial of a number using four different methods each. Sample outputs for each task are provided to illustrate the expected results.

Uploaded by

abhaykatre70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

3/4/25, 10:47 PM Abhay_CS23047_assignment.

ipynb - Colab
for num, letter in t :
numbers.append(num)
letters.append(letter)
print(numbers)
print(letters)

[1, 2, 3]
['a', 'b', 'c']

33. Write a Python program to reverse a tuple.

t=(1,2,3,4,5,6)
print(t[::-1])

(6, 5, 4, 3, 2, 1)

34. Write a Python program to convert a list of tuples into a dictionary.

tuple_list = [('a', 1), ('b', 2), ('c', 3)]


my_dict = dict(tuple_list)
print(my_dict)

{'a': 1, 'b': 2, 'c': 3}

35. Write a Python program to remove an empty tuple(s) from a list of tuples. Sample data: [(), (), (‘’,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’)] Expected
output: [(‘’,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), ‘d’]

tuples_list = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
t1 = []
for t in tuples_list:
if t:
t1.append(t)
print(t1)

[('',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]

36 . Write a Python program to convert a given string list to a tuple. Original string: python 3.0 <class ‘str’> Convert the said string to a tuple:
(‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ’n’, ‘3’, ‘.’, ‘0’)

t1 = ["python 3.0"]
print(tuple(t1))

('python 3.0',)

keyboard_arrow_down Wednesday Task 05/03/2025


1. wap to find cube of given number using all four method.

# Function without arguments and without return value


def cube1():
num = int(input("Enter a number: "))
print("Cube:", num ** 3)

cube1()

Enter a number: 3
Cube: 27

# Function without arguments but with return value


def cube2():
num = int(input("Enter a number: "))
return num ** 3

result = cube2()
print("Cube:", result)

Enter a number: 5
Cube: 125

# Function with arguments but without return value


def cube3(num):
print("Cube:", num ** 3)

https://colab.research.google.com/drive/1YkHdpv4W36nPAonTkFou_KhqPE6U57A6#scrollTo=kKAcp66IKQyy&printMode=true 23/25
3/4/25, 10:47 PM Abhay_CS23047_assignment.ipynb - Colab

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


cube3(n)

Enter a number: 2
Cube: 8

# Function with arguments and with return value


def cube4(num):
return num ** 3

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


result = cube4(n)
print("Cube:", result)

Enter a number: 7
Cube: 343

2. Wap to find factorial of number using all four method

# Function without arguments and without return value


def fact1():
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)

fact1()

Enter a number: 2
Factorial: 2

# Function without arguments but with return value


def fact2():
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
return fact

result = fact2()
print("Factorial:", result)

Enter a number: 0
Factorial: 1

# Function with arguments but without return value


def fact3(num):
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)

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


fact3(n)

Enter a number: 4
Factorial: 24

# Function with arguments and with return value


def fact4(num):
fact = 1
for i in range(1, num + 1):
fact *= i
return fact

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


result = fact4(n)
print("Factorial:", result)

Enter a number: 6
Factorial: 720

Double-click (or enter) to edit


add Code add Text

format_size format_bold format_italic code link image format_quote format_list_numbered format_list_bulleted horizontal_rule ψ mood
https://colab.research.google.com/drive/1YkHdpv4W36nPAonTkFou_KhqPE6U57A6#scrollTo=kKAcp66IKQyy&printMode=true 24/25

You might also like