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.

  • Mak'@pawb.social
    link
    fedilink
    arrow-up
    9
    ·
    5 months ago

    I think you’ve already gotten some good answers here regarding the function itself:

    It sits and waits for the user to input something and hit Enter, and returns the value the user entered, which is then assigned to your nam variable. (See the documentation for the function.

    I might also offer the advice of confirming your understanding of the flow of a program. That is, understand that, in the general sense, the computer must resolve the right-hand side of the equals sign to a value before it can assign it to the left.

    For example, if the right-hand side is a literal value, it’s already resolved. For example, a line like name = “Joe” is easy—assign the string literal “Joe” to the variable name, when the line is run.

    If the right hand side is a mathematical equation, it must be resolved to a value when the line is run. For example, for a line like value = 2+2, the 2+2 must be resolved to 4 before it can be assigned to the variable.

    Then, for something like name = input(“Who are you?”), in order to resolve the right-hand side, the computer must first run the function before it can assign a value to the variable name.

    It can, of course, get more complicated, where you can call multiple functions on a line, and the results of one feed into the next, and so on. But, that can be an exercise for the near future.

    • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      5 months ago

      That’s a good point! That is something I’ve noticed. Traditional mathematics uses PEMDAS, which is bidirectional, but Python uses…PEMDRAS, I guess, which is...bi-monodirectional(?), that is, left-to-right in all cases, except in the exception of variables which is right-to-left.

      I swear this makes sense to me. My brain just thinks weirdly. Haha.

      • Voroxpete@sh.itjust.works
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        5 months ago

        If it helps, think of the contents of the variable as being the result of the formula. If the formula is 2 + 2, the result is 4. The variable is the value we are trying to determine. If I want to know my speed, I calculate distance / time. So in Python I would say speed = distance / time, because speed is the unknown that I want to know.

        But then with Python we can have more complex “formulas”. For example, we can say laugh = “Ho” * 3

        Yeah, you can multiply a string. The result is that print(laugh) prints “HoHoHo”.

        In the example you gave, the function is the formula, so input() evaluates to a result that is stored in the variable.

        If functions are nested, each nested function resolves before the one that contains it. So, for example, you can do print(input("Who are you? ")). The nested function (input) resolves first, obtaining the user input, which is then printed. The difference is that doing it this way just prints directly without storing the input as a variable.

        You can also do print("Hello ", input("Who are you? ")). Again, the nested function resolves first, so the user is presented a prompt to give their name. Then print combines the two comma separated statements ("Hello ", and the result of the input function) to display “Hello <name>”. Try it for yourself.

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

          But then with Python we can have more complex “formulas”. For example, we can say laugh = “Ho” * 3

          Yeah, you can multiply a string. The result is that print(laugh) prints “HoHoHo”.

          Okay, that’s just fucking cool. :3