Created
January 28, 2013 01:58
-
-
Save bennuttall/4652238 to your computer and use it in GitHub Desktop.
Code snippets from Ternary Operator & Shorthand Code blog post
This file contains 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
if x > 0: | |
var += 1 |
This file contains 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
var += 1 if x > 0 else 0 |
This file contains 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
if (a > b) { | |
result = x; | |
} | |
else { | |
result = y; | |
} |
This file contains 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
var += 2*(x>0) # increment by 2 if (and only if) true | |
var += x*(x>0) # increment by x if (and only if) true | |
var += y*(x>0) # increment by y if (and only if) true |
This file contains 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
var += x>0 |
This file contains 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
def sum_digits_greater_than_ten(n): | |
return sum([int(j) for j in str(n)]) > 10 |
This file contains 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
def sum_digits_greater_than_ten(n): | |
return True if sum([int(j) for j in str(n)]) > 10 else False |
This file contains 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
result = a > b ? x : y; |
This file contains 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
<?php | |
echo 'User is ' . $user['loggedin'] ? 'online' : 'offline'; |
This file contains 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
vowels += 'y' | |
print all([j in vowels for j in 'you']) | |
# returns True because all the values in the resultant list are True |
This file contains 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
vowels = 'aeiou' | |
print sum(j in vowels for j in 'stringy cheese') > 5 | |
# returns False because the sum of the values in the resultant list is 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment