forked from yasoob/intermediatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathternary_operators.html
More file actions
80 lines (70 loc) · 3.83 KB
/
Copy pathternary_operators.html
File metadata and controls
80 lines (70 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ternary Operators</title>
<link rel="stylesheet" href="_static/epub.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="decorators.html" title="Decorators"
accesskey="N">next</a></li>
<li class="right" >
<a href="set_-_data_structure.html" title="set Data Structure"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Python Tips 0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="ternary-operators">
<h1>Ternary Operators</h1>
<p>Ternary operators are more commonly known as conditional expressions in
Python. These operators evaluate something based on a condition being
true or not. They became a part of Python in version 2.4</p>
<p>Here is a blueprint and an example of using these conditional
expressions.</p>
<p><strong>Blueprint:</strong></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">condition_is_true</span> <span class="k">if</span> <span class="n">condition_true</span> <span class="k">else</span> <span class="n">condition_is_false</span>
</pre></div>
</div>
<p><strong>Example:</strong></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">is_fat</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">state</span> <span class="o">=</span> <span class="s">"fat"</span> <span class="k">if</span> <span class="n">is_fat</span> <span class="k">else</span> <span class="s">"not fat"</span>
</pre></div>
</div>
<p>It allows to quickly test a condition instead of a multiline if
statement. Often times it can be immensely helpful and can make your
code compact but still maintainable.</p>
<p>Another more obscure and not widely used example involves tuples. Here
is some sample code:</p>
<p><strong>Blueprint:</strong></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">if_test_is_false</span><span class="p">,</span> <span class="n">if_test_is_true</span><span class="p">)[</span><span class="n">test</span><span class="p">]</span>
</pre></div>
</div>
<p><strong>Example:</strong></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">fat</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">fitness</span> <span class="o">=</span> <span class="p">(</span><span class="s">"skinny"</span><span class="p">,</span><span class="s">"fat"</span><span class="p">)[</span><span class="n">fat</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Ali is "</span> <span class="o">+</span> <span class="n">fitness</span><span class="p">)</span>
<span class="c"># Output: Ali is fat</span>
</pre></div>
</div>
<p>The above example is not widely used and is generally disliked by
Pythonistas for not being Pythonic. It is also easy to confuse where to
put the true value and where to put the false value in the tuple.</p>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2015, Muhammad Yasoob Ullah Khalid.
</div>
</body>
</html>