This is a Python Program to create a list of tuples with the first element as the number and the second element as the square of the number.
The program takes a range and creates a list of tuples within that range with the first element as the number and the second element as the square of the number.
1. Take the upper and lower range for the numbers from the user.
2. A list of tuples must be created using list comprehension where the first element is the number within the range and the second element is the square of the number.
3. This list of tuples is then printed.
4. Exit.
Here is source code of the Python Program to create a list of tuples with the first element as the number and the second element as the square of the number. The program output is also shown below.
l_range=int(input("Enter the lower range:")) u_range=int(input("Enter the upper range:")) a=[(x,x**2) for x in range(l_range,u_range+1)] print(a)
1. User must enter the upper and lower range for the numbers.
2. List comprehension must be used to create a list of tuples where the first number is the number itself from the given range and the second element is a square of the first number.
3. The list of tuples which is created is printed.
Case 1: Enter the lower range:1 Enter the upper range:4 [(1, 1), (2, 4), (3, 9), (4, 16)] Case 2: Enter the lower range:45 Enter the upper range:49 [(45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401)]
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs
- Check Python Books
- Apply for Python Internship