Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does this work in c but not in python: (a = 5) == 5

Tags:

python

c

New to both python, and fairly beginner in C. Why will the subject code return an error in python?

Does the assignment not have a return value?

like image 883
blythe10 Avatar asked Jun 01 '26 09:06

blythe10


2 Answers

This is simply not valid in Python. You can't use an assignment as an expression.

like image 161
Simeon Visser Avatar answered Jun 03 '26 00:06

Simeon Visser


I am going to expand on Simeon's answer.

First off, you are putting parenthesis around an expression. If you type that into the interpreter by itself you will get a syntax error. That is because parenthesis are only defined/interpreted in context - there is no context for Python when you are using an expression within the parenthesis.

If you type (a-5) then it will attempt to resolve the expression and tell you that a is not defined. I hope this helps.

like image 26
PyNEwbie Avatar answered Jun 02 '26 23:06

PyNEwbie