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

Python

6378 readers
98 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
 

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
[–] SaintWacko@midwest.social 31 points 5 months ago* (last edited 5 months ago) (6 children)

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.zone 3 points 5 months ago (2 children)

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

[–] ICastFist@programming.dev 4 points 5 months ago (1 children)

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.

That's so cool! Thank you!

load more comments (3 replies)