# — Argument —
An _argument_ is the actual information you send to a function when you call it
You called the function twice in the example above
The first time you called `greet_person()` you used the argument `"Ishaan"` and the second time `"Elizabeth"`
When you call the function, the information (the argument) is stored in a variable named `person` inside the function
Don't worry too much if you confuse parameters and arguments. Many programmers confuse them, too!
/5
# — Parameter —
A _parameter_ is the name you choose for information that's needed by the function
You add parameters inside the brackets in the function signature which is the line which includes `def`
In this example, the parameter is `person`
This is the name of the "storage box" which is ready to hold any information you send into the function
However, when you define the function, this "box" is still empty…
/4
# — Call —
You _call_ a function when you use it
You're calling a function when you write its name followed by parenthesis (the round brackets)…
The code in the function definition will run when you call a function
In the example above, you're calling the function twice on the last two lines of the code
/3
# — Define —
You _define_ the function when you use the `def` keyword
The first line of the definition is the function signature and the code after the colon is the code you want the function to perform…
However, this code does not run when you define a function
For this, we need to call the function…
/2
# Functions in Python—easy or hard to learn & master?
This week I'll be looking at aspects of functions which can be tricky for those who know basics and want to move to next level. If you've learnt about functions in Python are keen to take the next step, then hopefully this series of toots will help…
Let's start with the unexciting but important—terms
• _define_
• _call_
• _parameter_
• _argument_
Here's the code we'll use as an example which you can also get from the ALT text of the image in this toot:
`def greet_person(person):`
`print(f"Hello {person}! How are you doing today?")`
`greet_person("Ishaan")`
`greet_person("Elizabeth")`
You can follow this thread in the replies (unlisted)
/1
#Python #functions #coding #programming #LearnToCode #LearnPython
@hynek @asmodai @glyph this set up is more prone to acrimony than Twitter. Twitter has one moderation policy, whether you agree with it or not, carried out by “anonymous” Twitter staff. Here, the moderators are public and all do their own thing. This can only lead to more fracturing.
The model here will make social media bubbles a lot worse for those who choose overtly political servers who block any server with a different political view
I only interact with Python/programming people both on Twitter and here, so never had issues as they’re very friendly communities everywhere. But those who use these platforms for other topics will become more isolated from other views, not less
@Dwflanagan also, the instance I’m on has markdown as an option for writing toots, which although not as good as full syntax formatting, is good enough to create a simple code block. Unfortunately at the moment I think it’s one if the few and only those on the same server see the markdown version, everyone else sees plain text
@Dwflanagan adding the code as the ALT text of an image snippet is a very good solution that I’ve been using elsewhere and will be using here
@allenellis FWIW, this is what it appears for those on the #qoto server – but it makes it a bit pointless to create toots with markdown when most people will see a plain text version of it!
@allenellis I’ve since discovered only people on my server (and any others that implement markdown) see the markdown. Everyone else sees the plain text version. Which defeats the purpose for me!
@peterdrake @chris @s_gruppetta right!
I usually explain to my students by noting that __iadd__ stands for in-place addition and in-place operations are intended to mutate the original object when possible.
Though += and friends are most often used on strings and numbers, which are immutable so we don't see that behavior most of the time...
I do wonder whether it might have been more sensible to design Python to never mutate with augmented assignments, even on mutable objects.
@peterdrake @chris As @s_gruppetta said, it's all about how += works on lists.
+= on mutable objects is a combination of __iadd__ and =,
The __iadd__ call succeeds but the assignment fails
Interestingly += on tuples doesn't work this way! The += operator falls back to __add__ followed by an assignment on immutable objects.
So while
a += b
Is the same as
a = a + b
With a tuple or a string.
Those two lines aren't the same on a list!
This oddity is in the docs too: https://docs.python.org/3.5/faq/programming.html#faq-augmented-assignment-tuple-error
@peterdrake @chris @treyhunner This shows that `+=` is not quite just syntactic sugar for the separate `=` and `+`. One creates a new object; the other doesn't
@peterdrake @chris @treyhunner Oops, markdown error (shame there's no preview), the id for `b` in the first example should be on a new line, of course
@peterdrake @chris @treyhunner Trey is the master at these things, but here's my attempt.
In the first one, the expression after the `=` is executed first; this is fine as you're adding one list to another (using `list.__add__()`). This creates a new object. Then, there's an attempt to assign this new object as the first item in `a` which fails since `a` is a tuple.
In the second, the assignment and addition are attempted together using the `__iadd__()` dunder method, rather than `__add__()`. No new object is created.
Here's a different example which may shed some light:
`>>> b = [2]`
`>>> c = [3]`
`>>> id(b)`
`4407498688`
`>>> b = b + c`
`>>>`id(b)`
`4407508864`
`b` has a different id after the assignment, so it's a different object to the original `b`
now, in a new session:
`>>> b = [2]`
`>>> c = [3]`
`>>> id(b)`
`4394855808`
`>>> b += c`
`>>> id(b)`
`4394855808`
`b` is now the same object as it was before (same id)
@treyhunner Each time I see this I still have to stop and look at it again, despite having seen it before!
@ampanmdagaba @treyhunner I somehow suspect that many servers will very soon shift to enabling markdown like qoto.org does
@ampanmdagaba @treyhunner I did have the back ticks when I wrote it in markdown setting (which my editor/server has), and my toot was rendered like this for me and others on my server, but shows as plain text for others
@ampanmdagaba @treyhunner Yes, #QOTO does, which is what I'm on. The problem is that only other QOTO users or those on the (few, I assume) servers who support markdown will see it.
For example, this below will show as a proper code block to qoto.org users, but only them, unfortunately
`print("I love markdown")`
Still one of the oldest #Python oddities I've ever seen 🐍🤔
>>> a = ([],)
>>> a[0] += ["what"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a
(['what'],)
Note that an exception was raised but the operation also worked! 😳 #pythonoddity
• Rethinking how to teach programming – I prefer the friendly, relaxed approach when communicating about Python programming
• I write about Python on The Python Coding Book blog and on Real Python
• Former Physicist
• Expect posts on scientific and numerical programming –> NumPy, Matplotlib and friends!