Created
January 5, 2017 05:45
-
-
Save u1and0/4529b9451670bab93b1164dc171cb749 to your computer and use it in GitHub Desktop.
pythonで三項演算子のネスト ref: http://qiita.com/u1and0/items/e47e3de059e27d4ebd74
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
(変数) = (条件がTrueのときの値) if (条件) 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
(変数) = (条件がTrueのときの値) if (条件) else (条件がFalseのときの値) if (条件) else (条件がFalseのときの値) ...if-()-else-()無限につなげていく |
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
In [22]: for v in range(-5, 15): | |
...: if v > 10: | |
...: p = 'foo' | |
...: else: | |
...: p = 'bar' | |
...: print(v, p) | |
...: | |
-5 bar | |
-4 bar | |
-3 bar | |
-2 bar | |
-1 bar | |
0 bar | |
1 bar | |
2 bar | |
3 bar | |
4 bar | |
5 bar | |
6 bar | |
7 bar | |
8 bar | |
9 bar | |
10 bar | |
11 foo | |
12 foo | |
13 foo | |
14 foo |
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
In [36]: for v in range(-5, 15, 1): | |
...: if v > 10: | |
...: p = 'foo' | |
...: elif v < 0: | |
...: p = 'foobar' | |
...: else: | |
...: p = 'bar' | |
...: print(v, p) | |
...: | |
-5 foobar | |
-4 foobar | |
-3 foobar | |
-2 foobar | |
-1 foobar | |
0 bar | |
1 bar | |
2 bar | |
3 bar | |
4 bar | |
5 bar | |
6 bar | |
7 bar | |
8 bar | |
9 bar | |
10 bar | |
11 foo | |
12 foo | |
13 foo | |
14 foo |
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
In [38]: for v in range(-5, 15, 1): | |
...: p = 'foo' if v > 10 else 'foobar' if v < 0 else 'bar' | |
...: print(v, p) | |
-5 foobar | |
-4 foobar | |
-3 foobar | |
-2 foobar | |
-1 foobar | |
0 bar | |
1 bar | |
2 bar | |
3 bar | |
4 bar | |
5 bar | |
6 bar | |
7 bar | |
8 bar | |
9 bar | |
10 bar | |
11 foo | |
12 foo | |
13 foo | |
14 foo |
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
In [26]: for v in range(-5, 15): | |
...: p = 'foo' if v > 10 else 'bar' | |
...: print(v, p) | |
-5 bar | |
-4 bar | |
-3 bar | |
-2 bar | |
-1 bar | |
0 bar | |
1 bar | |
2 bar | |
3 bar | |
4 bar | |
5 bar | |
6 bar | |
7 bar | |
8 bar | |
9 bar | |
10 bar | |
11 foo | |
12 foo | |
13 foo | |
14 foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment