Better understanding: Python's None value (for-loop inside function)

What's the deal with printing and returning values from a function?

A for-loop work as expected outside a function,
but, in the exercise, it keeps placing a None value.

I sense it has to do with way procedure/functions operate,
like, a mandatory return statement... not simply by doing just the statements you type...

Motivator:

# Define a procedure, sum_list,
# that takes as its input a
# list of numbers, and returns
# the sum of all the elements in
# the input list.

Test 1: printing both numbers and sum

def sum_list(list):
    sum = 0
    for number in list:
        print number
        sum = sum + number
    print sum
    #return sum

print sum_list([1, 7, 4])
#>>> 12

print sum_list([9, 4, 10])
#>>> 23

print sum_list([44, 14, 76])
#>>> 134

Test 2: printing numbers, returning sum

def sum_list(list):
    sum = 0
    for number in list:
        print number
        sum = sum + number
    print sum
    #return sum

print sum_list([1, 7, 4])
#>>> 12

print sum_list([9, 4, 10])
#>>> 23

print sum_list([44, 14, 76])
#>>> 134

The natural answer tested:

def sum_list(list):
    sum = 0
    for number in list:
        #print number
        sum = sum + number
    #print sum
    return sum

print sum_list([1, 7, 4])
#>>> 12

print sum_list([9, 4, 10])
#>>> 23

print sum_list([44, 14, 76])
#>>> 134

asked 03 Aug '12, 10:54

Leandro%20De%20Leite's gravatar image

Leandro De L...
474315

accept rate: 57%

edited 03 Aug '12, 11:07

Sam%20the%20Great's gravatar image

Sam the Great ♦
15.9k132548


One Answer:

By default, functions in Python evaluate to None if you don't provide a return statement in their definition.

In the statement print sum_list([1, 7, 4]), you are requesting to print the evaluation of sum_list([1, 7, 4]), which will be None in the cases where you try to print sum inside the function instead of return sum.

If you wish to use print sum inside of the function, you can do that. Instead of the statement print sum_list([1, 7, 4]), simply use sum_list([1, 7, 4]) -- don't print the evaluation of the function call. That will just run the function with the given arguments (which will run the print sum line), and not care about the evaluation of it (which by default, would be None if you don't have a return statement).

link

answered 03 Aug '12, 11:01

Sam%20the%20Great's gravatar image

Sam the Great ♦
15.9k132548

1

Thanks again.

What is the better way to have print sum_list(...) without None?

In your experience, is there best case (or common case) to evaluate a function instead print a function?

For now, I can see evaluation for calculations/db, and printing for markup...

(03 Aug '12, 11:12)

Leandro De L...

Leandro%20De%20Leite's gravatar image
1

It's just a matter of whether print or return will suit your needs better in a given situation. In my experience (which is not much), I think I've only used print inside a function for debugging purposes. There may very well be other very useful applications of it, like the ones you just mentioned. I don't know much about them; you should continue testing things out for yourself, and see what you find. =]

(03 Aug '12, 11:42)

Sam the Great ♦

Sam%20the%20Great's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags

×30,527
×172
×112
×30
×30
×19

Asked: 03 Aug '12, 10:54

Seen: 2,515 times

Last updated: 03 Aug '12, 11:42