Here, we will figure out how to make three lists with the numbers, their squares and solid shapes in Python, where the scope of the numbers is given.
Take a range, for example, start and end, and we need to make three lists, list1 ought to contain numbers, list2 ought to contain squares of the numbers and list3 ought to contain 3D shapes of the numbers in Python.
Example:
Input:
Start = 1
End = 10
Output:
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
cubes : [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
Rationale:
- Pronounce three lists.
- Characterize run, here we are characterizing start with 1 and end with 10.
- Run a circle with the range as a range(start, end+1) and circle counter as a check.
- Affix the circle counter check to the list named numbers, annexe square to the list named squares and attach the shape to the list named blocks.
- At last, print the lists.
Program:
# declare lists
numbers = []
squares = []
cubes = []
# start and end numbers
start = 1
end = 10
# run a loop from start to end+1
for count in range (start, end+1) :
numbers.append (count)
squares.append (count**2)
cubes.append (count**3)
# print the lists
print "numbers: ",numbers
print "squares: ",squares
print "cubes : ",cubes
Output:
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
cubes : [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
By characterizing own capacities:
# define function to add numbers in list
def listNumbers(a,b):
#define dynamic list
list = []
for count in range(a,b+1):
list.append(count)
#return list
return list
# define function to add squares in list
def listSquares(a,b):
#define dynamic list
list = []
for count in range(a,b+1):
list.append(count**2)
#return list
return list
# define function to add cubes in list
def listCubes(a,b):
#define dynamic list
list = []
for count in range(a,b+1):
list.append(count**3)
#return list
return list
# Main code
# declare lists
numbers = []
squares = []
cubes = []
# start and end numbers
start = 1
end = 10
# get values in lists
numbers = listNumbers(start, end)
squares = listSquares(start, end)
cubes = listCubes(start, end)
# print the lists
print "numbers: ",numbers
print "squares: ",squares
print "cubes : ",cubes
Output:
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
cubes : [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]