Skip to content

Commit 2910453

Browse files
committed
Initial import of basic Python material
1 parent 9f0cafd commit 2910453

6 files changed

+2074
-13
lines changed

001-values.ipynb

+97-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
"\n",
1313
"Key concepts\n",
1414
"* Primitive values and types: `int`, `float`, `complex`, `bool`, `str`, and `NoneType`\n",
15-
"* Type queries\n",
16-
"* Basic string operations\n",
17-
"* Slicing and `slice` objects"
15+
" - Type promotion\n",
16+
" - Type queries\n",
17+
"* Strings\n",
18+
" - Indexing\n",
19+
" - Slicing and `slice` objects\n",
20+
"* Booleans and bit manipulation\n",
21+
"* Syntatic sugar: Update operations"
1822
]
1923
},
2024
{
@@ -172,6 +176,15 @@
172176
"print(type(5.0), isinstance(5.0, complex)) # Does not always behave as expected"
173177
]
174178
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"(5.0).is_integer() # Special test for floating-point values"
186+
]
187+
},
175188
{
176189
"cell_type": "markdown",
177190
"metadata": {},
@@ -191,6 +204,42 @@
191204
"print(x is None, x is not None) # Preferred idiom"
192205
]
193206
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"**\"Big\" integers by default.** Integers do not \"overflow\" as they do in most other languages."
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": null,
217+
"metadata": {},
218+
"outputs": [],
219+
"source": [
220+
"print('(a)', 3**4)\n",
221+
"print('(b)', 3**40)\n",
222+
"print('(c)', 3**400)"
223+
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {},
228+
"source": [
229+
"**Math functions.** For numerical values, many of the functions you might want are available in the `math` module."
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": null,
235+
"metadata": {},
236+
"outputs": [],
237+
"source": [
238+
"import math\n",
239+
"\n",
240+
"math"
241+
]
242+
},
194243
{
195244
"cell_type": "markdown",
196245
"metadata": {},
@@ -392,7 +441,28 @@
392441
"cell_type": "markdown",
393442
"metadata": {},
394443
"source": [
395-
"## Boolean (logical) values and operations\n",
444+
"## Booleans and bit manipulation\n",
445+
"\n",
446+
"Boolean variables can take on the values of `True` or `False`. The built-in boolean operations are `and`, `or`, and `not`."
447+
]
448+
},
449+
{
450+
"cell_type": "code",
451+
"execution_count": null,
452+
"metadata": {},
453+
"outputs": [],
454+
"source": [
455+
"print(True and False)\n",
456+
"print(True or True)\n",
457+
"print(not True)"
458+
]
459+
},
460+
{
461+
"cell_type": "markdown",
462+
"metadata": {},
463+
"source": [
464+
"In addition to booleans, you can also perform bit-level manipulations on integers. The following operators perform logical operations bitwise between the corresponding bits of the operands.\n",
465+
"\n",
396466
"\n",
397467
"| Operator | Name | Description |\n",
398468
"|--------------|-----------------|---------------------------------------------|\n",
@@ -404,14 +474,35 @@
404474
"| ``~a`` | Bitwise NOT | Bitwise negation of ``a`` |"
405475
]
406476
},
477+
{
478+
"cell_type": "markdown",
479+
"metadata": {},
480+
"source": [
481+
"First, some different ways to inspect the binary representations of integer values as strings (see also [`bin()`](https://docs.python.org/3/library/functions.html#bin) in the Python docs):"
482+
]
483+
},
484+
{
485+
"cell_type": "code",
486+
"execution_count": null,
487+
"metadata": {},
488+
"outputs": [],
489+
"source": [
490+
"print(bin(5))\n",
491+
"print(format(5, '#b'))\n",
492+
"print(format(5, 'b')) # no prefix\n",
493+
"print(f'{5:#b}', f'{5:b}')\n",
494+
"print('{:07b}'.format(5)) # 7 bits with leading zeros"
495+
]
496+
},
407497
{
408498
"cell_type": "code",
409499
"execution_count": null,
410500
"metadata": {},
411501
"outputs": [],
412502
"source": [
413-
"print(True & False)\n",
414-
"print(True ^ True)"
503+
"print('{:06b}'.format(5)) # or use '{:#06b}' for a 6-bit string prefixed by '0b'\n",
504+
"print('{:06b}'.format(13))\n",
505+
"print('{:06b}'.format(5 & 13))"
415506
]
416507
},
417508
{

0 commit comments

Comments
 (0)