-2

What is the purpose of the parentheses after the input statement?

>>> name = input()
>>> print(name)
  • 6
    `input()` is a function. The parentheses are part of Python syntax for calling functions – will-hedges Jun 24 '22 at 16:05
  • 1
    Sounds like a question designed to probe your understanding of Python's C interpreter and compiler. The parenthesis indicate the prior word is to be treated as a callable function that takes 0 parameters, or under the hood just one, the identity of the invoking structure. Add this to the pile of fun questions like: "What happens when you type google dot com into a browser's URL bar and press enter? Our fascinating journey begins with a keydown event on your interface device..... – Eric Leschinski Jun 24 '22 at 16:05
  • 1
    Can you please clarify what exactly you are asking? *Every* function needs parentheses to run. – MisterMiyagi Jun 24 '22 at 16:06
  • Because `name=input` means something else. That creates a new binding `name` for the built-in function `input`. You probably wouldn't want to do that, but that isn't the point. – BoarGules Jun 24 '22 at 16:14
  • @MisterMiyagi Thank you for your interest in helping my understanding. As far as I have seen the "Input" requires the use of the parentheses. As I asked I want to understand the role of those parentheses, not only apply them because it is the syntax of python. I want to know if there are a specific meaning by using them. – Solutionist Thinker Jun 24 '22 at 18:50
  • @SolutionistThinker See, your question shows *two* functions with parentheses yet it asks only about *one* of those. So I'm wondering what it is you need explained – why the purpose seems clear for `print` but not for `input` even though it is the same. – MisterMiyagi Jun 24 '22 at 19:02
  • @MisterMiyagi I understand that when using "print" there is always a variable between parentheses. Unlike "input" that can have nothing between parenthesis. In the case of Input could we just use Input without parenthesis? >>>how_many_touchdowns = input() >>>print(how_many_touchdowns) – Solutionist Thinker Jun 24 '22 at 19:13
  • Does this answer your question? [What are the parentheses for at the end of Python method names? \[duplicate\]](https://stackoverflow.com/questions/32129064/what-are-the-parentheses-for-at-the-end-of-python-method-names) – MisterMiyagi Jun 24 '22 at 20:04
  • Does this answer your question? [What is the difference between calling function with parentheses and without in python? \[duplicate\]](https://stackoverflow.com/questions/46001292/what-is-the-difference-between-calling-function-with-parentheses-and-without-in) – MisterMiyagi Jun 24 '22 at 20:05

4 Answers4

1

input is a function. To call a function you need brackets.

See this example:

# Defining a function to print 'Hi'
def print_hi():
    print('Hi')

print_hi # Does nothing because you haven't called the function
print_hi() # Prints 'Hi' because the function has been called

This is the same with input, and all other functions

The Thonnu
  • 3,578
  • 2
  • 8
  • 30
0

@Ahad Mosharraf @MisterMiyagi and others. Thank you all for your answers. It will really help me. I have also done some in-depth research and written an explanation. I am waiting for your opinions.

The input() function allows your programs to accept user input or your own input.

Here comes an example:

age = input("What is your age? ")

Let's have a look at this function step by step.

  1. "input" is the part of the function that asks for your "age" (What is your age?)

  2. The parentheses (...) mean that this function takes an argument.

  3. "What is your age?" --> "What is your age is your argument."

  4. That prompt will be displayed to the user or to ourselves

warning: don't forget to enter your prompt. If you leave this blank you may get unexpected results.

  1. age = now the returned value is held by the "age" variable (the chosen "age")

I hope this explanation is good and clear enough!

-1

in Python the parentheses after input function is used to show a message to users. For an example if you want to show "Input Your Name: " you need to write it in the parentheses like name=input("Input Your Name: ") :-)

-1

input is a function. Functions are used to run a certain block of code. I suppose you know how variables work, if you just typed input Python would think you're trying to access the variable called function. But if you add brackets, Python knows that you are trying to call input.

So actually what happens if you type input() is:

-Python gets the function called input.

-Calls that function/executes the code inside that function/accepts user input from the terminal.

MomoVR
  • 113
  • 1
  • 8