Define a function that can generate and print a tuple where the value is a square of numbers between 1 and 20 (both included)

Question:

Define a function that can generate and print a tuple where the value is a square of numbers between 1 and 20 (both included).

Hints:

  • Use the ** operator to get the power of a number.
  • Use range() for loops.
  • Use list.append() to add values into a list.
  • Use tuple() to get a tuple from a list.

Solution:

def printTuple(): li=list() for i in range(1,21): li.append(i**2) print tuple(li) printTuple()
Code language: Python (python)

Leave a Comment