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.

  • 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.

    • EveryMuffinIsNowEncryptedOP
      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