Question:
what is the function of () in python ?
?
2013-02-13 06:38:53 UTC
what is the funtion of () in python ?
ex -
question = " are you going ? "
print "question"
answer = raw_input() <---------- ??

print ("Ok ", + answer + "as your wish!")

so why you should put () after raw input ?? or any other command ??
Three answers:
husoski
2013-02-13 07:20:15 UTC
raw_input is a function, not a "command". It happens to be pre-defined, but isn't part of the syntax of the language. That makes it different from keywords like while, if, for, def, etc.



The () afterwards says to call that function, but pass no argument values. The name itself simply refers to the function as an object. You can use that to pass the raw_input function to another function, or create an alias, like:



my_input = raw_input # Creates an alias for raw_input, but does NOT call it



However, print is different--in Python 2 at least. Prior to Python 3, print was a statement. No parentheses were needed to run:



>>> print "the answer is", 42

the answer is 42



If you did happen to put parentheses in:



>>> print("the answer is", 42)

('the answer is', 42)



... that would print the tuple ("the answer is", 42).



So, print is a statement (some might say "command", but that's not a term that's used in Python documentation) and not a function.



(This changed in Python 3, along with a number of other things. In P3, print is a predefined function, and the parentheses are required. But Python 2.7 is still actively used and taught.)
Siju
2013-02-14 09:35:34 UTC
Refer the below link



http://www.tutorialspoint.com/python/python_functions.htm



http://zetcode.com/lang/python/functions/
?
2013-02-13 14:41:33 UTC
You put () after it if it's a function that takes in no parameters.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...