[Tutor] Beginner struggling with python.

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jun 22 20:07:10 EDT 2023


On 22/06/2023 22:45, Jack Simpson wrote:

> Please pardon my ignorance but is this email chain for fresh beginners
> also? 

Absolutely. Beginners to programming and beginners to Python
(regardless of programming experience).


> nicely until we got on to return values and defining functions. Are
> particular functions built into python such as def convert( etc.

There are function built into Python (and these are called builtins!:)
There's a lot more functions that come with Python in the form of modules.
You can add your own functions to your program or
You can create your own modules and share these across
programs or even with your friends and colleagues.

Defining functions is how we extend the basic language
to do more powerful things so that we can solve bigger
problems.

> my main issue is syntax required as I feel the course I am in brushed over
> it quite quickly.

Here is an excerpt from my tutorials chapter on functions
(there's a lot more on the web page, see the link in my .sig):

==================
The basic structure of a function call is as follows:

aValue = someFunction ( anArgument, another, etc... )

That is, the variable aValue takes on the value obtained by calling a
function called someFunction. The function can accept, inside
parentheses, zero or many arguments which it treats like internal
variables. Functions can call other functions internally. In most
programming languages, even if there are no arguments, we must still
provide the parentheses when calling a function. (Although VBScript is
an example of a language which does not require parentheses, even when
there are arguments, but it usually makes things clearer to include them.)

...

Defining our own functions
--------------------------
OK, so we know how to use the existing functions and modules, but how do
we create a new function? Simply by defining it. That is we write a
statement which tells the interpreter that we are defining a block of
code that it should execute, on demand, elsewhere in our program.

VBScript first
--------------

So let's create a function that can print out a multiplication table for
us for any value that we provide as an argument. In VBScript it looks like:

<script type="text/vbscript">
Sub Times(N)
Dim I
For I = 1 To 12
    MsgBox I & " x " & N & " = " & I * N
Next
End Sub
</script>

We start with the keyword Sub (for Subroutine) and end the definition
with End Sub, following the normal VBScript block marker style. We
provide a list of parameters enclosed in parentheses. The code inside
the defined block is just normal VBScript code with the exception that
it treats the parameters as if they were local variables. So in the
example above the function is called Times and it takes a single
parameter called N. It also defines a local variable I. It then executes
a loop to display the N times table, using both N and I as variables.

...

In Python the Times function looks like:

def times(n):
    for i in range(1,13):
        print( "%d x %d = %d" % (i, n, i*n) )
And is called like:

print( "Here is the 9 times table..." )
times(9)

Note that in Python procedures are not distinguished from functions and
the same name def is used to define both. The only difference is that a
function which returns a value uses a return statement, like this:

def timesTable(n):
   s = ""
   for i in range(1,13):
       s = s + "%d x %d = %d\n" % (i,n,n*i)
   return s

As you see it's very simply done, just return the result using a return
statement. (If you don't have an explicit return statement Python
automatically returns a default value called None which we usually just
ignore.)

We can then print the result of the function like so:

print( timesTable(7) )

Although we haven't followed this advice throughout the tutorial, it is
usually best to avoid putting print statements inside functions.
Instead, get them to return the result and print that from outside the
function. That makes your functions much more reusable, in a wider
variety of situations.

There is one very important thing to remember about return. Not only
does it return a value from the function but it also immediately returns
control back to the code that called the function. This is important
because the return does not have to be the last line in the function,
there could be more - and it may never get executed. Indeed there can be
more than one return statement in a function body and whichever return
is reached first will terminate the function and return its value to the
calling code.

=================

I hope that helps! :-)

> Are there any recommendations where I can access more tutorials for
> beginners to learn more and get good at the basics?
> 

There is a web page with a long list on python.org:

https://wiki.python.org/moin/BeginnersGuide/NonProgrammers

It includes mine along with many others. Pick one that you
find suits your learning style.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p2
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list