Basic Question Involving Functions (Python)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

MathDoofus
Posts: 247
Joined: Fri Jan 02, 2015 2:01 pm UTC

Basic Question Involving Functions (Python)

Postby MathDoofus » Mon May 01, 2017 1:23 pm UTC

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?

Demki
Posts: 193
Joined: Fri Nov 30, 2012 9:29 pm UTC

Re: Basic Question Involving Functions (Python)

Postby Demki » Mon May 01, 2017 4:12 pm UTC

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.

MathDoofus
Posts: 247
Joined: Fri Jan 02, 2015 2:01 pm UTC

Re: Basic Question Involving Functions (Python)

Postby MathDoofus » Mon May 01, 2017 4:20 pm UTC

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.

>-)
Posts: 512
Joined: Tue Apr 24, 2012 1:10 am UTC

Re: Basic Question Involving Functions (Python)

Postby >-) » Mon May 01, 2017 5:30 pm UTC

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

User avatar
Xanthir
My HERO!!!
Posts: 5228
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Basic Question Involving Functions (Python)

Postby Xanthir » Tue May 02, 2017 4:39 pm UTC

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.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

MathDoofus
Posts: 247
Joined: Fri Jan 02, 2015 2:01 pm UTC

Re: Basic Question Involving Functions (Python)

Postby MathDoofus » Wed May 03, 2017 1:38 am UTC

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?

Demki
Posts: 193
Joined: Fri Nov 30, 2012 9:29 pm UTC

Re: Basic Question Involving Functions (Python)

Postby Demki » Wed May 03, 2017 4:17 am UTC

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.

User avatar
The Great Hippo
Swans ARE SHARP
Posts: 6874
Joined: Fri Dec 14, 2007 4:43 am UTC
Location: behind you

Re: Basic Question Involving Functions (Python)

Postby The Great Hippo » Sat May 06, 2017 9:04 pm UTC

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)

User avatar
PM 2Ring
Posts: 3620
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Basic Question Involving Functions (Python)

Postby PM 2Ring » Mon May 08, 2017 4:51 pm UTC

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.

MathDoofus
Posts: 247
Joined: Fri Jan 02, 2015 2:01 pm UTC

Re: Basic Question Involving Functions (Python)

Postby MathDoofus » Wed May 10, 2017 5:58 pm UTC

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.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Basic Question Involving Functions (Python)

Postby ahammel » Thu May 11, 2017 2:28 am UTC

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.
He/Him/His/Alex
God damn these electric sex pants!

User avatar
pogrmman
Posts: 337
Joined: Wed Jun 29, 2016 10:53 pm UTC
Location: Probably outside

Re: Basic Question Involving Functions (Python)

Postby pogrmman » Thu May 11, 2017 4:15 pm UTC

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.

User avatar
ucim
Posts: 5635
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: Basic Question Involving Functions (Python)

Postby ucim » Thu May 11, 2017 5:26 pm UTC

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
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

User avatar
The Great Hippo
Swans ARE SHARP
Posts: 6874
Joined: Fri Dec 14, 2007 4:43 am UTC
Location: behind you

Re: Basic Question Involving Functions (Python)

Postby The Great Hippo » Thu May 11, 2017 10:57 pm UTC

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).

User avatar
PM 2Ring
Posts: 3620
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Basic Question Involving Functions (Python)

Postby PM 2Ring » Sat May 20, 2017 8:30 am UTC

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.

User avatar
phillip1882
Posts: 95
Joined: Fri Jun 14, 2013 9:11 pm UTC
Location: geogia
Contact:

Re: Basic Question Involving Functions (Python)

Postby phillip1882 » Sat Jun 17, 2017 5:00 pm UTC

python is a dynamically typed language, which is particularly useful for functions.

Code: Select all

def my_function(a,b,c):
     v = a+b+c
     return v

can handle integers and strings and even arrays. if they are integers, it will add them as expected and return the result. if strings, it will concatenate them and return. if arrays, it will add the elements of the the arrays ([1,2,3]+[4,5,6] = [5,7,9])
this is the power of python. it does the most logical thing possible with each data type. my suggestion is to write many, many test functions, and use print statements as often as possible to get a feel for what you can and cannot do.
bitcoin address: 1B73mjAguoK9ATwVjGcy2LasyfG9xtLGsq

User avatar
Flumble
Yes Man
Posts: 1951
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Basic Question Involving Functions (Python)

Postby Flumble » Sat Jun 17, 2017 6:36 pm UTC

phillip1882 wrote:it does the most logical thing possible with each data type.

When both operands of an operator are of the same type... kind of. But if the types differ, the only 'logic' is that you get the same result every time you combine some two types.

Note that this type of overloading is not at all unique to Python. Statically typed languages can also have generic functions like my_function : (x,x,x)->x (where x is a type variable that is replaced by a concrete type at every point it is used during compilation).

Nyktos
Posts: 138
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: Basic Question Involving Functions (Python)

Postby Nyktos » Fri Jun 23, 2017 9:39 pm UTC

phillip1882 wrote:can handle integers and strings and even arrays. if they are integers, it will add them as expected and return the result. if strings, it will concatenate them and return. if arrays, it will add the elements of the the arrays ([1,2,3]+[4,5,6] = [5,7,9])
This is not correct. While what you wrote arguably is the logical thing, + is concatenation for lists as well as strings.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Basic Question Involving Functions (Python)

Postby ahammel » Mon Jun 26, 2017 3:58 pm UTC

Nyktos wrote:
phillip1882 wrote:can handle integers and strings and even arrays. if they are integers, it will add them as expected and return the result. if strings, it will concatenate them and return. if arrays, it will add the elements of the the arrays ([1,2,3]+[4,5,6] = [5,7,9])
This is not correct. While what you wrote arguably is the logical thing, + is concatenation for lists as well as strings.
You could make a case that the logical thing is `[1, 2, 3] + [4, 5, 6] = [5, 6, 7, 6, 7, 8, 7, 8, 9]` (i.e. `[x + y for x in [1, 2, 3] for y in [4, 5, 6]]`). Particularly if you've written too much Haskell.
He/Him/His/Alex
God damn these electric sex pants!


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests