5

I am new to Python, and am trying to create a function which creates lists with different material parameters from user inputs as shown in the code below.

def material():
    layers = int(raw_input("State the number of material layers in the wall (0 for default material): "))

    rho = [] # Density [kg/m3]
    c = [] # Heat capacity [J/(kg K)] 
    k = [] # Heat conductivity [W/(m K)]
    #a = [] # Thermal diffusivity [m2/s]
    d = [] # Thickness of material [m]

    # Saveing material properties
    if layers == 0:
        rho.append(2300)
        c.append(900)
        k.append(1.6)
        d.append(3.2)
        layers = 1

    else:
        for i in range(layers):
            print "\n" "Define thermal properties for material", i+1,"(starting from left)"
            rho.append(float(raw_input("Density [kg/m3]: ")))
            c.append(float(raw_input("Heat capacity [J/(kg K)]: ")))
            k.append(float(raw_input("Heat conductivity [W/(m K)]: ")))
            d.append(float(raw_input("Thickness [m]: ")))

    return ???

How should I return rho, c, k, d and layers so I am able to e.g. print – or use the value of – e.g. the second item in the list of d?

print d[1]

4 Answers 4

15

How should I return rho, c, k, d and layers [...] ?

Simply do it:

return rho, c, k, d, layers

And then you'd call it like

rho, c, k, d, layers = material()
print d[1]

Note that the more stuff you're returning, the more likely it is you're going to want to wrap it all together into some structure like a dict (or namedtuple, or class, etc.)

Sign up to request clarification or add additional context in comments.

3 Comments

Obs.: This feature is actually returning a tuple and then doing a multiple assignment statement. You can also use S = material(); print S[3][1] to achieve the same result for example.
@OdraEncoded: very true. I tend not to use tuple indexing for heterogeneous return results because it's too easy for me to mix up which variable correponds to which index.
God, you are fast out there. Thank you so much. I can see that my problem was more about how to call the function – but I got it now. Thanks again, everyone. Cheers.
6

return can return multiple values if you separate them with commas:

return rho, c, k, d, layers

This will make material return a tuple containing rho, c, k, d, and layers.

Once this is done, you can access the values returned by material through unpacking:

rho, c, k, d, layers = material()

Here is a demonstration:

>>> def func():
...     return [1, 2], [3, 4], [5, 6]
...
>>> a, b, c = func()
>>> a
[1, 2]
>>> b
[3, 4]
>>> c
[5, 6]
>>> a[1]
2
>>>

Comments

0
def return_values():
    # your code
    return list1, list2

and get the value like below

value1, value2 = return_values()

Comments

0
def function_returning_multiple_list():
  l1=[]
  l2=[]
  l3=[]
  return (l1,l2,l3)


def another_fucntion():
  list1,list2,list3=function_returning_multiple_list()
  print list2

1 Comment

Note that there is no need for the parenthesis in the return statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.