# New to Python

Discussion in 'Python' started by Alberto Vieira Ferreira Monteiro, Mar 12, 2007.

1. ### Alberto Vieira Ferreira MonteiroGuest

Hi, I am new to Python, how stupid can be the questions I ask?

For example, how can I add (mathematically) two tuples?
x = (1,2)
y = (3,4)
How can I get z = (1 + 3, 2 + 4) ?

Alberto Monteiro

Alberto Vieira Ferreira Monteiro, Mar 12, 2007

2. ### Bert HeymansGuest

Alberto -

List comprehesion, no doubt about it:
[3, 7]

- Bert

Bert Heymans, Mar 12, 2007

3. ### Paul RubinGuest

Well, it's a matter of how you ask them, but anyway newcomers
are welcome here.
The simplest way is explicitly:

z = (x[0]+y[0], x[1]+y[1])

There's not a really direct way to do it. Tuples aren't vectors.

Python does support complex numbers if that's what you want:

x = 1+2j # Python uses "j" for sqrt(-1)
y = 3+4j
z = x + y
print z

Paul Rubin, Mar 12, 2007
4. ### StargamingGuest

Since 1+3 is not really (only if you use rally bad approximations) 3
(neither 2+4 is 7!), i'd rather prefer using zip:
[4, 6]

What your's is doing is unpacking the contents of x first to k and p and
adding them to each other and afterwards doing the same with y's contents.

Stargaming, Mar 12, 2007
5. ### Diez B. RoggischGuest

Others have shown you ways to accomplish this, and stated correctly that
tuples aren't vectors.

You can also go and look into Numpy, the python numerical package. That
let's you do lots of operations on vectors and matrices.

Diez

Diez B. Roggisch, Mar 12, 2007
6. ### Alberto MonteiroGuest

Wow, I really didn't expect that my silly little newbie question
would get so many _different_ answers!

What is the best way to get documentation about the functions
and classes of python? I tried to google, but usually I can just
find the __doc__ of the objects, without examples or anything that
can help me use it.

Alberto Monteiro

Alberto Monteiro, Mar 12, 2007
7. ### Paulo da SilvaGuest

Alberto Vieira Ferreira Monteiro escreveu:
I think that what you want is numpy.
I don't know too much about it but a possible solution is:

import numpy
x=numpy.array([1,2])
y=numpy.array([3,4])
z=x+y

Now z is an array containing 4,6.
You may convert it to list or tuple
z=list(z)

Paulo da Silva, Mar 12, 2007
8. ### DustanGuest

Refer to this as a reference:
http://docs.python.org/

