[Tutor] Beginner struggling with python.
dn
PythonList at DancesWithMice.info
Thu Jun 22 22:48:39 EDT 2023
On 23/06/2023 12.07, Alan Gauld via Tutor wrote:
> On 22/06/2023 22:45, Jack Simpson wrote:
...
>> nicely until we got on to return values and defining functions. Are
>> particular functions built into python such as def convert( etc.
Yes, Python offers various built-in functions as well as the opportunity
to write your own "custom functions".
The 'built-ins' include len( string ) which will return the number of
characters in the string - or number of elements in a "collection" when
used with a list or similar.
There are also 'functions' which are part of a data-type (class).
Sticking with strings for the moment: string.upper() will ensure each
character in that string is converted (where necessary) into
upper-case/capital-letters. In Python, we use the term "everything is an
object". So, this function is actually a "method" of the str[ing]
type/class. There is an important difference between a function and a
method - a method works on the value to which is is attached, eg
"string" in this case, whereas a function works independently of any
specific instance. (see "arguments" and "result" below - but this
distinction is a bit curly for a beginner) You can tell which you're
dealing-with, by the 'dotted-notation'.
Then there are the functions you can write yourself, as @Alan has described.
Noting that functions 'take in' certain (optional) values and return a
result, the general form is:
def name( arguments ):
# computation
return result
The "name" should be a meaningful description of the "computation", ie
the purpose of the function.
The "arguments" (zero or more) will be a comma-separated list of
identifiers which are inputs to the function. In @Alan's example, 7, to
produce the seven times-table or 9 when your math is better than mine...
The "computation" can be anything which Python will do for you.
Sometimes we use a function to attend to a sub-problem within the wider
application, ie produces a neatly labelled-result. Another use for
functions is when we repeat the same "computation" repeatedly, and
perhaps from different points in the application.
The "result" seems self explanatory. Every function returns a "result".
If there is a return-statement, that will compute a value. If not, the
return-value is None. The wording in the singular-form is slightly
mis-leading. If appropriate, a function can return a collection of
values, eg a tuple or a list - however it is still only one "collection"!
We don't have to def-ine built-in functions (or those which are
import-ed from a library).
The "name( arguments )" and "return result" components are often called
the "signature" of the function. Basically, the function says: if you
give me this, I'll give you that by-return. Hah! The "signature" is an
interface which sets-out all the 'rules' for what you can do with the
function - remember the len() example (above), its signature allows us
to apply it to a string or a list (or other "collection"). Flexibility!
("polymorphism").
The "signature" also advises what to expect as a result: OK the 'length'
part is right there in the function-name, but the signature tells us
that the return-value will be an integer. Again, not much of a surprise
given that we are expecting a number, and it is hard to have a string
with a negative-number of characters or indeed part of a character.
However, some functions are less obvious and thus the signature is the
means of ensuring that we use the function for the purpose and in the
manner its author intended.
In the Python REPL try: help( function_name )
Web.Ref: https://docs.python.org/3/library/functions.html#len
Using a function is termed "calling a function". As above, this can look
like:
number_of_characters = len( string )
or
seventh_times_table = times_table( 7 )
The general form here is:
identifier = function_name( parameters )
The "signature" is also generalised, but is at-least specific to that
particular function.
Do we need to document that "function_name" has to correspond to a
def-statement (either one of our own, or one built-in and provided to us)?
The optional parameters must be provided if the function/signature
demands. Some may be required. Others may be optional. There is a
variety of ways these may be expressed (a story for another day).
The function's return-value is passed 'across' the assignment to
"number_of_characters" or "seventh_times_table" (or passed directly into
the print() function*). The identifier will become the same data-type as
the result-data.
* you will have noticed that @Alan uses the function's result within a
print-call rather than an assignment statement, but we are still making
use of the return-value.
(just because this very afternoon, I've been helping someone-else who's
using it)
A free-to-download or read-on-the-web book is Al Sweigart's "Automate
the Boring Stuff". Chapter 3 deals with functions
(http://automatetheboringstuff.com/2e/chapter3/)
It's getting a bit old now, but the basics haven't changed...
@Alan mentioned online tutorials, quite possibly anticipating that I'd
chime-in with (an alternate/additional view): the suggestion of online
courses, eg Coursera, edX, FutureLearn, ... The advantage of these is
that they usually present their material in both text and video forms,
plus they collect a community of fellow-trainees, who will help
each-other with questions and readily-recognise which session/chapter
you're working at, right now.
(however, re-iterating @Alan's welcome, this Discussion List is also for
exactly those kinds of discussions)
Disclaimer: I use the edX platform - but not for Python training.
--
Regards,
=dn
More information about the Tutor
mailing list