You are on page 1of 2

assignment 2

In [1]: #Ql
#a)Empty Tuple
# defining an empty tuple
empty_tuple = ()

print(type(empty_tuple))
#b)Tuple having integer value
# defining a tuple with integer value
int_tuple = (1, 2, 3)

print(type(int_tuple))
#c)Tuple with mix data type
# defining a tuple with mixed data types
mix_tuple = (1, 'two', 3.0, False)

print(type(mix_tuple))
#d)Nested Tuple
# defining a nested tuple
nested_tuple = (1, 2, ('a', 'b', 'c'), 3)

print(type(nested_tuple))
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>

In [2]: #Q2
tuple_name = ("Green", "Orange", "Blue")
new_tuple = tuple_name + ("Red",)

print(tuple_name)
( 'Green' , 'Orange' , 'Blue')

In [3]: #Q3

tuple = (10, 20, "KIIT", 30, "BSC")

# To find the index of the element 10


index_of_10 = tuple.index(10) if 10 in tuple else None
print("Index of 10 in the tuple:", index_of_10)

# To find the index of the element "KIIT"


index_of_kiit = tuple.index("KIIT") if "KIIT" in tuple else None
print("Index of \"KIIT\" in the tuple:", index_of_kiit)

# To find the index of the element 30


index_of_30 = tuple.index(30) if 30 in tuple else None
print("Index of 30 in the tuple:", index_of_30)
Index of 10 in the tuple: 0
Index of "KIIT" in the tuple: 2
Index of 30 in the tuple: 3

In [4]: #Q4

tuple = (10,20,30,"BSC", 40, "KIIT", 50)

# To display (10, 20, 30)


print(tuple[:3])

# To display (40, "KIIT", 50)

localhost:8889/nbconvert/html/Downloads/lDS/assignment2.ipynb?download=false 1/2
3/14/24, 2:04 PM assignment2
print(tuple[4:])

# To display (30, "BSC", 40, "KIT")


print(tuple[2:6])

# To display 50 to 10
print(tuple[::-1])
(10, 20, 30)
(40, 'KIIT', 50)
( 30, 'BSC', 40, 'KIIT')
(50, 'KIIT', 40, 'BSC', 30, 20, 10)

In [5]: #Q5

students = [
{"Name": "John", "Mark": 90},
{"Name": "Emma", "Mark": 85},
{"Name": "Jim", "Mark": 95},
{"Name": "Jane", "Mark": 88},
{"Name": "Lucy", "Mark": 98}

for i in range(0, 5):


print(students[i].keys(), ' => ', students[i].values())
dict_keys(['Name', 'Mark']) => dict_values(['John', 90])
dict_keys(['Name', 'Mark']) => dict_values(['Emma', 85])
dict_keys(['Name', 'Mark']) => dict_values(['Jim', 95])
dict_keys(['Name', 'Mark']) => dict_values(['Jane', 88])
dict_keys(['Name', 'Mark']) => dict_values(['Lucy', 98])

In [6]: #Q6
n = 10
for 1 1n range(l, n+l):
print('(', i, ', ', i**2, ') ')

( 1 1 )
( 2 4 )
( 3 9 )
( 4 16 )
( 5 , 25 )
( 6 , 36 )
( 7 49 )
( 8 , 64 )
( 9 , 81 )
( 10 , 100

In [ ]:

2/2

You might also like