Can’t assign to conditional expression syntax error
The syntax for a conditional expression is A if C else B , which is weird and different from your usual if statement. So the correct way to write this is not to repeat the assignment:
model_gender = '' if model_props.gender == 'MALE' else ' [F]'
Related Query
- Dictionary syntax error «can’t assign to function call»
- Keep getting syntax error around async def on_ready(): whilst making discord bot
- How to debug lxml.etree.XSLTParseError: Invalid expression error
- Non-ASCII character Syntax Error in Python while calling URL
- Regular expression for python syntax
- if i!=0 in list comprehension gives syntax error
- python syntax error *args with optional arg
- Syntax error while installing pdfminer using python
- sqlite3.OperationalError: near «)»: syntax error
- Invalid syntax error in Python
- Python: «yield from » produces syntax error
- psycopg2 syntax error at or near «UPDATE»
- Jupyter Notebook Anaconda cannot open because of syntax error with dateutil
- OSX Caffe Compilation Fails With Expected Expression Error
- Python «all» function with conditional generator expression returning True. Why?
- XML parser syntax error
- psycopg2 syntax error because of single quotes in insert command
- Syntax error while trying to run scripts from iPython
- py2exe compiling error : invalid syntax (_socket3.py, line 183)
- Python 2.7 — unpacking iterable object throws syntax error
- Psycopg2 query with variable number of parameters throws syntax error
- elif giving syntax error python?
- Conditional matching in regular expression
- Jython syntax error on ‘finally’ word
- Syntax Error without sudo
- Python IRC 8.1.1 Invalid Syntax Error with class Connection(object, metaclass=abc.ABCmeta)?
- I do not understand the reason for this syntax error
- Syntax error eval’ing an assignment
- sqlite3.OperationalError: near «WHERE»: syntax error (Python 2, sqlite3)
- Syntax error with IP Address format
- Applications not related to python output weird error message: SyntaxError: invalid syntax
- Cannot execute SQL HANA query in Python — syntax error
- syntax error when using forward slash to define positional only arguments
- psycopg2 dynamic insert query issues, syntax error at or near Identifier
- Stanford parser and nltk produce (regular expression matching?) error
- Syntax error with Cloud 9?
- try. else. except syntax error
- Python 3.1: Syntax Error for Everything! (Mac OS X)
- Is IndentationError a syntax error in Python or not?
- How to check python code’s syntax error before running it
- Why is the regular expression returning an error in python?
- GNU Radio (Companion) gives Python Syntax Error on variable substitution
- F string prefix in python giving a syntax error
- «print» throws an invalid syntax error in Python 3
- PyCharm false syntax error using turtle
- Python Syntax Error on Bit Literal
- Syntax error while using Lambda functions
- Invalid syntax using regular expression in python 3.4
- Syntax error with ternary operator
- Python syntax error (in the interpreter) after a for loop
More Query from same tag
- Plotting two distance matrices together on same plot?
- From Haskell to Python: how to do currying?
- How to click on load button dynamically using selenium python?
- python ImportError without import call
- How to improve Merge Sort speed in python
- Python: split list into chunks of defined size and fill up rest
- How do you simulate buoyancy in games?
- Parsing JSON object with large number of unique keys (not a list of objects) using PySpark
- Python + cx_Oracle : Unable to acquire Oracle environment handle
- __getattr__ on a class and not (or as well as) an instance
- Why am I getting «TypeError: id() takes exactly one argument (0 given)»?
- Shading a segment between two lines on polar axis (matplotlib)
- Differences in Python struct module lengths
- Training speed on GPU become slower overtime
- Explaining a multi-label target Tensorflow model with SHAP
- python string split by separator all possible permutations
- Trouble scraping titles from a webpage
- How exactly does Python find `__new__` and choose its arguments?
- pySerial readline reads previously written data
- tf.keras `predict()` gets different results
- Can’t save PIL image in python
- Get QML element in Squish by id or objectName using findObject() or waitForObject() without object map
- Tkinter and openpyxl error no such file or directory »
- Mapping a single function is slower than twice mapping two separate functions?
- Python — Folium Choropleth Map — colors incorrect
- CMS channel list using Youtube API with onBehalfOfContentOwner
- How can I pip install pyx package from externally hosted source?
- Link Openssl-FIPS library for Python3
- PyS60 application not going full screen
- Running Python apps without python
- Can’t execute python script from php
- Why does it work?
- _tkinter.TclError: no display name and no $DISPLAY environment variable for new OS X Mavericks
- How to import a json from a file on cloud storage to Bigquery
- Slow public key authentication with paramiko
Python Ternary Conditional Operator
This article shows you how to write Python ternary operator , also called conditional expressions.
expression1 will be evaluated if the condition is true, otherwise expression2 will be evaluated.
1. Ternary Operator
1.1 This example will print whether a number is odd or even.
n = 5 print("Even") if n % 2 == 0 else print("Odd")
1.2 Can’t assign to conditional expression.
## we can't use syntax as follows a = 5 if True else a = 6
File "", line 2 a = 5 if True else a = 6 ^ SyntaxError: can't assign to conditional expression
Instead, assign value to a variable as follows.
## we can use it as follows a = 5 if False else 6 print(a)
2. Multilevel Ternary Operator
Till now, we have used the ternary operator with one condition only. Let’s see how to use it for multiple conditions. Suppose we have to check for two conditions, one is for even, and the other is for the multiple of four. Try to write the code for the condition using the ternary operator.
n = int(input("number: ")) print("Satisfied") if n % 4 == 0 else print("Destroyed1") if n % 2 == 0 else print("Destroyed2")
number: 3 Destroyed2 number: 6 Destroyed1 number: 8 Satisfied
Python executes the rightmost conditional operator first. So, in the above program, it checks whether the number is even or not first. If it’s Even then, it goes to check whether it is a multiple of four or not.
3. Python Tuple
We can use Tuple as ternary operator, it works like this:
(get_this_if_false, get_this_if_true)[condition]
n = 20 canVote = (False, True)[n >= 18] print(canVote)
n = 10 canVote = (False, True)[n >= 18] print(canVote)