If-Then-Else in One Line Python

preview_player
Показать описание
Python is so powerful, you can even compress whole algorithms in a single line of code. So the natural question arises: can you write conditional if-then-else statements in a single line of code? This article explores this mission-critical question in all detail.

Read the full article at:

~~~
Become a better coder! Download the 5x Python Cheat Sheet Course (PDF). It’s free!

🐍 Cheating in Python? Of course! 🐍
See you soon -- it’s fun! 🤓
~~~

Experts in their fields read more books! Become a Python expert: 📚

And become a Python master the fun way!
~~~

Start earning money by creating your own Python coding business (free webinar):
Рекомендации по теме
Комментарии
Автор

if 42 in range(100): print(42); print(42);

Hereafter the first print(42) the rest of the code would be treated as next line of code not the multi-line code. You may test the same with if-else statement in one line code.

for eg.

x = 1

print ("Greater") if x>0 else print("Smaller"); print("is it part of else statement")


in the above code, the value of x is greater than 0 so as per you it should only display Greater but it will also display is it part of else statement.

RameshKumar-pqtw
Автор

You can skip the 2nd print() after else :
print('42' if 42 in range(100) else 'whatever here')

ricardotijerina
Автор

your video was helpful but i think you can make it much more better

khush