+ Reply to Thread
Results 1 to 10 of 10

Thread: Python Question

  1. #1
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Python Question

    Has anybody got a simple explination of the 'indent rule' when programing in Python?

    Am finally getting around to exploring the uses the Google Apps Engine and any links for tutorials or code examples would be useful.
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Python Question

    You don't use { brackes } to group statements in Python , you use indentation.

    Using brackets:

    Code:
    if( x > 1 ){
    
    x = x + 14 ;
    foobar( x );
    
    } else {
    x = x + 1:
    blarg(x);
    }
    
    y = 4;
    Using indentation to create blocks:

    Code:
    if x > 0 :
        x = x + 14
        foobar(x)
    else:
        x = x + 1
        blarg(x)
    y = 4
    Note that since y = 4 is on the same indent level as the if/else, it is not part of the else clause.
    Nothing is always absolutely so.

  3. #3
    vv.bbcc19's Avatar
    vv.bbcc19 is offline Community Advocate vv.bbcc19 is just really nice
    Join Date
    Jun 2010
    Location
    India
    Posts
    1,505

    Re: Python Question

    Python uses whitespace to delimit program blocks, following the off-side rule. Its uncommon block marking convention is a feature that many programmers, otherwise unfamiliar with Python, have heard of. Python borrows this feature from its predecessor ABC — instead of punctuation or keywords, it uses indentation to indicate the run of a block.

    In so-called "free-format" languages, that use the block structure derived from ALGOL, blocks of code are set off with braces ({ }) or keywords. In most coding conventions for these languages, programmers conventionally indent the code within a block, to visually set it apart from the surrounding code (prettyprinting).

    Consider a function, foo, which is passed a single parameter, x, and if the parameter is 0 will call bar and baz, otherwise it will call qux, passing x, and also call itself recursively, passing x-1 as the parameter. Here are implementations of this function in both C and Python:
    Code:
    void foo(int x)
    {
        if (x == 0) {
            bar();
            baz();
        } else {
            qux(x);
            foo(x - 1);
        }
    }
    I got the above from here
    Last edited by vv.bbcc19; 07-02-2011 at 10:27 PM.
    BCV | Community Support Representative
    █ x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  4. #4
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Re: Python Question

    Cheers guys, that should keep me quiet for while lol
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  5. #5
    vv.bbcc19's Avatar
    vv.bbcc19 is offline Community Advocate vv.bbcc19 is just really nice
    Join Date
    Jun 2010
    Location
    India
    Posts
    1,505

    Re: Python Question

    Ha ha..
    You like a programming python.."keep me quiet for a while" is a nice one.
    BCV | Community Support Representative
    █ x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  6. #6
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Python Question

    Just one little thing -- it is pretty much insane[ to use spaces rather than the tab key when editing Python. Even if you're a True Believer on the Space side of the religious war, the best option is to set your editor to convert tabs to spaces on save (and spaces to tabs on load) so that you don't accidentally create another indent level (this is especially true if you like to use proportional fonts for programming -- a good habit to get out of in Python). Python doesn't care how much indentation you use at any level (it can be a single space) but any difference at all creates a new level.

    If you're working with other programmers, find out immediately what their conventions are -- whether they're a tab, three-space, four-space or an eight-space "cult" (it helps a lot if you can turn on "show whitespace characters" in your editor) -- again, so that you aren't creating an unexpected new block level anywhere.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  7. #7
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Re: Python Question

    Am slowly getting the hang of it and am now attempting to translate what I do in PHP into Python.
    Quite begining to enjoy it, the 'sentance' structure is familiar and rapidly makes more sense as one
    learns the verb/commands peculiar to Python For example preg_match_all becomes re.findall
    on the Google Apps Engine.

    Will be a while before I can say that I've mastered it, but for know it certainly is helping to broaden horizons.
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  8. #8
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Python Question

    It is rather easy to pick up, and a big part of that is that the "Pythonic" way is usually obvious -- a big change from the TIMTOWTDI arrangement that PHP "borrowed" from Perl. Just make sure you don't get carried away (literally):

    (Image courtesy XKCD. Used in accordance with the Creative Commons Attribution/Non-Commercial version 2.5 license.)
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  9. #9
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Re: Python Question

    Beneath that 'easy to pick up' layer the hardest thing for me as a novice to assimilate is that unlike 'normal' languages where one has all the commands ready and waiting to be used if needed, in Python some have to be imported into the script before being used like regex for example. I guess it was intended that way to create a leaner/ faster server enviroment and if the Google Apps Engine is anything to go by it's impressive. Though have spent more time reading various tutorials and forums than putting code down and can honestly say the learning curve is not that steep but the path is somewhat muddy.
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  10. #10
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Python Question

    AppEngine is still at Python 2.x, right? There are a couple of pretty good books (free to download in a couple of formats) that can get you "Pythonic": Mark Pilgrim's Dive Into Python and Allen Downey's Think Python: How to Think Like a Computer Scientist.

    A less gentle, but much more thorough approach can be found in Zed Shaw's Learn Python the Hard Way. It's a three-dollar download for the ePub version, and from what I've heard from people who thought they knew what they were doing, it's worth more than the price of the dead-tree version. It hits a little harder at the functional aspects of the language than most books do.

    ("Dive Into" and "The Hard Way" are also available in a P3.x version as well if Google's updated their platform.)

    Of course, there's always the MIT OpenCourseWare "Gentle Introduction to Programming Using Python" course. It's not SICP, but it's not bad either.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. Python
    By jnieve43 in forum Free Hosting
    Replies: 1
    Last Post: 01-30-2011, 10:03 AM
  2. CGI Python
    By i14mail-forum52 in forum Scripts & 3rd Party Apps
    Replies: 8
    Last Post: 11-17-2010, 02:28 PM
  3. Replies: 0
    Last Post: 09-05-2010, 01:06 AM
  4. Python 3
    By hwkoch in forum Free Hosting
    Replies: 1
    Last Post: 01-23-2010, 01:45 PM
  5. Python
    By bryanda in forum Programming Help
    Replies: 8
    Last Post: 10-13-2009, 07:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers