Learning Python on a Mac

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

  1. Doju macrumors 68000

    Joined:
    Jun 16, 2008
    #1
    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...

    Code:
    #!/usr/local/bin/python
    
    import string, sys
    
    # If no arguments were given, print a helpful message
    if len(sys.argv)==1:
        print 'Usage: celsius temp1 temp2 ...'
        sys.exit(0)
    
    # Loop over the arguments
    for i in sys.argv[1:]:
        try: 
            fahrenheit=float(string.atoi(i))
        except string.atoi_error:
    	print repr(i), "not a numeric value"
        else:
    	celsius=(fahrenheit-32)*5.0/9.0
    	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.

    [​IMG]

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

    aarond12

    Joined:
    May 20, 2002
    Location:
    Dallas, TX USA
    #2
    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.

    Code:
    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.

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

    Joined:
    Jun 16, 2008
    #3
    Cheers, but how do I execute the code I had above?

    I could execute the below code:

    Code:
    #!/usr/local/bin/python
    
    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.

    [​IMG]
     
  4. Mernak macrumors 6502

    Joined:
    Apr 9, 2006
    Location:
    Kirkland, WA
    #4
    It doesn't look like you can do it through the GUI in IDLE, you would have to do something like
    Code:
    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
    Code:
    python path_to_script.py arg1 arg2
    Edit:
    Oh you can also modify it a bit and do something like
    Code:
    if __name__ == '__main__':
        list = prompt_for_arguments
        for i in list: convert(i)
     
  5. pineapple216 macrumors member

    pineapple216

    Joined:
    Apr 12, 2010
    #5
    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
    however.

    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