This question already has answers here:
Getting SyntaxError for print with keyword argument end=' '
(17 answers)
Closed 5 years ago.
In python 3, I'm tying to print a line without skipping a line as:
print('hello', end='')
And I'm getting this error message:
print('hello', end='')
^
SyntaxError: invalid syntax
What's going on? How do I correct this error?
Are you absolutely sure you're using Python 3, and not accidentally invoking Python 2?
~ $ python3
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
hello>>>
~ $ python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
File "<stdin>", line 1
print('hello', end='')
^
SyntaxError: invalid syntax
>>>
Related
print("hello", end="")
output:
[Running] python -u "/home/a/code/projects/project1.py"
File "/home/a/code/projects/project1.py", line 1
print("hello", end="")
^
SyntaxError: invalid syntax
settings.json
{
"python.linting.pylintEnabled": false,
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pydocstyleEnabled": false,
"python.pythonPath": "/usr/bin/python3.6"
}
been looking up about why code is being run on older version of python in vscode... can't figure it out.
That's because you're using Python 2:
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
File "<stdin>", line 1
print("hello", end="")
^
SyntaxError: invalid syntax
In Python3 it wouldn't happen:
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
hello>>>
This question already has answers here:
Math domain error in python when using log
(2 answers)
Closed 5 years ago.
when I run this code:
max(MIN_LEARNING_RATE, min(0.5, 1.0 - math.log10((t+1)/25)))
with t = 0
I've this error:
ValueError: math domain error
but if i run the same code with python 3.6 the error disappeared
It's because in Python 2, the division returns a floor of the float, and in the case where it's 1/25, it returns 0.
And math.log(0) gives a domain error.
So in your Python 2 code, add this at the start of the script:
from __future__ import division
By default in Python 2:
$ python2
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[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.
>>> import math
>>> math.log(1/25)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> 1/25
0
In Python 3:
$ python3
Python 3.6.1 (default, Apr 4 2017, 09:40:21)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(1/25)
-3.2188758248682006
>>> 1/25
0.04
In Python 2 with __future__.division:
$ python2
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[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.
>>> from __future__ import division
>>> import math
>>> math.log(1/25)
-3.2188758248682006
>>> 1/25
0.04
Due to rounding in python-2.x, (0+1)/25 evaluates to 1/25, which rounds to 0. Thus, math.log10(0), which is undefined, results in a ValueError.
Either add from __future__ import division to the first line in your program or change the line of code to max(MIN_LEARNING_RATE, min(0.5, 1.0 - math.log10((t+1)/25.0)))
Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).
I'm python beginner.
I am wondering what the return value of the 'print' function is.
I tried type(print(3)) and didn't work.
I tried to find the api document but I could only find the pprint function.
print() returns None in Python3.
$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
3
<class 'NoneType'>
In Python2 print is a statement, so doesn't return anything...
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
File "<stdin>", line 1
type(print(3))
^
SyntaxError: invalid syntax
...unless you use from __future__ import print_function
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> type(print(3))
3
<type 'NoneType'>
Using the code print('{0} is not'.format('That that is not')) in Python 3.1.1, I get the following error:
AttributeError: 'str' object has no attribute 'format'
when I delete the line Netbeans automatically inserted at the beginning:
from distutils.command.bdist_dumb import format
which itself causes an error of
ImportError: cannot import name format
What am I doing wrong here?
You must be running an older version of Python. This does work in Python 3.1.1+:
$ python3
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
'That that is not is not'
You will, however, get this error in Python 2.5.4:
$ python2.5
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'
This feature seems to have been backported to Python 2.6, so you won't get this error there. You must be running Python < 2.6.