It includes a tutorial and documentation on the functions and classes
in all the global modules, as well as builtin functions (http://
docs.python.org/lib/built-in-funcs.html) and syntax. If you have prior
experience with programming, you may be able to learn python from the
tutorial, but otherwise, I would highly recommend you get a good book
for beginners on python (http://wiki.python.org/moin/PythonBooks);
there's plenty of them out there.

Dustan, Mar 12, 2007
9. ### Alberto MonteiroGuest

I guess I've been there
I do have previous experience with programming (much more than
it's reasonable for one lifetime, BTW. I have the curse of never
forgetting things I learn, so sometimes I catch myself thinking
in archaic and extinct languages), but I found python particularly
hard to learn (not as much as Haskell). Maybe I am not doing it
the right way, trying to learn Python _and_ Python GUI interface
at the same time.

For example, yesterday I wanted to open a window, draw some
image, and make it move. I tried it with tkinter and with pygame,
but I didn't succeed - there was no way I could find how to
begin the image in the center of the window (!!!).

Such basic things are usually solved with a google search, like
"python image position", but there was no inchantation I could utter
that would bring me the effect I wanted (oops. Bad example. I guess
"python image position" would give a useful answer...)

Another example, I found in pygame functions that would return
something called an "EventList". But whenever I searched for
"EventList" to see what is that, I got back to the page with
the functions that returned the EventList

Also, basic things like how does "+" operate on object "xxx"

Anyway, thanks for the help.

Alberto Monteiro

Alberto Monteiro, Mar 12, 2007
10. ### Diez B. RoggischGuest

I do have previous experience with programming (much more than
For which language they are? And in python you've got the interpreter loop,
either explicitly by invoking python on the commandline and entering some
statements, or implicitly by putting a

import pdb; pdb.set_trace()

call on a place where you are interested in toying around with objects. Then
you start poking around, and find out that a Eventlist is nothing but a
list - of events.

I think you just have to adjust to the much more dynamic nature of python a
bit.

Diez

Diez B. Roggisch, Mar 12, 2007
11. ### Dennis Lee BieberGuest

What you failed to consider (or didn't expect) is that Python
doesn't really have its "own" native GUI system. While Tkinter is
standard in the library, it is (or started as) a wrapper for TCL/TK.
wxPython is a wrapper for wxWidgets, etc.

So... To position an image one has to look at what the underlying
GUI library would use.
--
Wulfraed Dennis Lee Bieber KD6MOG

HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: )
HTTP://www.bestiaria.com/

Dennis Lee Bieber, Mar 12, 2007
12. ### Lou PecoraGuest

Refer to this as a reference:
http://docs.python.org/

It includes a tutorial and documentation on the functions and classes
in all the global modules, as well as builtin functions (http://
docs.python.org/lib/built-in-funcs.html) and syntax. If you have prior
experience with programming, you may be able to learn python from the
tutorial, but otherwise, I would highly recommend you get a good book
for beginners on python (http://wiki.python.org/moin/PythonBooks);
there's plenty of them out there.[/QUOTE]

Python in a Nutshell by Martelli (O'Reilly publ.) is very good.

I also liked Learning Python by Lutz and Ascher (O'Reilly publ.) when I
started Python, but I don't know if that's been updated recently.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.

Lou Pecora, Mar 12, 2007
13. ### Alberto MonteiroGuest

None
Ah! This is a precious help. This should be in the tutorials!!!
I know. Maybe I am trying to adjust to too many things at the same
time.

Alberto Monteiro

Alberto Monteiro, Mar 12, 2007
14. ### GoldfishGuest

Goldfish, Mar 12, 2007
15. ### Gabriel GenellinaGuest

En Mon, 12 Mar 2007 10:25:15 -0300, Alberto Monteiro
That's the problem: you have to search on how to do that using "pygame",
or "TkInter" (or Tk actually), or wxWidgets, or GTK, or whatever framework
you choose to make the GUI. Python itself is not bound to a single
framework so "how to do this in Python" has no sense, or has many answers
depending on the chosen framework.
You can always check yourself if in doubt. At the interpreter prompt, use
any of these to inspect any object:

repr(xxx) # a string representation of object xxx
type(xxx) # the actual type of xxx
dir(xxx) # list of attributes, including methods
vars(xxx) # name and value for all instance attributes, if any
help()
help(xxx)
help(modules)
help('keyword')

py> b
py> repr(b)
py> type(b)
<class 'webbrowser.WindowsDefault'>
py> dir(b)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash_
_', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr_
_', '__setattr__', '__str__', '__weakref__', 'args', 'basename', 'name',
'open',
'open_new', 'open_new_tab']
py> vars(b)
{'basename': '', 'name': ''}
py> help(b)
Help on WindowsDefault in module webbrowser object:

class WindowsDefault(BaseBrowser)
| Method resolution order:
| WindowsDefault
| BaseBrowser
| __builtin__.object
|
| Methods defined here:
|
| open(self, url, new=0, autoraise=1)
(...)
Usually you find that on the documentation for "xxx", either online
http://docs.python.org/lib/lib.html or using help(xxx)

Gabriel Genellina, Mar 12, 2007
16. ### NanjundiGuest

To put the correct form of the soulution here.
This will give the desired output z = [k+p for k,p in zip(x, y)]
Or z = [k+p for k,p in map(None, x, y)][4, 6]

-N

Nanjundi, Mar 12, 2007