473,750 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python newbie

I am a Python newbie who decided to see what that Python fuss is all about.
Quite frankly, I am a bit perplexed. After having had few months of
experience with Perl (started in 1994 with Perl v4, and doing it ever
since) , here is what perplexes me:

perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'

The equivalent in Python looks like this:

Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>a=[1,2,3]
map((lambda x: 2*x),a)
[2, 4, 6]
>>map((print),a )
File "<stdin>", line 1
map((print),a)
^
SyntaxError: invalid syntax
>>for x in a: print x
....
1
2
3
>>for x in a: x=2*x
....
>>for x in a: print x
....
1
2
3
>>>
There are several questions:

1) Why is the array "a" unchanged after undergoing a transformation with
map?
2) Why is it illegal to pass a built-in function "print" to map?
3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?
4) Is there an equivalent to \$a (Perl "reference" ) which would allow me to
decide when a variable is used by value and when by reference?

PHP also allows changing arrays with "foreach" loop:
#!/usr/local/bin/php
<?php
$a=array(1,2,3) ;
foreach($a as &$x) { $x=$x*2; }
array_walk($a,c reate_function( '$a','print("$a \n"); '));
?>

How can I make sure that
for x in a: x=2*x

actually changes the elements of the array "a"?
http://mgogala.freehostia.com
Sep 19 '08 #1
10 1409
Mladen Gogala wrote:
I am a Python newbie who decided to see what that Python fuss is all about.
Quite frankly, I am a bit perplexed. After having had few months of
experience with Perl (started in 1994 with Perl v4, and doing it ever
since) , here is what perplexes me:

perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'

The equivalent in Python looks like this:

Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>>a=[1,2,3]
map((lambd a x: 2*x),a)
[2, 4, 6]
>>>map((print), a)
File "<stdin>", line 1
map((print),a)
^
SyntaxError: invalid syntax
>>>for x in a: print x
...
1
2
3
>>>for x in a: x=2*x
...
>>>for x in a: print x
...
1
2
3

There are several questions:

1) Why is the array "a" unchanged after undergoing a transformation with
map?
Because you evaluated an expression in which a was a variable, giving an
entirely new object as a result.
2) Why is it illegal to pass a built-in function "print" to map?
Because at present "print" isn't a built-in function, it's a keyword
designating a specific statement type. Try using sys.stdout.writ e instead.
3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?
Because you aren't transforming a. You are extracting references to a's
elements into a separate variable and rebinding that variable to a new
value, leaving the references in a's elements pointing to the original
objects.
4) Is there an equivalent to \$a (Perl "reference" ) which would allow me to
decide when a variable is used by value and when by reference?
No. Python implicitly dereferences all names when using them to compute
values, and only uses them as references on the left-hand side of an
assignment.

Please note the above statement is contentious, and will likely bring a
horde of screaming fanatics of various flavors down on my head for
terminological inexactitude.
PHP also allows changing arrays with "foreach" loop:
#!/usr/local/bin/php
<?php
$a=array(1,2,3) ;
foreach($a as &$x) { $x=$x*2; }
array_walk($a,c reate_function( '$a','print("$a \n"); '));
?>

How can I make sure that
for x in a: x=2*x

actually changes the elements of the array "a"?
By saying something like

a = [2*x for x in a]

You don't modify the individual elements, you create a new list and
rebind a to that. AIf you insist on changing the elements of a, a more
cumbersome alternative is

for i, x in enumerate(a):
a[i] = 2+x

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 19 '08 #2
On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
There are several questions:

1) Why is the array "a" unchanged after undergoing a transformation with
map?
Because `map()` creates a new list and doesn't change the elements in `a`.
2) Why is it illegal to pass a built-in function "print" to map?
Because ``print`` isn't a function but a keyword. This will be changed
in PythonÂ*3.0. On the other hand this use case is discouraged because
`map()` builds a new list with the return value of the given function.
So you would build a list full of `None`\s just for the side effect.
3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?
Because there is no explicit transformation. The loop binds elements to
the name `x` in your example and within the loop you rebind that name to
another value. Neither the name `x` nor the objects bound to it have any
idea that the object might be referenced in some container object.
4) Is there an equivalent to \$a (Perl "reference" ) which would allow me
to decide when a variable is used by value and when by reference?
No, variables in Python are always name to object bindings. Don't think
of variables as boxes with names on it where you put objects in, or
references to another box. Think of objects and sticky notes with names
on it.
How can I make sure that
for x in a: x=2*x

actually changes the elements of the array "a"?
Well, actually change the elements. ;-)

Either:

for index, item in enumerate(seque nce):
sequence[index] = func(item)

Or:

sequence[:] = map(func, sequence)

But the need to do so doesn't seem to be that frequent. Usually one just
builds a new list instead of altering an existing one.

Ciao,
Marc 'BlackJack' Rintsch
Sep 19 '08 #3
On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
I am a Python newbie who decided to see what that Python fuss is all
about. Quite frankly, I am a bit perplexed.
Naturally you will be perplexed if you assume that Python is just Perl
with a more verbose syntax. It isn't.

After having had few months
of experience with Perl (started in 1994 with Perl v4, and doing it ever
since) , here is what perplexes me:

perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'

The equivalent in Python looks like this:
Actually, no it doesn't. The equivalent looks more like this:
$ python -c 'a=[1,2,3]; a=[2*x for x in a];
for x in a:
print x'
2
4
6

Now, to go on with your questions:
1) Why is the array "a" unchanged after undergoing a transformation with
map?

Because it hasn't undergone a transformation at all. The list (not array)
a remains unchanged, because map returns a new list.
2) Why is it illegal to pass a built-in function "print" to map?
Because print is not a function, it is a statement. However, in Python 3,
that design wart has been corrected.

3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?

Because the list a hasn't undergone any such transformation. Rebinding a
name is not an explicit transformation to the object that used to be
bound to the name. And if that sentence is gobbledygook to you, that
demonstrates that Python is not Perl and you shouldn't assume a one-to-
one semantic relationship between syntactical elements.
4) Is there an equivalent to \$a (Perl "reference" ) which would allow me
to decide when a variable is used by value and when by reference?
Python is neither call-by-reference nor call-by-value, although you will
have many people swear black and blue that it is one or the other. With
respect to all of them, they are wrong. Python's calling model is "call
by object". See here for more detail:

http://effbot.org/zone/call-by-object.htm
If you insist on a false dichotomy between call-by-reference and call-by-
value, then you won't often go wrong if you think of Python:

- being almost always call-by-reference, except for some odd corner cases
which look almost, but not quite, like call-by-value;

- being almost always call-by-value, but only if you think of the values
as pointers.

How can I make sure that
for x in a: x=2*x

actually changes the elements of the array "a"?
By explicitly changing the elements of the list. Here's one very old-
fashioned way of doing so:
>>a = [1,2,3]
for i in range(len(a)):
.... a[i] = a[i]*2
....
>>a
[2, 4, 6]
Here's a more Pythonic alternative:
>>a = [1,2,3]
a = [2*x for x in a]

--
Steven
Sep 19 '08 #4
On Fri, 19 Sep 2008 03:34:50 -0400, Steve Holden wrote:
Please note the above statement is contentious, and will likely bring a
horde of screaming fanatics of various flavors down on my head for
terminological inexactitude.
A pox on you and your descendants onto the tenth generation!
*wink*
--
Steven
Sep 19 '08 #5
Steve Holden wrote:
No. Python implicitly dereferences all names when using them to compute
values, and only uses them as references on the left-hand side of an
assignment.

Please note the above statement is contentious, and will likely bring a
horde of screaming fanatics of various flavors down on my head for
terminological inexactitude.
Actually, I am not a "screaming fanatic". I just want to learn. I am not
trying to compare Perl or PHP with Python. I don't know enough Python to
do that. I see an exponential increase in the use of Python and I want to
find out why is that. I don't plan on discarding my knowledge of Perl and
PHP and I don't know enough Python to make a reasonable comparison. whether
I will use Python on regular basis or not is still to be decided. One thing
is certain: it's easier to use what I already know but then, that's not the
point. I've heard very good things about Django and TurboGears and I got
my hands on the tool called orasachemadoc which is written in Python and
I want to check it out. True, I am coming from the "dark side", but I have
no preconceptions and am not a "screaming fanatic". Let me introduce myself:

http://www.dba-oracle.com/t_dbi_interface1.htm
http://www.rampant-books.com/book_2005_2_php_oracle.htm
http://www.oracle.com/technology/pub...a-symfony.html

Those articles are the only ones that are scripting related. There are some
other articles about Oracle, of no interest for this group. I am 47 years
old and I have learned many programming languages in my time. Learning
Python doesn't seem like a huge task. Conquering my own preconceptions
stemming from a very long use of other programming languages will not be a
big problem, either.

