Learning Python on a Mac

Discussion in 'Mac Programming' started by Doju, Jul 9, 2010.

  1. Doju macrumors 68000

    Jun 16, 2008
    Hi, and apologies if this is the wrong forum, it seemed to be the closest to what I was interested in.

    I'm new to programming altogether, and was told Python would be a great place to start. So, here I am, interested in learning Python (unless someone recommends Ruby or something).

    I've ran into a bit of a speed-bump, however, with learning how to start. I've found some tutorials and example code, but they all seem to deal with Windows. How do I take the code and execute it?

    Is there an application I should use?

    This was a script I found...

    import string, sys
    # If no arguments were given, print a helpful message
    if len(sys.argv)==1:
        print 'Usage: celsius temp1 temp2 ...'
    # Loop over the arguments
    for i in sys.argv[1:]:
        except string.atoi_error:
    	print repr(i), "not a numeric value"
    	print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5))
    Now how do I take that and execute it in order to take advantage of its usage (basic Fahrenheit to Celsius converter)?

    I tried download the Mac downloader on Python.org but I can't seem to get the IDLE to open, which I guess is a good thing to have.


    Can anyone give me a hand? :(
  2. aarond12 macrumors 65816


    May 20, 2002
    Dallas, TX USA
    On my computer, at least, Python is located in /usr/bin, not /usr/local/bin, so you will need to change the first line of the script.

    If that doesn't work use the "whereis" command to find Python on your system.

    whereis python
    I modified your code for the location of Python on my system and it worked. Don't forget to use chmod +x filename.py when executing your code.

    ./celsius.py 36 79
    36�F = 2�C
    79�F = 26�C
  3. Doju thread starter macrumors 68000

    Jun 16, 2008
    Cheers, but how do I execute the code I had above?

    I could execute the below code:

    def C2F():
    	Celsius = input('Degrees celsius: ');
    	Fahrenheit = 1.8 * Celsius +32;
    	print Celsius, 'Celsius =', Fahrenheit, 'Fahrenheit'
    By selecting "Run Module" in IDLE, but when I have to type C2F() for it to ask for input. How do I do this for ones without the "def" part? (Such as the one far above, the first, how would I execute that?)

    See, this is what I did for the most recent one with def in it, I just don't know how to do it for ones without def like the first code, I saw the def one on YouTube.

  4. Mernak macrumors 6502

    Apr 9, 2006
    Kirkland, WA
    It doesn't look like you can do it through the GUI in IDLE, you would have to do something like
    idle.py -r scriptname.py arg1 arg2 arg3
    or use something like Editra, which has Run... and a command line argument box (unfortunately it doesn't have the cool run module feature, but that is the only think I liked about IDLE). Or you can do it in the Terminal
    python path_to_script.py arg1 arg2
    Oh you can also modify it a bit and do something like
    if __name__ == '__main__':
        list = prompt_for_arguments
        for i in list: convert(i)
  5. pineapple216 macrumors member


    Apr 12, 2010
    Great that you want to learn python!
    It's a very readable language, and more forgiving than other languages.
    I learned programming in python for my bio-informatics classes.

    I too suggest that you use a program like Editra, or Vim

    I myself use Textmate, it's a nice program, customizable and with a run function like in IDLE. You're scripts will be run in terminal instead. It's not free

    Installing the mac version of python on your mac probably changed the version of python used (Snow Leopard comes preinstalled with version 2.6.1).

    And also the $PATH variable.
    You should google for a solution, you're not the first with this problem.

    (In the past I installed macpython too, but quickly reverted to using the stock installed python).
    There are plently of tutorials on the internet if you want to know more about python.

    Good luck! ;)

Share This Page