How to save python codes in files?

Evan Klitzke evan at yelp.com
Wed Jun 13 10:15:10 CEST 2007


On 6/13/07, why? <jimbomaan at gmail.com> wrote:
>
> Evan Klitzke wrote:
> > On 6/12/07, why? <jimbomaan at gmail.com> wrote:
> > > Im working with Python 2.2 on my red hat linux system. Is there any
> > > way to write python codes in separate files and save them so that i
> > > can view/edit them in the future? Actually I've just started with
> > > python and would be grateful for a response. Thanx!
> >
> > Of course -- just put the code into a text file (using your favorite
> > text editor) and then run the script using the python command, e.g. by
> > executing on a command line:
> >   python my_program.py
> >
> > Since you're on a Linux system you can also use this as the first line
> > of your file and then chmod +x the file to make it into an executable
> > script:
> >
> > #!/usr/bin/env python
> >
> > --
> Im still unable to follow... :(
> See, if i have to save the following code in a file, how should i
> proceed?
> >>> def sum(x,y): return x+y
> ...
> >>>

If you want to write a program, you need to have some code that
actually does something (rather than just containing definitions of
functions). Here's a trivial example of a file using your sum
function:

#!/usr/bin/env python

def sum(x, y):
        return x+y

print 'Enter two numbers:'
x = int(raw_input())
y = int(raw_input())

print 'The sum is equal to %s + %s = %s' % (x, y, sum(x,y))

-- 
Evan Klitzke <evan at yelp.com>



More information about the Python-list mailing list