Hopefully, this explains who am I and what is my motivation for delving into
Python. I share your fears of screaming fanatics, too.

--
http://mgogala.freehostia.com
Sep 19 '08 #6
Mladen Gogala wrote:
Steve Holden wrote:
>No. Python implicitly dereferences all names when using them to compute
values, and only uses them as references on the left-hand side of an
assignment.

Please note the above statement is contentious, and will likely bring a
horde of screaming fanatics of various flavors down on my head for
terminologic al inexactitude.

Actually, I am not a "screaming fanatic". I just want to learn.
I'm sure Steve can speak for himself, but just to clarify:
the "screaming fanatics" referred to are those within the
Python community whose opinions of the terminology used are
at variance with Steve's. I think it was a comment which would
mean more to those of us who've seen such terminology-battles
come and go over the years.

But thanks for introducing yourself anyway. Knowing more about
where a poster is coming from never hurts when trying to answer
their questions. :)

TJG
Sep 19 '08 #7
On 19 sep, 10:13, Steven D'Aprano
<ste...@REMOVE. THIS.cybersourc e.com.auwrote:
On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
I am a Python newbie who decided to see what that Python fuss is all
about. Quite frankly, I am a bit perplexed., here is what perplexes me:
perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'
>
Naturally you will be perplexed if you assume that Python is just Perl
with a more verbose syntax. It isn't.
Indeed, it isn't ... at all:
$ python -c 'for s in [2*x for x in (1, 2, 3)]: print s'
2
4
6

And it's always (a lot) more readable :-)

kb
Sep 19 '08 #8
On 2008-09-19, Mladen Gogala <go***********@ gmail.comwrote:
Steve Holden wrote:
>No. Python implicitly dereferences all names when using them
to compute values, and only uses them as references on the
left-hand side of an assignment.

Please note the above statement is contentious, and will
likely bring a horde of screaming fanatics of various flavors
down on my head for terminological inexactitude.

Actually, I am not a "screaming fanatic".
Steve was refering hyperbolically to people like me who find it
entertaining to argue about obscure technical points involving
useless, degenerate bits of example code. It's one of the
things we do here instead of Perl golfing. Though off hand I
don't really see anything much wrong with the statement for
which Steve predicted an impending onslaught of language
lawyers -- unless one wanted to jump right away into the stock
discussion on name-rebinding vs. dereferencing-pointer-lvalues
that we generally save for C/C++ programmers. I only used Perl
once about 15 years ago (that was more than enough for me), but
I would have guessed that an assigment in Perl was a
name-(re)binding more akin to Python than a store to a
variable's memory location like C/C++/Pascal/etc.
Hopefully, this explains who am I and what is my motivation
for delving into Python. I share your fears of screaming
fanatics, too.
We're actually quite harmless and much friendlier than those in
most other newsgroups.

--
Grant Edwards grante Yow! My EARS are GONE!!
at
visi.com
Sep 19 '08 #9
On 19 Sep 2008 08:13:05 GMT, Steven D'Aprano wrote:
On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
[snip]
>perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'

The equivalent in Python looks like this:

Actually, no it doesn't. The equivalent looks more like this:
$ python -c 'a=[1,2,3]; a=[2*x for x in a];
for x in a:
print x'
2
4
6
Good job, SD'A.

A note for Mladen Gogala: the [...for...] construction in

a=[2*x for x in a];

is called a list comprehension, and is wonderful for shortening
and clarifying code. One of the pleasures of following this
group is watching the old hands answer noob questions with one
or two list comprehensions.

--
To email me, substitute nowhere->spamcop, invalid->net.
Sep 19 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2610
by: post400 | last post by:
Hi , I was just wondering ( yeah I know it's not the first time this question pops up ) what would be the best 2 or 3 books for someone who wants to learn Python , already experienced in other non-OOP languages .It takes time to browse endlessly on the net , in a bookshop or a library for THOSE books that are really useful ! Thanks ,
23
1908
by: Simon Wittber | last post by:
For the first time, I have been bitten by Python. The below code produces the results: False True when I initially expected the results: False False It took me a while to work out that default keyword argument values
114
9870
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
16
1882
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy in c++/java !? Tnx, Raxit
30
2725
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For example, if I had: ------- 1 2 3 4 5 6
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.