Python Tuple: Exercise-1 With Solution: Write A Python Program To Create A Tuple
Python Tuple: Exercise-1 With Solution: Write A Python Program To Create A Tuple
Sample Solution:-
Python Code:
#Create an empty tuple
x = ()
print(x)
tuplex = tuple()
print(tuplex)
Sample Output:
()
()
Sample Solution:-
Python Code:
#Create a tuple with different data types
print(tuplex)
Sample Output:
('tuple', False, 3.2, 1)
Write a Python program to create a tuple with numbers and print one item.
Sample Solution:-
Python Code:
#Create a tuple with numbers
print(tuplex)
tuplex = 5,
print(tuplex)
Sample Output:
(5, 10, 15, 20, 25)
(5,)
Sample Solution:-
Python Code:
#create a tuple
tuplex = (4, 6, 2, 8, 3, 1)
print(tuplex)
#tuples are immutable, so you can not add new elements
#using merge of tuples with the + operator you can add an element and it will create
a new tuple
print(tuplex)
print(tuplex)
listx = list(tuplex)
listx.append(30)
tuplex = tuple(listx)
print(tuplex)
Sample Output:
(4, 6, 2, 8, 3, 1)
(4, 6, 2, 8, 3, 1, 9)
(4, 6, 2, 8, 3, 15, 20, 25, 4, 6, 2, 8, 3)
(4, 6, 2, 8, 3, 15, 20, 25, 4, 6, 2, 8, 3, 30)
Sample Solution:-
Python Code:
tup = ('e', 'x', 'e', 'r', 'c', 'i', 's', 'e', 's')
str = ''.join(tup)
print(str)
Sample Output:
exercises
Write a Python program to get the 4th element and 4th element from last of a
tuple.
Sample Solution:-
Python Code:
#Get an item of the tuple
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print(tuplex)
item = tuplex[3]
print(item)
item1 = tuplex[-4]
print(item1)
Sample Output:
('w', 3, 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e')
e
u
Sample Solution:-
Python Code:
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print("r" in tuplex)
print(5 in tuplex)
Sample Output:
True
False
Sample Solution:-
Python Code:
#create a tuple
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
print(tuplex)
count = tuplex.count(4)
print(count)
Sample Output:
(2, 4, 5, 6, 2, 3, 4, 4, 7)
3
Python Code:
#Convert list to tuple
print(listx)
#use the tuple() function built-in Python, passing as parameter the list
tuplex = tuple(listx)
print(tuplex)
Sample Output:
[5, 10, 7, 4, 15, 3]
(5, 10, 7, 4, 15, 3)
Sample Solution:-
Python Code:
#create a tuple
print(tuplex)
#using merge of tuples with the + operator you can remove an item and it will create
a new tuple
print(tuplex)
listx.remove("c")
tuplex = tuple(listx)
print(tuplex)
Sample Output:
('w', 3, 'r', 's', 'o', 'u', 'r', 'c', 'e')
('w', 3, 's', 'o', 'u', 'r', 'c', 'e')
('w', 3, 's', 'o', 'u', 'r', 'e')
Sample Solution:-
Python Code:
#create a tuple
tuplex = (2, 4, 3, 5, 4, 6, 7, 8, 6, 1)
#used tuple[start:stop] the start index is inclusive and the stop index
slice = tuplex[3:5]
#is exclusive
print(_slice)
#if the start index isn't defined, is taken from the beg inning of the tuple
_slice = tuplex[:6]
print(_slice)
#if the end index isn't defined, is taken until the end of the tuple
_slice = tuplex[5:]
print(_slice)
_slice = tuplex[:]
print(_slice)
_slice = tuplex[-8:-4]
print(_slice)
print(tuplex)
#tuple[start:stop:step]
_slice = tuplex[2:9:2]
print(_slice)
_slice = tuplex[::4]
print(_slice)
_slice = tuplex[9:2:-4]
print(_slice)
Sample Output:
(5, 4)
(2, 4, 3, 5, 4, 6)
(6, 7, 8, 6, 1)
(2, 4, 3, 5, 4, 6, 7, 8, 6, 1)
(3, 5, 4, 6)
('H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D')
('L', 'O', 'W', 'R')
('H', 'O', 'R')
('L', ' ')
Python tuple: Exercise-12 with Solution
Sample Solution:-
Python Code:
#create a tuple
print(tuplex)
index = tuplex.index("p")
print(index)
index = tuplex.index("p", 5)
print(index)
index = tuplex.index("e", 3, 6)
print(index)
index = tuplex.index("y")
Sample Output:
('i', 'n', 'd', 'e', 'x', ' ', 't', 'u', 'p', 'l', 'e')
8
8
3
Traceback (most recent call last):
File "d0e5ee40-30ab-11e7-a6a0-0b37d4d0b2c6.py", line 14, in
<module>
index = tuplex.index("y")
ValueError: tuple.index(x): x not in tuple
Python tuple: Exercise-13 with Solution
Sample Solution:-
Python Code:
#create a tuple
tuplex = tuple("w3resource")
print(tuplex)
print(len(tuplex))
Sample Output:
('w', '3', 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e')
10
Sample Solution:-
Python Code:
#Create a new empty set
x = set()
print(x)
n = set([0, 1, 2, 3, 4])
print(n)
Sample Output:
set()
{0, 1, 2, 3, 4}
Sample Solution:-
Python Code:
#Create a set
for n in num_set:
print(n)
Sample Output:
0
1
2
3
4
5
Sample Solution:-
Python Code:
#A new empty set
color_set = set()
color_set.add("Red")
print(color_set)
color_set.update(["Blue", "Green"])
print(color_set)
Sample Output:
{'Red'}
{'Red', 'Blue', 'Green'}
Sample Solution:-
Python Code:
num_set = set([0, 1, 3, 4, 5])
num_set.pop()
print(num_set)
num_set.pop()
print(num_set)
Sample Output:
{1, 3, 4, 5}
{3, 4, 5}
Python sets: Exercise-5 with Solution
Write a Python program to remove an item from a set if it is present in the set.
Sample Solution:-
Python Code:
#Create a new set
#Discard number 4
num_set.discard(4)
print(num_set)
Sample Output:
{0, 1, 2, 3, 5}
Sample Solution:-
Python Code:
#Intersection
print(setz)
Sample Output:
{'blue'}
Sample Solution:-
Python Code:
#Union
print(seta)
Sample Output:
{'yellow', 'green', 'blue'}
Sample Solution:-
Python Code:
setx = set(["apple", "mango"])
print(setz)
#Set difference
print(setb)
Sample Output:
{'mango'}
{'apple'}
Sample Solution:-
Python Code:
setp = set(["Red", "Green"])
setq = setp.copy()
print(setq)
setq.clear()
print(setq)
Sample Output:
{'Green', 'Red'}
set()
Write a Python program to find maximum and the minimum value in a set.
Sample Solution:-
Python Code:
#Create a set
print(max(seta))
print(min(seta))
Sample Output:
20
2
Sample Solution:-
Python Code:
#Create a set
print(len(seta))
Sample Output:
6
Python dictionary: Exercise-1 with Solution
Sample Solution:-
Python Code:
d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)
Sample Output:
{0: 10, 1: 20}
{0: 10, 1: 20, 2: 30}
Sample Solution:-
Python Code:
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
def is_key_present(x):
if x in d:
else:
is_key_present(5)
is_key_present(9)
Sample Output:
Key is present in the dictionary
Key is not present in the dictionary
Write a Python script to print a dictionary where the keys are numbers between 1
and 15 (both included) and the values are square of keys.
Sample Solution:-
Python Code:
d=dict()
for x in range(1,16):
d[x]=x**2
print(d)
Sample Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10:
100, 11: 121, 12: 144, 13: 169, 14: 196, 15:
225}
Sample Solution:-
Python Code:
d1 = {'a': 100, 'b': 200}
d = d1.copy()
d.update(d2)
print(d)
Sample Output:
{'x': 300, 'y': 200, 'a': 100, 'b': 200}
Sample Solution:-
Python Code:
d = {'Red': 1, 'Green': 2, 'Blue': 3}
Sample Output:
Red corresponds to 1
Blue corresponds to 3
Green corresponds to 2
Python Code:
my_dict = {'data1':100,'data2':-54,'data3':247}
print(sum(my_dict.values()))
Sample Output:
293
Sample Solution:-
Python Code:
my_dict = {'data1':100,'data2':-54,'data3':247}
result=1
result=result * my_dict[key]
print(result)
Sample Output:
-1333800
Python Code:
keys = ['red', 'green', 'blue']
print(color_dictionary)
Sample Output:
{'green': '#008000', 'blue': '#0000FF', 'red': '#FF0000'}
Sample Solution:-
Python Code:
color_dict = {'red':'#FF0000',
'green':'#008000',
'black':'#000000',
'white':'#FFFFFF'}
Sample Output:
black: #000000
green: #008000
red: #FF0000
white: #FFFFFF
Write a Python program to get the maximum and minimum value in a dictionary.
Sample Solution:-
Python Code:
my_dict = {'x':500, 'y':5874, 'z': 560}
Sample Output:
Maximum Value: 5874
Minimum Value: 500
Sample Solution:-
Python Code:
from heapq import nlargest
print(three_largest)
Sample Output:
['e', 'b', 'c']
Write a Python program to get the key, value and item in a dictionary.
Sample Solution:-
Python Code:
dict_num = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
Sample Output:
key value count
1 10 1
2 20 2
3 30 3
4 40 4
5 50 5
6 60 6