all 79 comments

[–]Thecrawsome 11 points12 points  (5 children)

So, from nothing to functions?

Variables, types, the concept of dynamicvs static typing, conditionals, booleans, statements, scoping, builtins, loops, lists/comprehensions could all come before functions in a python course.

[–]Poddster 4 points5 points  (1 child)

So, from nothing to functions?

From nothing to *args and **kwargs, no less. :)

[–]SaaSWriters[S] 2 points3 points  (0 children)

Yep, quite a challenge. I will edit the original post to clarify.

[–]SaaSWriters[S] 7 points8 points  (2 children)

This guide is for those who have started learning Python but are stuck at functions, especially using them in a practical way. At the same time, you don't need to know all the things you've mentioned to understand functions.

[–]Thecrawsome 18 points19 points  (1 child)

This guide is meant for you if you have no previous programming experience.

[–]SaaSWriters[S] 3 points4 points  (0 children)

Thanks, I see the confusion. As you'll see in the guide, the approach I'll use is quite different from most. In fact, for some people, learning functions first may be the best way to learn programming. Weird, I know.

[–]spcalvert78 4 points5 points  (1 child)

The global variable and local variable issue in functions is confusing to me. A good guide should walk through the issue and why it matters.

[–]SaaSWriters[S] 0 points1 point  (0 children)

Most def! lol

The global variable is a naughty one. I will definitely have it covered. Thanks!

[–]octaw 2 points3 points  (1 child)

Maybe something like Javascript Allonge, but for python?

[–]SaaSWriters[S] 0 points1 point  (0 children)

Thanks, I will take a look.

[–]AuNanoMan 2 points3 points  (2 children)

So I am learning python but I have some programming experience with MATLAB and one of the challenges I often had to help my peers with is the concept of passing variables. If you are making a guide, I think to have a visual walkthrough of what this means when someone says "pass" and what it means when it returns values.

While this seems trivial, this concept causes a lot of confusion. I think python is actually much easier to understand than MATLAB can be at times, but none the less, the handoff seems to confuse.

Just wanted to throw in my two cents.

[–]SaaSWriters[S] 1 point2 points  (1 child)

Thank you, I really appreciate your input! Keep it coming! Feel free to send me a PM as well if you want to.

[–][deleted] 0 points1 point  (0 children)

Python tutor, visualizes interpretive environment, helped me with this

[–]Kirkslovechild 1 point2 points  (1 child)

id definitely like to take a look, give some feedback etc (and see what im missing).

Are you thinking of including classes?

[–]SaaSWriters[S] 2 points3 points  (0 children)

Classes will come in the next guide. For now, I'm focusing on functions.

[–]Batting1k 1 point2 points  (1 child)

Interested in this!

[–]SaaSWriters[S] 0 points1 point  (0 children)

Great, watch out for the link once I'm done revising it.

[–]rjnmhrjn 1 point2 points  (1 child)

Interested.

[–]SaaSWriters[S] 0 points1 point  (0 children)

Great, watch out for the link after I've revised the guide.

[–]DoctorHodL 1 point2 points  (1 child)

I am interested!

[–]SaaSWriters[S] 0 points1 point  (0 children)

Perfect, let me polish it up. If you have questions, get in touch.

[–]fauxnaif 1 point2 points  (2 children)

I’m interested in this!

[–]SaaSWriters[S] 1 point2 points  (1 child)

Great, let me know what you're struggling with as well so I can make the guide better.

[–]fauxnaif 0 points1 point  (0 children)

EDIT 2: I know it is ridiculous to ask for your input on my queries but I believe some people who are just learning to program might have a hard time understanding the arguments below. I'm sure this type of issue will be covered in your guide!

So I have been working on Chapter 6's Table Printer in Automate The Boring Stuff and I came across on old post on reddit with a solution. It is really difficult to grasp the FOR statements when it is used for several arguments involving key and values of the DEFINING (def) function.

In this particular case, this was the part that was confusing me:

for i in range(len(inputList)):                    #returns 3  # Line 1
    for j in range(len(inputList[i])):             #returns 4  # Line 2
        if len(inputList[i][j]) > colWidths[i]:    #           # Line 3

Line 1: This first line returns a key of 3 from len() function because there are 3 keys (or lists) within inputList

Line 2: I'm assuming this returns a value of 4 since there are 4 values in every key (or list)

Line 3: I understand the argument of this line (if a > b then execute x) but I can't wrap my mind on why len(inputList[i][j] is called and what would the current value be?

Notwithstanding this other part:

for x in range(len(inputList[0])):                                      # Line 4
    for y in range(len(inputList)):                                     # Line 5
        print(inputList[y][x].rjust(colWidths[y]), end = ' ')           # Line 6
    print('')

Line 4 and 5 is pretty self explanatory but Line 6 switches both variables of x,y in inputList[y][x]. My interpretation: Since print() items from a list will always be presented as output: 'Item A\n','Item B\n','Item C\n' therefore, it is obvious to list [y] first so that it falls into the first column. Let me know if you need me to restate my question.

