Re: [Tutor] Python printing parentheses and quotes
On 10Jun2019 19:04, Sai Allu wrote: Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in a module that was being imported. So the Python interpreter cached it or somehow crashed randomly, which meant that the print was working as a keyword instead of a function. But when I removed that "#! usr/bin/python" line and then rewrote the print statements, it went back to working normally. My personal suspicision is that what you might have been doing is this (notice the trailing comma): print("",) or maybe: print("this", that") Look: % python Python 2.7.16 (default, Apr 1 2019, 15:01:04) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) ('',) >>> % python3 Python 3.7.3 (default, Mar 30 2019, 03:38:02) [Clang 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) >>> What is happening? In Python 2, print is a statement unless you use the __future__ import already mentioned. That means that this: print("",) is a "print" of the expression ("",), which is a 1-tuple, and gets printed as a tuple. The more likely scenario is when you're printing mulitple things: print("this", "that") which is still a "print" of a tuple. However, in Python 3 print is a function which means that the brackets are part of the function call. So this: print("") or: print("this", "that") is a call to the "print()" function, passing one or two arguments, which get printed. And printing "" (the former case) is an empty string. Please revisit your code can test this. Subtle issues like this are why we like to receive _exact_ cut/paste of your code and the matching output, not a retype of what you thought you ran. If you encounter something weird like this, it is well worth your time (and ours) if you make a tiny standalone script showing the problem, as small as possible. Then paste it into your message and paste in the output (this list drops attachments). That we we can all run exactly the same code, and solve your actual problem. And start always using: from __future__ import print_function in Python if you're using print. That will make your prints behave the same regardless if whether they are using Python 2 or 3. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing Python
On 10/06/2019 22:20, Avi Chein wrote: > I'm trying to install Python 3.6 on my MacOS Mojave but it isn't installing > properly. When asking for help, on any forum, it's never a good idea to say that something "doesn't work" or "isn't installing properly". That gives us nothing to work on. What exactly is happening? Where did you download from? How did you try to install it? What actually happened? - Error messages? Or just a non-functioning icon or menu? Or nothing at all? The more specific the information you give us the better the chance that we can answer. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing Python
Hi, I'm trying to install Python 3.6 on my MacOS Mojave but it isn't installing properly. Can someone help me out? I'm a college student and have never used Python before. Would be much appreciated! Thans, Avi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in a module that was being imported. So the Python interpreter cached it or somehow crashed randomly, which meant that the print was working as a keyword instead of a function. But when I removed that "#! usr/bin/python" line and then rewrote the print statements, it went back to working normally. Thank you for the help though! Sai Allu From: Sai Allu Sent: Monday, June 10, 2019 11:53 AM To: Mats Wichmann; tutor@python.org; Deepak Dixit Subject: Re: [Tutor] Python printing parentheses and quotes But then how come it was working earlier for me without that import statement. Python doesn't interpret it as a statement exclusively, before it worked fine as a function. Best Wishes, Sai Allu From: Mats Wichmann Sent: Monday, June 10, 2019 11:12 AM To: Sai Allu; tutor@python.org Subject: Re: [Tutor] Python printing parentheses and quotes On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python > interpreter was changing how it interprets print statements. So I'm using > default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the > "python script.py" command. > > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") > > So it was printing parentheses and quotes. The above result might not be > exactly accurate because I didn't save the output, but it was something > generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
But then how come it was working earlier for me without that import statement. Python doesn't interpret it as a statement exclusively, before it worked fine as a function. Best Wishes, Sai Allu From: Mats Wichmann Sent: Monday, June 10, 2019 11:12 AM To: Sai Allu; tutor@python.org Subject: Re: [Tutor] Python printing parentheses and quotes On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python > interpreter was changing how it interprets print statements. So I'm using > default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the > "python script.py" command. > > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") > > So it was printing parentheses and quotes. The above result might not be > exactly accurate because I didn't save the output, but it was something > generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
On 10/06/2019 17:50, Sai Allu wrote: > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") Are you sure? Is that a cut n paste or just how you think you remember it? The reason i ask is that its not what i see and not what I'd expect. In Python v2 print is a statement which means that Python sees your first print line like: print (" Welcome to Squid Monitoring for ", "10.41.17.237") That is it thinks you want it to print a tuple of 2 strings and what I see as output is: (' Welcome to Squid Monitoring for ', '10.41.17.237') Which is a tuple of 2 strings... Now if I remove the parentheses it looks like: print " Welcome to Squid Monitoring for ", "10.41.17.237" Which is telling Python to print two strings joined by a space. And I see the output: Welcome to Squid Monitoring for 10.41.17.237 And in both cases the second print just prints out an empty string with no quotes. Are you sure that's not what you saw? > P.S. After I upgrade to Python3 this started working. In Python 3 print is a function so it needs the parentheses. Without them it will report a syntax error. So for Python 3 your original code is correct. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python > interpreter was changing how it interprets print statements. So I'm using > default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the > "python script.py" command. > > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") > > So it was printing parentheses and quotes. The above result might not be > exactly accurate because I didn't save the output, but it was something > generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python printing parentheses and quotes
Hello! I was just wondering if anybody encountered an issue where the Python interpreter was changing how it interprets print statements. So I'm using default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the "python script.py" command. Basically what happened was that I had a few lines in the script like this ip = "10.41.17.237" print(" Welcome to Squid Monitoring for ", ip) print("") and the output was like this (" Welcome to Squid Monitoring for 10.41.17.237") ("") So it was printing parentheses and quotes. The above result might not be exactly accurate because I didn't save the output, but it was something generally like that. Then I changed a few small things in the script but nothing big ("import sys", adding "#!usr/bin/env python", and accidentally trying to close the Python interpreter by using ^C multiple times). I didn't really change too much though but maybe I changed something simple that I didn't know would cause something like that. Is there any reason why Python would start to print parentheses and quotes like that. Thank you! Best Wishes, Sai Allu P.S. After I upgrade to Python3 this started working. When I kept Python2, then I was able to add an extra statement like Print("Yo") in Sublime Text and this printed Yo just like expected. But actually maybe I had Python3 before I added this print statement, I'm not too sure. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor