Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python programs

Python | Create three lists of numbers, their squares and cubes



Here, we are going to learn how to create three lists with the numbers, their squares and cubes in Python, where range of the numbers is given.
Submitted by IncludeHelp, on August 03, 2018

Take a range i.e. start and end, and we have to create three lists, list1 should contains numbers, list2 should contain squares of the numbers and list3 should contain cubes 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]

Logic:

  • Declare three lists.
  • Define range, here we are defining start with 1 and end with 10.
  • Run a loop with the range as range(start, end+1) and loop counter as count.
  • Append the loop counter count to the list named numbers, append square to the list named squares and append the cube to the list named cubes.
  • Finally, 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 defining own functions

# 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]





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.