Sunday, 17 November 2019

Ugly alternatives to - Generator Expressions

 
 
 
 
 
 
 
print("URL for PEP - 289 = https://www.python.org/dev/peps/pep-0289/")
URL for PEP - 289 = https://www.python.org/dev/peps/pep-0289/
In [2]:
sum([x*x for x in range(10)]) # Source -  PEP - 289
Out[2]:
285
In [3]:
#So whats ->  x in range(10):
for x in range(10): # as seen below the variable - x , will take all values from 0-9 
    print(x)
0
1
2
3
4
5
6
7
8
9
In [5]:
mylist = [] # Init empty list 
for x in range(10): 
    mylist.append(x) # Append all ints from 0-9 
In [8]:
# Multiply Consecutive Elements of LIST - mylist 
#
print("myList = ",mylist)
n = 2
sqr_of_nums = []
for e in range(len(mylist)):
    sqr_of_nums.append(mylist[e]*mylist[e])
print("List of Product's of Consecutive Nums = ",sqr_of_nums)
print("SUM of Product's of Consecutive Nums = ",sum(sqr_of_nums))
myList =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
List of Product's of Consecutive Nums =  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
SUM of Product's of Consecutive Nums =  285
In [ ]:
 

No comments:

Post a Comment