#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    New to python- Python prints parentheses


    Hi, I'm new to the community and new to programming I might add. I've been self teaching myself python as my first language, completely from online sources. I feel like every problem I've ironed out with enough perseverance but I'm at a loss and this is holding me up.

    When I define a function:

    Code:
    def printMax(a,b):    
       if a > b:         
          print (a, " is the max")
       elif a == b:         
          print (a, " is equal to", b)     
       else:         
          print (b, " is the max")
    
    printMax(3,4)
    Results in

    Code:
    (4, "is the max")
    This is driving me insane. I want it to simply print:

    Code:
    4 is the max
    Is there a graceful way to do this without converting a and b to strings? I'm pretty sure python is printing a tuple. Though I am not very versed on this subject, the book I am reading from, "A byte of python", format the code this way precisely but claims the result I want. What am I doing wrong?

    Again I am new to this. Any direction would be greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    It seems that you are trying to use python3 syntax with a python2 interpreter.

    Code:
    Python2:
    >>> print a, 'is the max'
    1 is the max
    
    Python 3:
    >>> print("%d is the max" % 1)
    1 is the max
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    He is right


    It seems you are using Python 2.something, so like the other guy/gal said just remove the brackets around the print statements and your all good.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by Romana
    I want it to simply print:
    Code:
    4 is the max
    Hi,

    You can try this (Python 2)

    Code:
    def printMax(a,b):    
       if a > b:         
          print (str(a) + " is the max")
       elif a == b:         
          print (str(a) + " is equal to", b)     
       else:         
          print (str(b) + " is the max")
    
    printMax(3,4)
    Originally Posted by Romana
    Is there a graceful way to do this without converting a and b to strings?
    According to what we read in the online documentation for the function print() I would say: No
    http://docs.python.org/release/3.2.3/library/functions.html#print

    print(*objects, sep=' ', end='\n', file=sys.stdout)

    Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.

    All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

    The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.
    Regards,
    Dariyoosh
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Yeah, this is definitely a Python 2 vs Python 3 problem. It's probably a good idea to familiarize yourself with the differences between them. If you are teaching yourself using online sources, plural, you're likely to encounter some examples from Python 2 and some from Python 3.

    If you prefer the Python 3 behaviour of the print function (i.e. that it is a function), it can be enabled in Python 2.6 or 2.7 by putting this at the top of your file:
    Code:
    from __future__ import print_function
    (Or you could just use Python 3, of course.)
    Last edited by Nyktos; April 3rd, 2013 at 06:54 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Ah, thank you all! I will have to figure out how to change which interpreter komodo edit is running my code with. I'm running OSX which, if I'm not mistaken, comes with 2.7 and I've installed 3.3 which is the version the book I'm following uses.

    Should I be learning 2.7 or 3.3? I'm learning python to gain an understanding of programming but of course I'd still like to frequently utilize it. That being said, I've taken a look at Nkytos' link and found it too technically dense- I have the feeling I don't have enough knowledge of python to make sense of it one way or the other. Am I right in thinking that I should choose one version and stick with it until I have a compete enough understanding to differentiate the two? Or is it common to learn both concurrently?

    Python 3.3 is backward compatible with 2.7, no? Does this mean if I learn python 3 I'll be able to use libraries written in 2.7 in my 3.3 code? If that is so then it's a no-brainer to start with 3.3.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0
    What I've been told is to learn 2.7 with the knowledge that something's are different in 3. Like print being a function and input taking over for raw_input. I've been told once I learn 2 then go to 3 because 3 is the future. Maybe some of the pro's have a better take than me , but this is what I've been told so far. I hope it helps
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by Romana
    Should I be learning 2.7 or 3.3? I'm learning python to gain an understanding of programming but of course I'd still like to frequently utilize it. That being said, I've taken a look at Nkytos' link and found it too technically dense- I have the feeling I don't have enough knowledge of python to make sense of it one way or the other. Am I right in thinking that I should choose one version and stick with it until I have a compete enough understanding to differentiate the two? Or is it common to learn both concurrently?
    Pick either version; if you want to be a "real Python programmer" right now you're going to have to become comfortable with both at some point. I think you're quite right that it's better to stick with learning one until you're fairly comfortable with it. If you like the book you're using, stick with the version it uses for the moment.

    Originally Posted by Romana
    Python 3.3 is backward compatible with 2.7, no? Does this mean if I learn python 3 I'll be able to use libraries written in 2.7 in my 3.3 code? If that is so then it's a no-brainer to start with 3.3.
    No, unfortunately, that's where all the trouble is. Python 3 broke compatibility in a number of ways, some big and some small. There's a reasonably subset of the language that is compatible with both (especially if __future__ statements are used to enable some Python 3 features in Python 2), but there are still some differences that require major changes from one to the other. Most libraries are compatible with Python 3 now, but there are still a lot that aren't.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0
    Hi Nyktos,

    So I hear this talk of libraries all the time. What exactly is a library in python? Is it referring to a module?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    "Library" isn't a Python-specific term, it just refers to a piece of software which is distributed with the goal of being used in other pieces of software rather than doing anything on its own. In Python specifically, it'll be a module or a collection of modules.

IMN logo majestic logo threadwatch logo seochat tools logo