2

I was learning about dictionaries in Python and I created a simple program:

# Create an empty dictionary called d1
d1 = {}

# Print dictionary and length
def dixnary():
    print "Dictionary contents : "
    print d1
    print "Length = ", len(d1)

# Add items to dictionary
d1["to"] = "two"
d1["for"] = "four"

print "Dictionary contents :"
print d1
print "Length =" , len(d1)

# Print dictionary and length
print dixnary()

Now there's a difference in the results when I use the print commands and when I use the dixnary function.

Using the print commands I get the result:

Dictionary contents:
<'to':'two','for:'four'>
Length = 2

and when I use the function dixnary, I get the results:

Dictionary contents:
<'to':'two','for:'four'>
Length = 2
None

Notice the None on the final line. This None gets added when I use the function dixnary. Why is this?

1
  • 4
    What does dixnary() return? That's the last thing you're printing. Commented Apr 24, 2012 at 15:44

1 Answer 1

11

You're attempting to print the return value of a function, but the function doesn't return a value, so it returns the default value of None.

The reason why it prints out other data is that you have print commands inside of the function. Just run the function (dixnary()), instead of printing it (print dixnary()).

Or alternatively, have the program return the string, so you can do useful things with it.

def dixnary():
    return "Dictionary contents :\n%s\nLength = %d" % (d1, len(d1))

print dixnary()
Sign up to request clarification or add additional context in comments.

6 Comments

Umm, you are actually wrong. I was only using the functions earlier and was getting the result with NOne in it. Therefore I added the print commands to see whether they give the same results and I found that results were different. Have you tried this program? Maybe use a # in front of the print commands and you will see that the function still gives the results.
No, I'm really not... You're printing out the return value of the function, which isn't returning a value, so it defaults to None. I promise you. If something's unclear I can try to explain better.
Please do not ask a question and then tell people they're wrong when they answer... especially when they are right :) You haven't understood: the function does all the printing normally, and then returns a value None. You wrote code that calls the function, and then prints the value returned from the function. That is what the line print dixnary() does. If you didn't want to print the None, then you should just have dixnary(). Try that yourself.
Aha! Yes, it worked. I hadn't understood earlier. Sorry for saying you are wrong. But I dont understand why this is happening. What is meant by value returned from the function? and how is it different when I just use dixnary() ?
Sorry for the lull, function take argument, do actions then return a variable (optionally). In python, if a function does not return a variable explicitly, it implicitly returns the None object. When you print a function, what you're actually printing is the return value of the function.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.