1
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?

2

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.

0

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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