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

Python

6247 readers
58 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] 29 points 3 months ago* (last edited 3 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!

[โ€“] rainerloeten 2 points 3 months ago

The only actual good answer.

load more comments (5 replies)