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.
You’re setting ‘nam’ to whatever the output of the function called ‘input’ is. The string asking who are you is an argument to the ‘input’ function. What that function does happens to be that it prints its argument out to the console, waits for the user to enter text, and returns whatever text was entered as its output. I would recommend actually trying out the code and playing around with it if you want to understand it better.
The other two functions you mentioned work similarly. The output of the function named ‘int’ is a new integer. Usually you will give it a number as an argument to set the value of that integer.
"The other two functions work similarly" noo I wouldn't say that! :D On a very abstract way, maybe. But especially to a beginner, they don't. One just processes it's input a bit (casting) while the other displays text, reads from stdin, etc.
I believe OPs confusion stems exactly from presuming strong similarities between all functions, while only
float()
andint()
are similar andinput()
being a completely different thing (relatively speaking..)