I don't understand this syntax error in python [duplicate] - python

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 2 months ago.
print list(prolog.query(code))
^
SyntaxError: invalid syntax
can anyone tell me what does this syntax error mean?
I tried to install but it doesn't work.

check which python version you are using.
The code shown in question is valid until python 2.7, but not in Python 3 onward.
valid for Python 3:
print(list(prolog.query(code)))
(note the additional parenthesis like any function for print).
And as general advice, as noticed in comments, a syntax error marked at a line could be confusing and caused by a problem on the line before. Comment the line where you have the error and check.

This code is not valid in python 3.x
But this is valid in 2.x
Change the syntax to print(list(prolog.query(code)) (recommended)
or, Change your python version to 2.x (Not recommended)
Check this article.

Related

Python: Converting python2 to python3 [duplicate]

This question already has answers here:
Difference between int() and long()
(1 answer)
What are metaclasses in Python?
(25 answers)
Closed 1 year ago.
I am trying to use
https://github.com/iandees/mongosm/blob/master/insert_osm_data.py
this package. It seems like it is written in Python2. I have converted all the way to next(context). However, I am getting name 'long' is not defined.
Is there any way that I can define this somewhere? How can I define 'long' and I have no idea what this is for even for Python2 Script (which worked fine somehow).
long() is basically renamend to int() in Python 3.
Please see https://www.python.org/dev/peps/pep-0237/ for details.
So, either do a search of long and replace with int, or define it
long = int
somewhere at the beginning of your file.
You should convert all code to Python3
https://www.google.com/search?channel=fs&client=ubuntu&q=Converting+python2+to+python3
https://docs.python.org/3/library/2to3.html

Invalid syntax error when using f-string in Python 3.6 [duplicate]

This question already has answers here:
Invalid Syntax when F' string dictionary [duplicate]
(3 answers)
Closed 2 years ago.
I've run into this weird error and I'm wondering whether anyone has a clue what's going on.
I'm trying to print the minimal date from a date column in Pandas series. However, the following is raising an invalid syntax error:
print(f'{df_raw['POSTING_DATE'].min()}')
This does work, though:
min_date = df_raw['POSTING_DATE'].min()
print(f'{min_date}')
Using .format() also works. Obviously I can use a workaround here but I was just wondering why the f-string syntax doesn't work in this case. I thought the f-strings should be able to handle similar expressions.
I'm using Python 3.6.9.
Use " as:
print(f"{df_raw['POSTING_DATE'].min()}")
Update:
Ideally, we could use \ to escape quotes but f-strings does not support using \ in it so this wouldn't work with f-strings
print(f'{df_raw[\'POSTING_DATE\'].min()}')

Strange syntax and error when executing lambda function with multiple arguments [duplicate]

This question already has answers here:
f-strings giving SyntaxError?
(7 answers)
Closed 3 years ago.
I'm using this tutorial to learn about lambda functions:
How to Use Python lambda Functions
There is an example involving this line:
full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'
I have 2 questions:
There is an "f" in front of "Full name". What does this "f" do?
When I run this line, I immediately get this error:
File "<stdin>", line 1
full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'
^
SyntaxError: invalid syntax
Why does this happen? Why did the tutorial show a properly executed function, but I get an error?
So firstly this f is a new, elegant way to format a string with variables. I invite you to read https://realpython.com/python-f-strings/ that tells the whole story.
However, this exists only since Python 3.6. Can you confirm your version?
python --version
Kind regards -

why am I getting a syntax error in python 2.7.14 [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 4 years ago.
coord = {'latitude' : '37.24N' , 'longitude' : '-115.81'}
print 'coordinates: {latitude}, {longitude}' .format(**coord)
I've been recently following this new program on learning python and I've come across an error that says SyntaxError: invalid syntax,but I can't spot any syntax errors, if anyone can offer help it would be much appreciated.
I'm guessing that you are getting a syntax error related to the print statement if you are using the wrong version of Python. Make sure you are using Python 2.x, otherwise upgrade your code to be compatible with Python 3.x.
python --version should tell you which version you are using
Python 3.x version of your code. Notice the round brackets used with the print statement.
coord = {'latitude' : '37.24N' , 'longitude' : '-115.81'}
print('coordinates: {latitude}, {longitude}'.format(**coord))

Why print(a++) has syntax errors in python, but not print(++a) [duplicate]

This question already has an answer here:
Preincrement operators in python
(1 answer)
Closed 5 years ago.
I am a beginner in python and I am using Python 3.5. The python console complains invalid syntax for the below statement:
a = 5
print(a++)
But print(++a) works fine. Can anyone help me understand the difference?
Btw, it seems that print(a+=1) also doesn't work.
Thanks!
++a is just the same as doing (+(+a)). I.E: You're using the mathematical addition operator on the variable a (with implied zeroes). So the result is a
a++ is not valid python syntax (unlike other languages).
a += 1 is an assignment. It is equivalent to a = a + 1 - you can not print an assignment

Categories