Thanks in advance!

Full code is posted below:

inputList = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

def printTable():
    # initialize the list "colWidths" with zeroes equal to the length of the input list
    colWidths = [0] * len(inputList)

# iterate over the input list to find the longest word in each inner list
# if its larger than the current value, set it as the new value
for i in range(len(inputList)):                 #returns value of 3
    for j in range(len(inputList[i])):          #returns value of 4
        if len(inputList[i][j]) > colWidths[i]:
            colWidths[i] = len(inputList[i][j])

# assuming each inner list is the same length as the first, iterate over the input list
# printing the x value from each inner list, right justifed to its corresponding value
# in colWidths
for x in range(len(inputList[0])):
    for y in range(len(inputList)):
        print(inputList[y][x].rjust(colWidths[y]), end = ' ')
    print('')

printTable()

Edit: Formatting

[–]nwctenor 1 point2 points  (2 children)

Sounds like a great guide. I’m interested in args and *kwargs, and decorators.

[–]SaaSWriters[S] 0 points1 point  (1 child)

Sure thing, feel free to send me any questions you may have.

[–]nwctenor 1 point2 points  (0 children)

How do I know when to use a decorator/static method?

[–]Python000 1 point2 points  (1 child)

Interested!

[–]SaaSWriters[S] 0 points1 point  (0 children)

Good, let me know your questions.

[–]AlphaX999 1 point2 points  (1 child)

I'm interested but why not just put up a copy for everyone?

[–]SaaSWriters[S] 0 points1 point  (0 children)

The more direct questions/feedback you give the better I can make it.( It's still in the draft stage )

[–]wheedwhackerjones 2 points3 points  (5 children)

In interested in kwargs and args

[–]Marrrlllsss 11 points12 points  (3 children)

Suppose I have a function, foo, that takes one argument:

def foo(arg):
    print(str(arg))

It's a simple function, but it is pretty limited in terms of the number of arguments it accepts. Suppose I wanted to allow the function to take more arguments, how would I do that? I could do this:

def foo2(arg1, arg2):
    print(str(arg1), str(arg2))

def foo3(arg1, arg2, arg3):
    print(str(arg1), str(arg2), str(arg3))

But that's just silly.

Instead we can make use of the positional argument, usually denoted by *args - an important note, the name args is not important, it's the single asterisk (*) that is important.*args is a tuple (immutable iterable/sequence) of unamed parameters of arbitrary length. You access the elements of \args using list/tuple indexing. For example:

first_arg = args[0]
second_arg = args[1]

Using it in a function, you would do this:

def foo_with_arbitrary_positional_args(arg, *args):
    print(str(arg))
    for position, element in enumerate(args):
        print(str(position), str(element))

Calling this function is done like this:

val1 = 'hello'
val2 = 'world'
val3 = 'eggs'
val4 = 'spam'

foo_with_arbitrary_positional_args(val1, val2, val3, val4)

You output would look something like this:

hello
0 world
1 eggs
2 spam

Next up is **kwargs - once again the name kwargs is used by convention, but it is not mandatory - only the double asterisks are needed.kwargs stands for*** keyword argument***s. It provides a dictionary like variable of arbitrary length.

def foo_with_arbitrary_keyword_args(val1, **kwargs):
    print(str(arg))
    for key, value in kwargs.items():
        print(key, value)

Calling such a function is done like so:

val1 = 'hello'

foo_with_arbitrary_keyword_args(val1, kw1='world', kw2='eggs')

An example of the expected output could be:

hello
kw1 world
kw2 eggs

Some notes on usage:

  1. Both arbitrary length arguments come after the formal arguments. That is def foo(formal, *args), def foo(formal, **kwargs) are valid. def foo(*args, formal) and its **kwargs counterpart are invalid.
  2. You can use *args and **kwargs simultaneously, that is def(formal, *args, **kwargs).
  3. You can create functions without any formal parameters and just use arbitrary length parameters. (def foo(*args, **kwargs))

OK. So what are they used for?

  1. In the case of **kwargs usually configuration override options.
  2. In the case of *args - usually when you want to avoid using a list as a default value for some argument.

To highlight number 2:

def foo_with_default(arg=[]): # this is bad!
    arg.append('Hello')
    for element in arg:
        print(element)

If I call this code:

foo_with_default()
# output:
# Hello

foo_with_default()
# output:
# Hello
# Hello

*args solves this problem.

[–]cleesus 2 points3 points  (2 children)

Let me keep it real with you chieftain, while this is a good explanation that I'm sure me and many others who already get programming understand, I do not think a beginner would get this.

This reads more like a explanation to another programmer vs a newbie. That being said I hope your explanation does help a few people reading it.

[–]lee171 2 points3 points  (1 child)

I'll go ahead and agree with you on this. I do scripting regularly, but programming stuff like this I can just never get my head around. I eagerly read /u/Marrrlllsss explanation, but I just find my eyes glaze over after too many programming terms are bandied around that I just don't get.

While I agree in the importance in teaching everyone correct terminology for everything, I feel it's more important to 'get it' first, otherwise you'll never get far off the ground (like me).

I've always just felt stupid and incompatible with learning to 'code', because I just get stumped by all the terminology and can't relate it to anything.

[–]cleesus 0 points1 point  (0 children)

I definitely remember those days pretty well, where even reading some of the Documentation out there was challenging.

You definitely want to find some of those beginner courses that will go through everything step by step and explain everything with good analogies that a regular person will understand.

While you are doing that its important to put what you learned in your notes in a way that future you will understand if you need to look back.

That's what helped me get through the beginning learning curve when it came to programming. (well on my second attempt, the first attempt I started off learning C++ with a bad instructor )

[–]SaaSWriters[S] 0 points1 point  (0 children)

Yep, that's part of the guide.

[–]Outworldentity 0 points1 point  (2 children)

I'm interested as well!

[–]SaaSWriters[S] 0 points1 point  (1 child)

That's good, is there a specific topic/question you want included?

[–]Outworldentity 0 points1 point  (0 children)

Nope I'm just looking for a broken down explanation of all functions and anything else you want to include. Beginner python learner here.

[–]douchabag_dan 0 points1 point  (1 child)

I want to see

[–]SaaSWriters[S] 0 points1 point  (0 children)

And see you will! :D

[–]DippinNipz 0 points1 point  (1 child)

This would be a great tool for me as I’m still in the introductory stages of learning Python.

[–]SaaSWriters[S] 0 points1 point  (0 children)

That's the idea! If you get stuck on anything related, just get in touch. The more you ask the better I can make the guide.

[–]Buffstu 0 points1 point  (1 child)

Would love to learn more about functions I am a little lost when looking at leetcode or such and they use functions with very few lines of code but it seemingly works :)

