43

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 are viewing a single comment's thread
view the rest of the comments
[-] Voroxpete@sh.itjust.works 2 points 4 months ago* (last edited 4 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 ". Try it for yourself.

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

this post was submitted on 12 Jun 2024
43 points (93.9% liked)

Python

6331 readers
165 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS