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

Python

6356 readers
2 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
[โ€“] thisisnotgoingwell@programming.dev 3 points 5 months ago (1 children)

I think you need to look into string concatenation, the easiest and best of which is f strings. You could do something like;

print(f'welcome, {nam}')

You could also "add" the strings together.

print('welcome, ' + nam)

Another thing, when assigning the output of something to a variable, you can think of it as "the result of the code right of the equals sign is the value of the variable".

The input function assumes that the value should be interpreted as a string, but what if want it to be a number? You can just wrap another function around your input

user_number = int(input('what's the number?'))

[โ€“] EveryMuffinIsNowEncrypted@lemmy.blahaj.zone 1 points 5 months ago* (last edited 5 months ago) (1 children)

user_number = int(input(โ€˜whatโ€™s the number?โ€™))

But then what's input('what's the number?') result in? I understand int() and float(), but I can't wrap my head around what exactly input does to the text within the succeeding parentheses.

ย 


Edit: I get it now! Another user helped me understand it, and I get it now! Thank you for your help too! You're awesome!

[โ€“] thisisnotgoingwell@programming.dev 2 points 5 months ago (1 children)

Yup. You'll see functions wrapped inside other functions all the time. The function on the inside will run first, then the next, etc.

In the example I gave, the value of nam is a string. But it you need to perform some mathematical function to it, it needs to be interpreted as a number. So once the value is received, int() will convert it into a number. Finally, that final value will be assigned to nam. Wrapping functions inside of functions is a great way to write concise code.

That's pretty cool. It's almost like a...time-based Excel function! Lol.