New to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alberto Vieira Ferreira Monteiro

    #1

    New to Python

    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
  • Bert Heymans

    #2
    Re: New to Python

    On Mar 12, 3:02 am, Alberto Vieira Ferreira Monteiro
    <albm...@centro in.com.brwrote:
    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

    Comment

    • Paul Rubin

      #3
      Re: New to Python

      Alberto Vieira Ferreira Monteiro <albmont@centro in.com.brwrites :
      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

      Comment

      • Stargaming

        #4
        Re: New to Python

        Bert Heymans schrieb:
        On Mar 12, 3:02 am, Alberto Vieira Ferreira Monteiro
        <albm...@centro in.com.brwrote:
        >
        >>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.

        Comment

        • Diez B. Roggisch

          #5
          Re: New to Python

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

          Comment

          • Alberto Monteiro

            #6
            Re: New to Python

            I wrote:
            >
            >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) ?
            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

            Comment

            • Paulo da Silva

              #7
              Re: New to Python

              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)

              Comment

              • Dustan

                #8
                Re: New to Python

                On Mar 12, 5:07 am, "Alberto Monteiro" <albm...@centro in.com.br>
                wrote:
                I wrote:
                >
                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) ?
                >
                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:


                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.

                Comment

                • Alberto Monteiro

                  #9
                  Re: New to Python

                  Dustan wrote:
                  >
                  >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.
                  >
                  Refer to this as a reference:

                  >
                  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


                  Comment

                  • Diez B. Roggisch

                    #10
                    Re: New to Python

                    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

                    Comment

                    • Lou Pecora

                      #11
                      Re: New to Python

                      In article <1173704503.459 048.310560@t69g 2000cwt.googleg roups.com>,
                      "Dustan" <DustanGroups@g mail.comwrote:
                      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:

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

                      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.

                      Comment

                      • Alberto Monteiro

                        #12
                        Re: New to Python

                        Diez B. Roggisch wrote:
                        >
                        >Also, basic things like how does "+" operate on object "xxx"
                        >are impossible to google search.
                        >
                        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

                        Comment

                        • Goldfish

                          #13
                          Re: New to Python

                          I read http://diveintopython.org/ online, and covered a lot of Python
                          territory nicely with this.

                          Greg

                          Comment

                          • Gabriel Genellina

                            #14
                            Re: New to Python

                            En Mon, 12 Mar 2007 10:25:15 -0300, Alberto Monteiro
                            <albmont@centro in.com.brescrib ió:
                            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')

                            pyb
                            <webbrowser.Win dowsDefault object at 0x00ADBEB0>
                            pyrepr(b)
                            '<webbrowser.Wi ndowsDefault object at 0x00ADBEB0>'
                            pytype(b)
                            <class 'webbrowser.Win dowsDefault'>
                            pydir(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']
                            pyvars(b)
                            {'basename': '', 'name': ''}
                            pyhelp(b)
                            Help on WindowsDefault in module webbrowser object:

                            class WindowsDefault( BaseBrowser)
                            | Method resolution order:
                            | WindowsDefault
                            | BaseBrowser
                            | __builtin__.obj ect
                            |
                            | 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)

                            --
                            Gabriel Genellina

                            Comment

                            • Nanjundi

                              #15
                              Re: New to Python

                              On Mar 12, 4:49 am, "Bert Heymans" <bert.heym...@g mail.comwrote:
                              On Mar 12, 3:02 am, Alberto Vieira Ferreira Monteiro
                              >
                              <albm...@centro in.com.brwrote:
                              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)]
                              >>z
                              [4, 6]

                              -N

                              Comment

                              Working...