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.
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 ;)
You don't use { brackes } to group statements in Python , you use indentation.
Using brackets:
Using indentation to create blocks:Code:if( x > 1 ){ 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.Code:if x > 0 : x = x + 14 foobar(x) else: x = x + 1 blarg(x) y = 4
Nothing is always absolutely so.
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:
I got the above from hereCode:void foo(int x) { if (x == 0) { bar(); baz(); } else { qux(x); foo(x - 1); } }
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
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 ;)
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
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)
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 ;)
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)
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 ;)
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)