Newbie feeling discouraged: returning values

I’m just diving into Python and already feeling a little lost & confused! My question is why would I do code A vs code B below. Code A is what was shown in the course, but Code B seems easier and gives the same result? So essentially, why put the hours, minutes, seconds = convert_seconds(x) and then printing that, instead of just going straight to convert_seconds(x) which returns the same thing (with parentheses)?

(The part of the course is about returning values, if that matters) (And I hope I did this right so the code looks right!)

Definitions:

def convert_seconds(seconds):
    hours = seconds//3600
    minutes = (seconds - hours * 3600)//60
    remaining_seconds = seconds - hours * 3600 - minutes * 60
    return hours, minutes, remaining_seconds

Code A

hours, minutes, seconds = convert_seconds(5000)
print (hours, minutes, seconds)
1 23 20

Code B

convert_seconds(5000)
(1, 23, 20)

In practice, you would do A when you want to use hours, minutes, seconds for further calllculation. For learning, you do A to should you know about returning and binding multiple values. The print is just a side issue to verify the function.

A tuple is a collection of values:

>>> tuple_of_ints = (1, 2, 3)  # alternatively, tuple_of_ints = convert_seconds(3723)
>>> print(tuple_of_ints)
(1, 2, 3)

You can access the values in a tuple by index:

>>> print(tuple_of_ints[0])
1
>>> print(tuple_of_ints[1])
2
>>> print(tuple_of_ints[2])
3

Alternatively, you can “unpack” a tuple into separate variables

>>> hours, minutes, seconds = tuple_of_ints
>>> print(hours)
1
>>> print(minutes)
2
>>> print(seconds)
3

This can sometimes result in more readable code, as Terry suggests:

>>> print("hours", hours, "minutes", minutes)
hours 1 minutes 2

as compared to:

>>> print("hours", tuple_of_ints[0], "minutes", tuple_of_ints[1])
hours 1 minutes 2

One other small difference is that unpacking will raise an error if you don’t have the right number of variables:

>>> hours, minutes = tuple_of_ints
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

It’s good to understand differences between pieces of code, for instance, why there are parentheses in the output of “Code B” but not “Code A”. But in general, if your code does what you want it to do, that’s great and you shouldn’t feel discouraged by that! :slight_smile:

I’m just diving into Python and already feeling a little lost &
confused! My question is why would I do code A vs code B below. Code A
is what was shown in the course, but Code B seems easier and gives the
same result? So essentially, why put the hours, minutes, seconds =
convert_seconds(x) and then printing that, instead of just going
straight to convert_seconds(x) which returns the same thing (with
parentheses)?

I think you’re confusing the return value from the function
(convert_seconds) with the printing of the value.

I expect you’re running the code directly in some interactive
environment. For example, here’s me doing the same in the Python
interactive mode:

 Python 3.10.6 (main, Aug 11 2022, 13:47:18) [Clang 12.0.0 
 (clang-1200.0.32.29)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> def convert_seconds(seconds):
 ...     hours = seconds//3600
 ...     minutes = (seconds - hours * 3600)//60
 ...     remaining_seconds = seconds - hours * 3600 - minutes * 60
 ...     return hours, minutes, remaining_seconds
 ...
 >>> hours, minutes, seconds = convert_seconds(5000)
 >>> print (hours, minutes, seconds)
 1 23 20
 >>> convert_seconds(5000)
 (1, 23, 20)
 >>>

I don’t see any >>> prompts in your examples, but in the above, Python
is prompting me for input at the >>> primary prompt and the ...
continuation prompt (for more lines of an incomplete thing, like the
function).

At the interactive prompt, if a line of code is an expression and that
expression does not evaluate to None, Python prints the value for you.

In a script, that doesn’t happen; it is purely a convenience thing to
make the interactive prompt useful.

So…

In a script you’d use this:

 hours, minutes, seconds = convert_seconds(5000)

so that you could use the values in hours etc conveniently for further
work. Because that is an assignment, the interactive mode doesn’t
display anything, and you need a subsequent print() call to see the
values. Or you could just enter the variable names as expressions to
see their values:

 >>> hours
 1
 >>> minutes
 23
 >>> hours, minutes, seconds
 (1, 23, 20)

So the short answer is: in a programme you’ve usually store the return
from the function in some variables so that you can use those values
later.

Interctively, do whatever is expedient.

(The part of the course is about returning values, if that matters) (And I hope I did this right so the code looks right!)

The code fences look fine.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Thank you all! I think I understand it much better now… though I think I’ll only fully understand it once I get a little further along and have more practice.