Search code examples
python-3.7

How can I make an array from the output in Python3


How Can I make an array A = [C,F] of all printed values of both C and F?

Here's the code:

Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]
print ('    C    F')
for C in Cdegrees:
    F = (9.0/5)*C + 32
    print ('%5d %5.1f' % (C, F))

Solution

  • Looks like you need

    Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]    
    A = [(C, (9.0/5)*C + 32) for C in Cdegrees]
    print(A)
    # OR 
    # print(dict((C, (9.0/5)*C + 32) for C in Cdegrees))
    # {-20: -4.0, -15: 5.0, -10: 14.0, -5: 23.0, 0: 32.0, 5: 41.0, 10: 50.0, 15: 59.0, 20: 68.0, 25: 77.0, 30: 86.0, 35: 95.0, 40: 104.0}
    

    Output:

    [(-20, -4.0), (-15, 5.0), (-10, 14.0), (-5, 23.0), (0, 32.0), (5, 41.0), (10, 50.0), (15, 59.0), (20, 68.0), (25, 77.0), (30, 86.0), (35, 95.0), (40, 104.0)]