Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
def printMax(a, b):
   if a > b:
   print(a, 'is maximum')
   elif a == b:
   print(a, 'is equal to', b)
   else:
   print(b, 'is maximum')

printMax(3, 4) # directly give literal values
x = 5
y = 7
printMax(x, y) # give variables as arguments

I'm learning python via the byte of python book, and the author uses this example to introduce us to functions. When the author calls the function printMax, he does not use quotation for the numbers 3,4 i.e. he does not use printMax("3","4") which I thought was fine.

However, later on he defines a function like so

def say(message, times = 1)
   print(message * times)

say("hello", 3)

why does he use the quotations for the message (i.e. why does he use say("hello", 3) instead of say(hello, 3)? How does python notice the difference?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

In your example, printMax is supposed to received integers. In python, and in many other language, literal integer are not expressed using quotation.

Consider this:

printMax(3, 4); ## ouput 4 is max
printMax(35, 4); ## output 35 is max
printMax("35", "4"); ## output 4 is max

My last call to printMax uses quotation, and therefore the argument are now strings. If you do string comparaison, "4" > "35" is true.

He uses say("hello", 3) because hello is a string here, therefore it needs quotation. If he did use say(hello, 3) python would have complained about undefined variable.

share|improve this answer

Python features a parser for figuring this out. The quotation marks tell Python that this object is a string, which is a different kind of object than an number. If you don't use quotation marks, Python will think it is a variable.

(Under the hood, all functions are code objects which get assigned to variables.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.