# Using the question mark (?) for inline conditions

**URL:** <https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155>\
**Category:** Python Help\
**Created:** [August 6, 2024, 6:46pm UTC](https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155 "2024-08-06T18:46:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nietanod](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nietanod/32/21769_2.png) [@Nietanod](https://discuss.python.org/u/Nietanod)\
**Post date:** [August 6, 2024, 6:46pm UTC](https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155/1 "2024-08-06T18:46:57Z")

</div>

In JavaScript, you can code some conditional assignment by using “?”

> var weather = temperature \> 30 ? “Hot” : “cold”

It means that `weather` will be set to ‘hot’ if `temperature > 30` is true, and to ‘cold’ if `temperature > 30` is false.

You can find more [here](https://www.freecodecamp.org/news/how-the-question-mark-works-in-javascript/).

I think it’s a good idea to implement this feature in python, because it can help developers to code faster and have a lighter code.

The implementation may be (in my opinion) something like this (exactly the same as JavaScript) :

> some\_var = condition ? value\_a : value\_b

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [August 6, 2024, 7:05pm UTC](https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155/2 "2024-08-06T19:05:52Z")

</div>

Good news! Guido used his time machine to add this in Python 2.5:

> <https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator>

> **[6. Expressions](https://docs.python.org/3/reference/expressions.html#conditional-expressions)**
>
> This chapter explains the meaning of the elements of expressions in Python. Syntax Notes: In this and the following chapters, extended BNF notation will be used to describe syntax, not lexical anal...

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [August 6, 2024, 7:06pm UTC](https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155/3 "2024-08-06T19:06:10Z")

</div>

Moving this to the Help category, as this already exists: `x = y if condition else z`

---

<div class="post-metadata">

**Author:** ![Nietanod](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nietanod/32/21769_2.png) [@Nietanod](https://discuss.python.org/u/Nietanod)\
**Post date:** [August 6, 2024, 7:07pm UTC](https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155/4 "2024-08-06T19:07:47Z")

</div>

Oh sorry i didn’t remember this one.  
You are right.  
But maybe there is something to do with the question mark?

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [August 6, 2024, 7:11pm UTC](https://discuss.python.org/t/using-the-question-mark-for-inline-conditions/60155/5 "2024-08-06T19:11:47Z")

</div>

> **[PEP 505 – None-aware operators | peps.python.org](https://peps.python.org/pep-0505/)**
>
> Several modern programming languages have so-called “null-coalescing” or “null- aware” operators, including C# 1, Dart 2, Perl, Swift, and PHP (starting in version 7). There are also stage 3 draft proposals for their addition to ECMAScript (a.k.a....
