Page 1 of 1

Basic Question Involving Functions (Python)

Posted: Mon May 01, 2017 1:23 pm UTC
by MathDoofus
I'm trying to learn a programming language, and after some research decided to explore Python.

But my first difficulty with Python involves functions. I can't seem to figure out how to use them, or how they work. Are they related to functions in mathematics? My stumbling block is that I have trouble distinguishing between what I must define and what I can simply trust Python to handle by invoking some particular function. Has anyone experienced this before when starting out in programming?

Re: Basic Question Involving Functions (Python)

Posted: Mon May 01, 2017 4:12 pm UTC
by Demki
While I am not too well versed in python, I think it is better for you to think about these functions as procedures, or subroutines, basically they are there to divide up your code and eliminate a lot of rewriting code.
Some languages allow passing functions as arguments to other functions, which may simplify code sometimes.

Re: Basic Question Involving Functions (Python)

Posted: Mon May 01, 2017 4:20 pm UTC
by MathDoofus
Demki wrote:While I am not too well versed in python, I think it is better for you to think about these functions as procedures, or subroutines, basically they are there to divide up your code and eliminate a lot of rewriting code.
Some languages allow passing functions as arguments to other functions, which may simplify code sometimes.


Thank you. For a particular function, how do I know how much input (or how many arguments) it can accept (and in what format)? The Python tutorials that I've read simply assume that the reader has this knowledge, but I'm totally new to programming.

Re: Basic Question Involving Functions (Python)

Posted: Mon May 01, 2017 5:30 pm UTC
by >-)
if the function name is fn, you can run the command help(fn) to print out instructions and information on what inputs it takes. google is also another resource

Re: Basic Question Involving Functions (Python)

Posted: Tue May 02, 2017 4:39 pm UTC
by Xanthir
Since you're a total beginner, I recommend steering away from most Python tutorials; they'll generally be teaching you Python itself, and assume you already have basic knowledge of how programming itself works.

There's a big list of Python-related "I'm totally new to this whole programming nonsense" tutorials at https://wiki.python.org/moin/BeginnersGuide/NonProgrammers that might be helpful to you, tho.

Re: Basic Question Involving Functions (Python)

Posted: Wed May 03, 2017 1:38 am UTC
by MathDoofus
Xanthir wrote:Since you're a total beginner, I recommend steering away from most Python tutorials; they'll generally be teaching you Python itself, and assume you already have basic knowledge of how programming itself works.

There's a big list of Python-related "I'm totally new to this whole programming nonsense" tutorials at https://wiki.python.org/moin/BeginnersGuide/NonProgrammers that might be helpful to you, tho.


How do I pick up basic knowledge of how programming itself works?

Re: Basic Question Involving Functions (Python)

Posted: Wed May 03, 2017 4:17 am UTC
by Demki
The tutorials and books Xanthir linked are aimed at people who have no programming backgroud, so they should teach you, along with the specifics of the python language, how programming works. I didn't check them all but they probably include example problems and solutions(using python).
Once you have a good grasp on python, I recommend solving many small programming problems, there are plenty of sites that provide you with problems, and even check your solution works.

Re: Basic Question Involving Functions (Python)

Posted: Sat May 06, 2017 9:04 pm UTC
by The Great Hippo
MathDoofus wrote:Thank you. For a particular function, how do I know how much input (or how many arguments) it can accept (and in what format)? The Python tutorials that I've read simply assume that the reader has this knowledge, but I'm totally new to programming.
One way is to just try sending arguments to the function to see what happens. repl.it is a great resource for exploring python on impulse, without even having to load anything. Whenever I'm unsure about how something in Python works, I play with it using repl.it to find out more about it. repl.it includes interpreters for other languages, too.

Well written functions should have documentation (basically, a description) that tells you how many arguments they accept and in what format. That's what you're viewing when you write help(fn).

When I learned Python, the three biggest things I had to learn about functions are as follows:
  • Functions, in Python, always return a value. That is to say, help(fn) returns some value. If you don't specify a value for the function to return (via the return statement), the function will return None (which is, mathematically, basically the equivalent of "undefined").
  • Functions are first class objects. All this really means is that you can treat a function like any other object in Python. You can type help(help) for example (I haven't actually tried this, but it should work!). You can type print(help). Etc.
  • Functions accept arguments in two forms: Positional and Keyword. Positional arguments are values you just pass to the function:

    Code: Select all

    fn(arg1, arg2, arg3)
    Keyword arguments are assigned to keywords.

    Code: Select all

    fn(key2=arg2, key1=arg1, key3=arg3)
    Some functions will let you mix both, so long as the keywords always come *after* the positionals.

    Code: Select all

    fn(arg1, arg2, key3=arg3)

Re: Basic Question Involving Functions (Python)

Posted: Mon May 08, 2017 4:51 pm UTC
by PM 2Ring
MathDoofus wrote:I'm trying to learn a programming language, and after some research decided to explore Python.


Given the conceptual difficulties that you've had in your Mathematics threads viewtopic.php?f=17&t=122713 and viewtopic.php?f=17&t=122712 I really don't think you're ready to learn a programming language.

Re: Basic Question Involving Functions (Python)

Posted: Wed May 10, 2017 5:58 pm UTC
by MathDoofus
PM 2Ring wrote:
MathDoofus wrote:I'm trying to learn a programming language, and after some research decided to explore Python.


Given the conceptual difficulties that you've had in your Mathematics threads viewtopic.php?f=17&t=122713 and viewtopic.php?f=17&t=122712 I really don't think you're ready to learn a programming language.


If you were to go back and check (rather than reflexively posting discouraging things about people--including me--who are admitted neophytes), you'd see that I've finally grasped the halting problem. But thanks for trying to play the gatekeeper-of-all-programming.

Re: Basic Question Involving Functions (Python)

Posted: Thu May 11, 2017 2:28 am UTC
by ahammel
The Great Hippo wrote:Functions, in Python, always return a value. That is to say, help(fn) returns some value. If you don't specify a value for the function to return (via the return statement), the function will return None (which is, mathematically, basically the equivalent of "undefined").

Spoilered for nit-picking irrelevant to the novice programmer:
Spoiler:
I think None is more like Unit (the type with only one value) in math terms.

Re: Basic Question Involving Functions (Python)

Posted: Thu May 11, 2017 4:15 pm UTC
by pogrmman
MathDoofus wrote:I'm trying to learn a programming language, and after some research decided to explore Python.

But my first difficulty with Python involves functions. I can't seem to figure out how to use them, or how they work. Are they related to functions in mathematics? My stumbling block is that I have trouble distinguishing between what I must define and what I can simply trust Python to handle by invoking some particular function. Has anyone experienced this before when starting out in programming?


OK, most other people have given a pretty good explanation of functions. Much like in math, they take some input, and produce some output (presumably based on the input). The input and output can be anything. You could, for instance, write a function that takes a text file as input, and outputs a list of words in that file that start with "a".

In programming, functions are used as a way to organize your code. Say you have a program that, for some reason or another, needs to find the mean of several different lists. Instead of writing the mean-finding algorithm every time you need to do it, functions allow you to break that off into a seperate piece of code, and trust that that code will return the mean when passed a list.

A part of learning a programming language is learning what functions are provided by the language. It is different for every language. Additionally, people publish groups of useful functions for doing certain things. You can make your code rely on those as well.

Re: Basic Question Involving Functions (Python)

Posted: Thu May 11, 2017 5:26 pm UTC
by ucim
Might as well cover this now; functions can do more than just return a value. Functions can do things. For example, a function can take a text array as input (the "argument"), print the text on a printer ("an action"), capitalize the first letter of every word in the text array (a "side effect"), and return the number of words (the "return value"). Sometimes the action is the main reason for the function (so the return value may simply indicate that the action was performed successfully without hearing back that there were any problems). Sometimes the side effect is the main reason for the function (such as cleaning user input, but see below), and sometimes the return value is the main reason for the function (taking the mean, median, and mode of a sample set).

Depending on the language, implementation, and program, a function may alter values that the rest of the program sees. These "side effects" are frowned upon in some (newer) languages and programming philosophies. Side effects make functions more dependent on how they are embedded in the program; functions with no side effects can operate more flexibly and predictably. Nonetheless, in some situations they can be useful, and there's a tradeoff between efficiency, modularity, readability, and a few other things. Functions with no side effects and no hidden dependencies (e.g. reliance on global variables) are sometimes called "pure" functions.

Jose

Re: Basic Question Involving Functions (Python)

Posted: Thu May 11, 2017 10:57 pm UTC
by The Great Hippo
ahammel wrote:Spoilered for nit-picking irrelevant to the novice programmer:
Spoiler:
I think None is more like Unit (the type with only one value) in math terms.
Pardon -- you are quite right! I've heard None described as one of the few occurrences of a singleton in Python (and the existence of NoneType seems to only confirm that).

Re: Basic Question Involving Functions (Python)

Posted: Sat May 20, 2017 8:30 am UTC
by PM 2Ring
MathDoofus wrote:
PM 2Ring wrote:
MathDoofus wrote:I'm trying to learn a programming language, and after some research decided to explore Python.


Given the conceptual difficulties that you've had in your Mathematics threads viewtopic.php?f=17&t=122713 and viewtopic.php?f=17&t=122712 I really don't think you're ready to learn a programming language.


If you were to go back and check (rather than reflexively posting discouraging things about people--including me--who are admitted neophytes), you'd see that I've finally grasped the halting problem. But thanks for trying to play the gatekeeper-of-all-programming.


I didn't mean to offend you, or to turn you off learning to program. FWIW, I did read every post in both of those threads before I posted in here. I admire your persistence in trying to understand those topics, and I agree that you definitely made progress in both of those threads. However, you did repeatedly state that you have learning difficulties associated with mathematical notation, functions, and pseudo-code. That is going to be a big impediment to coding in any language, so I feel that you will have a rather hard time if you try to teach yourself Python at this stage. If you can find yourself a competent teacher, preferably one that you can interact with directly, not just online, that could make a big difference.

Apart from finding a good teacher, I really don't know what to suggest. You don't need to be a great mathematician to be a programmer, but the syntax of programming languages is somewhat mathematical, and if you can't get comfortable with that you won't have a lot of fun learning to code. I suppose you should keep doing what you're doing, improving your mathematical skills, because the constant exposure to mathematical notation and thought processes could help you overcome those learning difficulties associated with mathematical stuff.

I guess you could just dive into learning Python, or maybe JavaScript, but I suspect that without a good teacher to guide you that you'll find the experience confusing and frustrating. In the early stages of learning to program it's easy to misunderstand stuff, and such misconceptions can block your progress if you don't have an expert on hand to explain things properly.