Last active
September 25, 2020 14:54
-
-
Save sudhamjayanthi/628001e4c75db3def74f09687cea977e to your computer and use it in GitHub Desktop.
[PYTHON] One liner IF-ELSE statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| ----------------------------------------------------- | |
| REQUIRED KNOWLEDGE TO UNDERSTAND: | |
| 1. Python Lists & indexing | |
| 2. Python if-else statements | |
| 3. In python when we cast a boolean to int | |
| 1. TRUE changes to 1 i.e int(True) --> 1 | |
| 2. FALSE changes to 0 i.e int(False) --> 0 | |
| ------------------------------------------------------ | |
| ''' ''' | |
| ------------------------------------------------------ | |
| SYNTAX FOR ONE LINE IF-ELSE STATEMENT IN PYTHON: | |
| variable = [value if true, value if false][ condition ] | |
| ------------------------------------------------------ | |
| ''' | |
| ------------------------------------------------------ | |
| # EXAMPLE: | |
| x = ["1 is less than 2","1 is greater than two"][1>2] | |
| ''' | |
| ------------------------------------------------------ | |
| EXPLANATION: | |
| Here 1 > 2 returns True and as it is given as a index of list, True gets casted to 1 [ int(True) --> 1 ] | |
| So x will be assigned the value of second item/first index of the list. | |
| ------------------------------------------------------ | |
| ''' | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
READ THIS ⬇️
STAR ⭐ THIS GIST if you don't know this trick. That would let me know that I was helpful to someone & would inspire me to post more gists.
- Sudham Jayanthi
Instagram
Github
Email