I'm currently learning Python and am learning about very basic functions such as int()
, float()
, and input()
.
I have the first two down pat, but I'm struggling to understand the last. The example I'm looking at is found at 12:26 of this video:
nam = input('Who are you? ')
print('Welcome', nam)
Who are you? Chuck
Welcome Chuck
In this case, wouldn't nam
be a variable equal to the text on the right side of the =
sign?
In which case, if nam
is equal to input('Who are you? ')
, then wouldn't print('Welcome', nam)
just result in
Welcome input(Who are you? )
?
Obviously not (nor does it work in a compiler), which leads me to believe I'm clearly misunderstanding something. But I've rewatched that section of the video several times, and looked it up elsewhere on the web, and I just can't wrap my head around it.
Could someone help me with this?
Thanks.
Oh! I get it now!
It's because while int() and float() are instantaneous, while input() uses the fourth dimension, time, and thus depends on a future input by the user. (May not make sense the way I'm putting it, but it makes sense to me. Lol.) In other words, while float() and int() have all the data they need to produce an output (i.e. what's in the parentheses), input() "outputs" the text in the parentheses, then puts itself into a pending state to await further user input, then outputs that second user input.
Am that about the right of it? :)
Yes, you got the gist of how it works.
To give a bit more context, functions are basically snippets of code that are executed when called. One way to look at the
input("What's your name?")
function is this (not how the actual function looks like, just an abstraction):That
return
is something you will see often in many functions, and when you call a function, that's the result it sends to the line that called. So, ifinput
was actually coded like this:Every time you called it, you would receive 1 as a result. In your example,
nam = input('Who are you? ')
would always assign 1 to nam, because the return is 1 rather than the variable that receives whatever you typed in.That's so cool! Thank you!
U got this ๐
Yay!