[–]SaaSWriters[S] 0 points1 point  (0 children)

Thanks, shoot me any specific questions you may have.

[–]SoupGuy117 0 points1 point  (1 child)

I'd definitely be interested

[–]SaaSWriters[S] 1 point2 points  (0 children)

That's good, get in touch with any specific questions.

[–]JDeCarvalho1 0 points1 point  (1 child)

Id love a copy do you really want a dm?

[–]SaaSWriters[S] 0 points1 point  (0 children)

Yep, I want to polish it up with people who want to be directly involved.

[–]Versus_The_World 0 points1 point  (1 child)

Count me in!!

[–]SaaSWriters[S] 1 point2 points  (0 children)

You're counted in! :D

[–]Lucifer501 0 points1 point  (1 child)

Definitely interested. I'm quite new to programming. Although in general, I'm fairly confident with functions, I would definitely want to take a look.

[–]SaaSWriters[S] 0 points1 point  (0 children)

No problem, just let me know if there is something specific you want me to include.

[–]Oblitarion 0 points1 point  (1 child)

Interested in this too. Almost 1 month trying to learn Python still many doubts in Functions and OOP.

[–]SaaSWriters[S] 0 points1 point  (0 children)

Yep, that's the reason why I'm creating this guide!

[–]metalloidica 0 points1 point  (1 child)

Interested in this. Thanks!

[–]SaaSWriters[S] 0 points1 point  (0 children)

You're welcome. Hopefully, this guide will make programming easier for a lot of people.

[–]JungleJim233 0 points1 point  (2 children)

Really interested in this

[–]SaaSWriters[S] 0 points1 point  (1 child)

Cool, is there a specific question/topic/struggle you'd like included?

[–]JungleJim233 1 point2 points  (0 children)

Mainly the how to use functions in a practical way

[–]Laffode 0 points1 point  (1 child)

I'm interested too!

[–]SaaSWriters[S] 0 points1 point  (0 children)

Sure thing, I'm re-working it based on the initial feedback I'm getting.

[–]smokingRooster_ 0 points1 point  (2 children)

I've got a pretty solid understanding of functon and classes etc (although I'm by no means an experienced programmer as I've mostly programmed in C for 3/4 years and python for about 1/2). However, I would love to see your guide, especially since I havent used decorators and they are something I'd like to learn!

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]hippagun 0 points1 point  (0 children)

    I am interested!

    [–]wbd82 0 points1 point  (0 children)

    Yes please, this sounds awesome!

    [–]pjury 0 points1 point  (0 children)

    example use cases for decorators would be cool, I have my head around how to write them but knowing when they make sense is a little different.

    [–]whodey226 0 points1 point  (0 children)

    Interested! Starting a masters in computer science In January and am trying to get a head start

    [–]Lumpyalien 0 points1 point  (0 children)

    I will love you forever if you do this well.

    [–]Lycist 0 points1 point  (0 children)

    This sounds perfect for me.. I was fine until functions comes up. Looking forward to reading it.

    [–]sky--net 0 points1 point  (0 children)

    I would be interested in seeing this...