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.

  • SaintWacko@midwest.social
    link
    fedilink
    English
    arrow-up
    29
    ·
    edit-2
    6 months ago

    So what it comes down to is that int(), float(), and input() (as well as print()) are functions that you are calling. In the case of int() and float(), they return (simply put, when you make a function call it “becomes” the return value) an int or float type object based on the argument (the value between the parentheses) that you passed in. In the case of print(), it causes the program to print out the provided argument.

    input() is a little more complicated. It prints out the provided argument (in your case: Who are you? ) and then puts the program on pause while it waits for the user to input some text and press enter. Once they have done so, the input function returns the text the user has entered. So as mentioned before, the code input('Who are you? ') “becomes” the text the user input, which then gets assigned to the variable nam.

    I think where you may be getting confused is what exactly defines “text”. The only things that python considers text (referred to as a string) are characters surrounded by “” or ‘’. In your example, input('Who are you? ') is not a string, but code to be executed (although the argument being passed to input, 'Who are you? ', is a string). As an experiment, try surrounding that code with quotation marks (name = "input('Who are you? ')") and see what happens!

    • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      3
      ·
      6 months ago

      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? :)

      • I Cast Fist@programming.dev
        link
        fedilink
        arrow-up
        4
        ·
        6 months ago

        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):

        function input(text_to_show):
            print(text_to_show)
            input_from_user = get_keyboard_input()
            return input_from_user
        

        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, if input was actually coded like this:

        function input(text_to_show):
            print(text_to_show)
            input_from_user = get_keyboard_input()
            return 1
        

        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.