r/learnpython • • Feb 20 '21

What are you having difficulty learning or are currently struggling to learn?

I'm curious what the community here has difficulty with when it comes to learning Python. Partly because I want to see if the more experienced community here (or myself) can help point you into the right direction. . . but also because I'm trying to come up with video tutorial ideas for future content and would love to hear from Python learners what would be most useful to dedicate our time on.

I'm looking forward to hearing what you guys have to say!

417 Upvotes

324 comments sorted by

View all comments

2

u/[deleted] Feb 20 '21

Functions and fitting together the parameters/arguments/syntax.

1

u/MaraudingAvenger Feb 21 '21

Couple of quick things on functions:

Function syntax

def function_name( argument, keyword=default_value): #colon at the end
    # indented block of instructions for what the function does

That function above has to be provided with one argument ("arg") when you use it, and can optionally be provided with a keyword argument ("kwarg") if you want, otherwise it will get/provide itself with whatever you set default_value to in the function definition.

Let's say that the function is written like this:

def say_hi ( name, message="Hi there,"):
    print(message, name)

When you call it, you can pass it a name only:

>>> say_hi("Jimbo")
Hi there, Jimbo

or you can pass it a name and a message in one of two ways:

>>> say_hi("Jimbo", "You absolute unit,")
You absolute unit, Jimbo
>>> say_hi("Jimbo", message="What an absolute unit, that")
What an absolute unit, that Jimbo

In the first one, python just takes whatever the second thing is and stores it in message. The second one specifically says "put this thing in message".

Function parameters + parameter syntax

Using the same kind of intro as above:

def func ( arg1, arg2, ...arg_n, kwarg1, kwarg2,...kwarg_n):
    # indented block of instructions

All your arguments go on the left, all your keyword arguments go on the right. So for something like:

def i_ate( food1, food2, drink1="tea", drink2="coffee"):
    print("I ate", food1, "and", food2)
    print("and I drank", f"{drink1} and {drink2}")

You can use that function like:

>>> i_ate("beef", "pork")
I ate beef and pork
and I drank tea and coffee

You can also use the names of non keyword arguments as keywords:

>>> i_ate(food2="beef", food1="pork")
I ate pork and beef
and I drank tea and coffee

But once you start using keyword arguments (kwargs) you can't switch back within the same call to a function. For instance, this is valid:

>>> i_ate("a monkey", food2="a small child", drink1="twisted tea", drink2="the blood of the innocents")
I ate a monkey and a small child
and I drank twisted tea and the blood of the innocents

But this throws an error:

>>> i_ate("a monkey", food2="a small child", "twisted tea", drink2="the blood of the innocents")
File "<ipython-input-10-c0bb8de06e3d>", line 1
    i_ate("a monkey", food2="a small child", "twisted tea", drink2="the blood of the innocents")
                                                                                               ^
SyntaxError: positional argument follows keyword argument

It tells you what you did wrong -- I put a positional (non-keyword) argument after a keyword argument.

Is that kind of intro what you need? There's more with packing/unpacking arguments if you're interested!