New to Python

  • Thread starter Alberto Vieira Ferreira Monteiro
  • Start date
A

Alberto Vieira Ferreira Monteiro

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
 
P

Paul Rubin

Alberto Vieira Ferreira Monteiro said:
Hi, I am new to Python, how stupid can be the questions I ask?

Well, it's a matter of how you ask them, but anyway newcomers
are welcome here.
For example, how can I add (mathematically) two tuples?
x = (1,2)
y = (3,4)
How can I get z = (1 + 3, 2 + 4) ?

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
 
S

Stargaming

Bert said:
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 -

List comprehesion, no doubt about it:
z = [k+p for k,p in (x, y)]
z

[3, 7]

- Bert

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:
>>> x = (1,2)
>>> y = (3,4)
>>> [k+p for k,p in (x,y)] # wrong one [3, 7]
>>> [k+p for k,p in zip(x,y)] # zip-version
[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.
 
D

Diez B. Roggisch

Alberto said:
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) ?

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
 
A

Alberto Monteiro

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
 
P

Paulo da Silva

Alberto Vieira Ferreira Monteiro escreveu:
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

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)
 
D

Dustan

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

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.
 
A

Alberto Monteiro

Dustan said:
Refer to this as a reference:
http://docs.python.org/
I guess I've been there :)
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.
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"
are impossible to google search.

Anyway, thanks for the help.

Alberto Monteiro
 
D

Diez B. Roggisch

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"
are impossible to google search.

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
 
D

Dennis Lee Bieber

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...)
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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Lou Pecora

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

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.
 
A

Alberto Monteiro

Diez said:
For which language they are?
None :)
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.

Ah! This is a precious help. This should be in the tutorials!!!
I think you just have to adjust to the much more dynamic nature of
python a bit.
I know. Maybe I am trying to adjust to too many things at the same
time.

Alberto Monteiro
 
G

Gabriel Genellina

En Mon, 12 Mar 2007 10:25:15 -0300, Alberto Monteiro
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...)

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.
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 :)

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
<webbrowser.WindowsDefault object at 0x00ADBEB0>
py> repr(b)
'<webbrowser.WindowsDefault object at 0x00ADBEB0>'
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)
(...)
Also, basic things like how does "+" operate on object "xxx"
are impossible to google search.

Usually you find that on the documentation for "xxx", either online
http://docs.python.org/lib/lib.html or using help(xxx)
 
N

Nanjundi

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 -

List comprehesion, no doubt about it:>>> z = [k+p for k,p in (x, y)]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top