Reputation: 47
My teacher gave me this question: make a program that reads an integer and prints it. So I found this code:
integer_number = int(input("Enter an integer: "))
print("You entered:", integer_number)
I would like to understand why the function: "int" comes first than "input"
I expected the "input" function to be first than the "int" function since "input" has the function of requesting information from the user so that he can fill it in and the "int" converts a given string to an integer.
Upvotes: 0
Views: 3723
Reputation: 76
integer_number = int(input("something here"))
is just an abbreviated way of saying:
temp_var = input("something here")
integer_number = int(temp_var)
It is not the other way around, because the int()
function takes a string and converts it into an int
. If you did it the other way around, the function wouldn't have anything to convert.
Upvotes: 6
Reputation: 3096
In Python you can pass functions as arguments of another function and of course they'll be evaluated before the outer function. Guess I should point you to the relevant official Python documentation that states that, but just googling for it isn't working right now.
See Python’s Functions Are First-Class:
Python's functions are first-class objects. You can assign them to variables, store them in data structures, pass them as arguments to other functions, and even return them as values from other functions.
Got a nice answer about that on SO :
What are "first-class" objects:
In short, it means there are no restrictions on the object's use. It's the same as any other object.
A first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have.
Your one is not the first question about that:
All arguments are evaluated before the function is called:
Answer:
You are right, all arguments are evaluated before the function is called, otherwise the function cannot know what is passed to it. So what you are doing is actually:
but no link to docs here too.
Took a while, I found it here:
6.3.4. Calls its the link for Python 3:
...............All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal parameter lists.........
Upvotes: 2
Reputation: 9
The reason you put int before input is because input automatically sets whatever you typed in as a string. Then the int function kicks in and sets whatever to a integer. Had you done it the other way, you would have set it to an integer and then a string as well as have an error because input must have something like this:
i = input('print things here')
with quotation marks to indicate what you print and then ask for input. With input(int('input')) it would try and print int('input') without quotations around it and would cause it to have an error.
Upvotes: 1
Reputation: 65
To begin, the code begins by utilizing the input()
function to request an integer from the user. The prompt "Enter an integer: "
is shown, clearly indicating the desired type of input. Next, the input is converted into an integer using the int()
function. This important step guarantees that the value entered by the user is treated as a whole number, making it suitable for mathematical calculations and avoiding any possible errors. The variable integer_number
is then assigned the converted integer, containing the final result of the code.
Next, we invoke the useful print()
function to deliver a customized message alongside the integer that was entered. The message "You entered:"
is smoothly followed by the integer value stored in the variable integer_number
, giving the user reassurance of their input.
Upvotes: 1
Reputation: 18866
When you use input("something")
, it asks the user for input, and return
s a string, here "something"
is an argument to input()
Calling int()
on that output attempts to convert it to an int
, the input here (whatever the user input) is the argument to int()
If you had input(int("something"))
, what happens is "something"
is passed to int()
, raising an Exception (because that literal string can't be a valid int
)
Upvotes: 1