[Tutor] Need to write a python and call it within a python main program (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Feb 18 21:18:05 CET 2006


> I need help on the SYNTAX of Python
>
> I have programming experience in Fortran and a little bit of C.


Ah, ok; let's see if we can transfer some concepts from your previous
experience into Python.

Just as a note: please use your email client's "Reply to All" feature so
that our correspondence stays on the Python-tutor mailing list.  The idea
is that if I say something that is inaccurate or incomplete, the other
helpers can come and compensate for my weaknesses.



> I don't know how to get started.
>
> I've downloaded Python, and have used the idle as a desk calculator, but
> do not yet know how to write programs in it.

You may want to do a quick run-through of:

    http://www.python.org/doc/tut/

which will help give you a better sense of the syntax of the language.




> Do you already know about conditionals?
>
> *******************
>
> I know about the == relational operator.
>
> ***************


Python has a familiar control-flow syntax for branches in the 'if'
statement.  For example, here is an interactive session that shows how one
can use it for a simple case-analysis:

######
>>> def is_even(n):
...     if n % 2 == 0:        ## % is the modulo operator
...         return "yes"
...     else:
...         return "no"
...
>>> is_even(42)
'yes'
>>> is_even(13)
'no'
######

This is a function that takes an argument 'n', and returns either a 'yes'
or 'no' output.  If you look through a tutorial like the official Python
tutorial, you should see more usage of the "if" statement to handle an
arbitrary number of cases.




> Do you know how to test a function?
>
> Do you mean
> (1)  call the function with test values to see if it performs the way you
> want it to?
> (2)  something else that is Python related that I know nothing about?


#1.  I was running under the assumption that you hadn't programmed before.
But since you have, I think you know how to test a program so that you
have a reasonable idea of how well a program is doing what you want.




> Please help me with the syntax.  I don't know how to tell python to run
> a main program and use subroutines.  I don't know how to make those
> subroutines available to the main program.


I think the best thing I can do is point you to tutorials that you can use
to get a handle for the syntax.  There is:

    http://www.python.org/doc/tut/

as I pointed to earlier, but there's also:

    http://wiki.python.org/moin/BeginnersGuide/Programmers

which are tutorials that are targetted toward people who already know how
to program.


If you have more questions, please feel free to email 'tutor at python.org'.
Good luck!



More information about the Tutor mailing list