this post was submitted on 12 Jun 2024
42 points (93.8% liked)
Python
6356 readers
1 users here now
Welcome to the Python community on the programming.dev Lemmy instance!
📅 Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django 💬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
🐍 Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
💓 Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
✨ Python Ecosystem:
🌌 Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- Pythörhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I think you’ve already gotten some good answers here regarding the function itself:
It sits and waits for the user to input something and hit Enter, and returns the value the user entered, which is then assigned to your
nam
variable. (See the documentation for the function.I might also offer the advice of confirming your understanding of the flow of a program. That is, understand that, in the general sense, the computer must resolve the right-hand side of the equals sign to a value before it can assign it to the left.
For example, if the right-hand side is a literal value, it’s already resolved. For example, a line like
name = “Joe”
is easy—assign the string literal “Joe” to the variablename
, when the line is run.If the right hand side is a mathematical equation, it must be resolved to a value when the line is run. For example, for a line like
value = 2+2
, the2+2
must be resolved to4
before it can be assigned to the variable.Then, for something like
name = input(“Who are you?”)
, in order to resolve the right-hand side, the computer must first run the function before it can assign a value to the variablename
.It can, of course, get more complicated, where you can call multiple functions on a line, and the results of one feed into the next, and so on. But, that can be an exercise for the near future.
That's a good point! That is something I've noticed. Traditional mathematics uses PEMDAS, which is bidirectional, but Python uses...PEMDRAS, I guess, which is...bi-monodirectional(?), that is, left-to-right in all cases, except in the exception of variables which is right-to-left.
I swear this makes sense to me. My brain just thinks weirdly. Haha.
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.
Okay, that's just fucking cool. :3