# Python
Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary
values=[]
#Note: range(start,end,increment)
# start is included and end is not included
#increment by default is 1
#E.g: range(1,11,1) means a sequence 1,2,3,4,5,6,7,8,9,10
#List comprehension
#l=[i for i in range(1,11)] in this i is added to list for each i in range(1,11)
# For 1 2 3 4 5 6 7 8 9 10
# This is 1-10 in sequence, So using list comprehension we get it
values=[i for i in range(1,11)]
print("a:",values)
# For 0 2 4 6 8 10 12 14 16 18 20
# This even number sequence, So using list comprehension we get it
values=[i for i in range(0,22,2)]
print("b:",values)
# For 1 4 9 16 25 36 49 64 81 100
# This Square of number sequence, So using list comprehension we get it
values=[i*i for i in range(1,11)]
print("c:",values)
# For 0 0 0 0 0 0 0 0 0 0
# This is [0] 10 times. So if we multiple a list by nummber it gets repeated
# In this case we multiple by 10.
values=[0]*10
print("d:",values)
# For 1 4 9 16 9 7 4 9 11
# This is a random sequence. So we have to write each element
# separated by ","
values=[1, 4, 9, 16, 9, 7, 4, 9, 11]
print("e:",values)
# For 0 1 0 1 0 1 0 1 0 1
# This is [0, 1] 5 times. So we multiple by 5.
values=[0, 1]*5
print("f:",values)
# For 0 1 2 3 4 0 1 2 3 4
# This is [0, 1, 2, 3, 4] 2 times. So we multiple by 2.
values=[0, 1, 2, 3, 4]*2
print("g:",values)
Output Screenshot:
Code Screenshots: (For better understanding of indentation)
Get Answers For Free
Most questions answered within 1 hours.