Q. Create the following tuple using a for loop :
(i) A tuple containing the squares of the integer 1 though 50.
(ii) The tuple (“a”, “bb”, “ccc”….)that ends with 26 copies of the letter z.
You can understand by Watching video :-
Answer :-
(i)
tup = () for i in range(1,51): tup = tup + ( i**2,) print(tup)
(ii)
tup = () for i in range(1 , 27): tup = tup + ( chr(i + 96 )* i ,) print(tup)
Output :-
(i)
(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500)
>>>
(ii)
('a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz')
>>>
in the second code it should be 97 instead of 96
ReplyDeleteOk, Sorry for wrong answer.
DeleteWhy, 96 is absolutely fine.
Deleteit was correct, it is supposed to be 96 only instead of 97. I Checked it.
ReplyDeleteOk, Now I have Corrected all scripts.
Deletethank you so much for helping out, really greateful<3
ReplyDeleteWelcome 😊
DeleteFirst one is wrong, it's asking to print the squares of numbers not the numbers. It should be
ReplyDeleteFor i in range (1,51):
a=(i)**2
tup= tup + (a,)
print (tup)
Oh, Sorry I have corrected it.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )