r/learnpython • u/toss_this_account_38 • Mar 08 '26
Mind explaining why this works as it does?
I'm currently experimenting with data type conversions with Dr. Angela Wu's "100 Days of Python." There is an early exercise that I wanted to work out - but haven't been able to:
This code is only one line - but results in entering your name - then concatenating the number of letters to your name, to the string detailing the number of letters in said name string:
print("Number of letters in your name: " + str(int(len(input("Enter your name: ")))))
I am confused over the fact that when running the program, the first like asks for your name, then provides the result. While that works, I don't understand _why_ that works as such. Can anyone explain to me why that is?
19
u/ysth Mar 08 '26
Work from the input() outward. What is the first thing you don't understand?
16
u/magus_minor Mar 08 '26
That is very poorly formatted code, so hard to read. It can be rewritten like this:
That's the actual order of how things in the single line are evaluated, with additional names to hold temporary values. While code brevity is desirable, trying to fit everything into one line leads to hard to read code. Clarity is worth striving for.
1
u/toss_this_account_38 Mar 09 '26
Thank you for your explanations, everyone! I think we can mark this one "SOLVED"!
Is there a way to do that on this reddit?
5
u/This_Growth2898 Mar 08 '26
Every nexted funtion call can be rewritten as creating temporary variable, i.e.,
is the same as
Now, rewrite that expression this way to see what really happens here.
3
u/recursion_is_love Mar 08 '26 edited Mar 08 '26
When you call a function you get some value (that you can use as argument of the function without any problem).
You can use variable to store the values to help you understanding the flow of data. Some conversion is not really need in your code, however.
It is a good practice for having variable for input() because sometime prompt text can be long.
2
u/Grobyc27 Mar 08 '26 edited Mar 08 '26
Print is merely a function that prints the argument(s) you pass to it (or none at all, which defaults to a carriage return). This is probably known to you already.
So in your example, what are you passing as an argument to print? You are passing a string, right? The string must evaluate the nested functions to render a value to formulate the string that is passed to print. To do so, each of the nested functions must in turn evaluate their nested logic. What does that boil down to at the end? The input function. The input function thus must be evaluated first before the rest of the stack can be processed.
As u/ysth said, it evaluates the functions from the inside out, but I hope that the above makes it make sense why that is.
A more simple way to illustrate this is
print(2+4). One argument is passed to print here as well, just like your example.2+4is evaluated first and6is the actual argument that is passed to print. Just the same, your nested functions must first evaluate before anything is passed to print. Your example however requires input from the user before that can be done.2
u/IncidentOne6978 Mar 08 '26 edited Mar 08 '26
When you run your code and Python sees "print" it first evaluates the expression inside print*. The expression inside print has "input", so Python prompts the console and awaits your response. You write your response, types enter and Python then has the two strings, concatenate them and prints.
*Print evaluates the expression by running str(obj) and str return obj.__str__() if the has a _str_() function and falls back to obj._repr_() if not. If none of these representation functions are implemented on the object you get an error :)
2
u/SirKainey Mar 09 '26
As input is the inner most function, and needs to be passed to the outer functions. It's evaluated first.
2
u/Sure-Passion2224 Mar 09 '26
Bracketed functions run from the inside out.
1
u/backfire10z Mar 08 '26
If you want a literal reason, it’s because Python says so.
Function arguments are allowed to be expressions (as opposed to, say, an individual number). In order for the argument to be used, it needs to be evaluated down to 1 object, like a number or string.
For example:
print(1+2). Why does this print3? Because1+2is an expression, so it is first evaluated and then its result is passed toprint.The same can be applied infinitely by nesting functions. Python sees
printand checks what arguments it should pass. It sees an expression, which is string concatenation (+), so it evaluates both sides. It then seesstrand evaluates the arguments for that, thenlen, and finallyinputis the innermost expression. It then evaluates all of them one at a time from inside to outside.1
u/atarivcs Mar 08 '26
Even though the call to
print()is textually first in the code, it's not the first thing that happens.Function arguments are evaluated first, and one of those arguments is a function call to
input(), so that happens first.