this post was submitted on 12 Jun 2024
42 points (93.8% liked)

Python

6157 readers
32 users here now

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

๐Ÿ“… Events

October 2023

November 2023

PastJuly 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
 

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
[โ€“] [email protected] 0 points 2 months ago* (last edited 2 months ago)

No, nam is a placeholder for whatever is inputed into the function input by the user when the program is ran. Input prints to screen whatever you put () when you first call it. It expects something to then be inputted by the user when the program runs by prompting the user with the message in the (). Whatever the user inputs is then referred to by the variable, in this case "Chuck" was inputted.

It will make a bit more sense when you start writing functions, you can return whatever results you want from calling a function. Those returns will be referred to by the variable you label it, word on the left of the =.

In short, whatever is returned by a function is what is "saved" in the variable.