[BangPypers] Beginner Help

Hrishikesh Kulkarni rishi at turtleyogi.com
Tue Apr 2 11:41:09 CEST 2013


>> # The Problem is:
>> When "w" is not defined how python interpreter can evaluate the output??

When you say "defined" you are comparing it to a programming language you
have already know. I am guessing C or C++.

C/C++ supports the concept of variable declaration and definition. Variable
name is actually attaching a label or tag to a memory location where the
real value is stored (for simplicity ignore pointers for now).

in C, the pseudo code would have been:

for (int i = 0; i< numberofwords ; i++)
{
  printf("%s", words[i]);
}

here "i" is the index in the array "words", which means you could use "i"
to step into the memory owned by array "words".

In Python, the same thing is in play except that python allocates the label
or tag on the go. "w" is internally pointing to words[index]. The index is
auto generated. The "for" is auto incrementing index for you. "for" also
ensures "w" is available within its scope.

>>> for w in words:
...     print w, len(w)


Quick analogy:

Variable names in C/C++ vs Python is like sticking post-its along the
memory location (in RAM).  C/C++ makes you buy the post-its and makes you
figure out where to stick the post-its and even lets your make mistakes.
Python buys it for you and sticks it for you. You just have to pick the
color of the post-it and remember it.


Suggestion:
Another reason why it is so important to understand pointers C. Once you
get it, you can always figure out the internals of other programming
languages. (especially those which are built on top of C)

regards,
Rishi



On Tue, Apr 2, 2013 at 2:38 PM, Gaurav Malhotra <gaurav.tula at gmail.com>wrote:

> Hi everyone,
>
> Below is the code (with output) which can be found in the Python Official
> Documentation:
>
> >>> # Measure some strings:
>
> ... words = ['cat', 'window', 'defenestrate']
>
> >>> for w in words:
>
> ...     print w, len(w)
>
> ...
>
> cat 3
>
> window 6
>
> defenestrate 12
>
> # The Problem is:
> When "w" is not defined how python interpreter can evaluate the output??
>
> Please Explain the lines where "w" is occurred. if i would replace "w" with
> any other variable, it will show the same output.
>
> I want to know "What is happening in the back (i mean how interpreter is
> handling it)" ??
>
> Thanks & Regards,
> Gaurav Malhotra
>
> --
> *Regards
> Gaurav Malhotra
> +91-9410562301*
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list