PAI 789 Advanced Policy Analysis > Basic For Loops (g04)

for.py

"""
for.py
Spring 2022 PJW
"""

#  Set up initial list

list1 = [1,3,5,7,9]

print('\nList1:')
print(list1)

#  Square it via a loop

list2 = []
for n in list1:
    list2.append( n**2 )

print('\nList2')
print(list2)

#  Square it via a list comprehension

list3 = [n**2 for n in list1]

print('\nList3:')
print(list3)

#  Build and print a table of powers

print('\nTable of Powers:')

bases = range(1,11)
powers = range(0,5)

for b in bases:
    values = [b**p for p in powers]
    print('base',b,'powers:',values)

Site Index | Zoom | Admin
URL: https://wilcoxen.maxwell.insightworks.com/pages/6081.html
Peter J Wilcoxen, The Maxwell School, Syracuse University
Revised 02/13/2022