You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python.rst
+29-3Lines changed: 29 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1552,6 +1552,7 @@ and an optional ``else`` statement at the end. In Python, the word ``elif`` is D
1552
1552
1553
1553
Python supports the following tests: ``>``, ``>=``, ``<``, ``<=``, ``==``, and ``!=``. For boolean operators use ``and``, ``or``, and ``not`` (``&``, ``|``, and ``^`` are the bitwise operators).
1554
1554
1555
+
1555
1556
Note that Python also supports *range comparisons*::
1556
1557
1557
1558
>>> x = 4
@@ -1605,6 +1606,31 @@ The following table lists *truthy* and *falsey* values:
The ``and`` statement will short circuit if it evaluates to false::
1613
+
1614
+
>>> 0 and 1/0
1615
+
0
1616
+
1617
+
Likewise, the ``or`` statement will short circuit when something evaluates to true::
1618
+
1619
+
>>> 1 or 1/0
1620
+
1
1621
+
1622
+
Ternary Operator
1623
+
------------------
1624
+
1625
+
Python has its own ternary operator, called a *conditional expression* (see PEP 308). These are handy as they can be used in comprehension constructs and ``lambda`` functions::
1626
+
1627
+
>>> last = 'Lennon' if band == 'Beatles' else 'Jones'
1628
+
1629
+
Note that this has similar behavior to an ``if`` statement, but it is an expression, and not a statement. Python
1630
+
distinguishes these two. An easy way to determine between the two, is to remember that an expression follows a ``return`` statement. Anything you can ``return`` is an expression.
1631
+
1632
+
1633
+
1608
1634
1609
1635
1610
1636
Exceptions
@@ -2278,14 +2304,14 @@ You can import a package or a module::
2278
2304
import packagename
2279
2305
import packagename.module1
2280
2306
2281
-
Assume there is a ``fib`` function in ``module1``. You have access to everything in the namespace of the module you imported::
2307
+
Assume there is a ``fib`` function in ``module1``. You have access to everything in the namespace of the module you imported. To use this function you will need to use the fully qualified name, ``packagename.module1.fib``::
2282
2308
2283
2309
import packagename.module1
2284
2310
2285
2311
packagename.module1.fib()
2286
2312
2287
-
To use this you will need to use the fully qualified name, ``packagename.module1.fib``.
2288
-
If you only want to import the ``fib`` use the ``from`` variant::
2313
+
2314
+
If you only want to import the ``fib`` function, use the ``from`` variant::
0 commit comments