Python Question

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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.
 

vv.bbcc19

Community Advocate
Community Support
Messages
1,524
Reaction score
92
Points
48
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:

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
Cheers guys, that should keep me quiet for while lol
 

vv.bbcc19

Community Advocate
Community Support
Messages
1,524
Reaction score
92
Points
48
Ha ha..
You like a programming python.."keep me quiet for a while" is a nice one.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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.
 

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
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.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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):
python.png

(Image courtesy XKCD. Used in accordance with the Creative Commons Attribution/Non-Commercial version 2.5 license.)
 

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
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.
fp0209~Monty-Python-Gumby-Man-Posters.jpg
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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.
 
Top