Computer Science
Create a tuple containing the squares of the integers 1 through 50 using a for loop.
Answer
tup = ()
for i in range(1,51):
tup = tup + (i**2,)
print("The square of integers from 1 to 50 is:" ,tup)
Output
The square of integers from 1 to 50 is: (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)
Related Questions
Write a program that inputs two tuples and creates a third, that contains all elements of the first followed by all elements of the second.
Write a program as per following specification :
"'Return the length of the shortest string in the tuple of strings str_tuple.
Precondition: the tuple will contain at least one element."'Create a tuple ('a', 'bb', 'ccc', 'dddd', … ) that ends with 26 copies of the letter z using a for loop.
Given a tuple pairs = ((2, 5), (4, 2), (9, 8), (12, 10)), count the number of pairs (a, b) such that both a and b are even.