|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "7fbc4c40", |
| 5 | + "id": "3561769f", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | 8 | "# Python" |
9 | 9 | ] |
10 | 10 | }, |
11 | 11 | { |
12 | 12 | "cell_type": "markdown", |
13 | | - "id": "6d82b2da", |
| 13 | + "id": "79f02d37", |
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | 16 | "## Disclaimer\n", |
|
24 | 24 | }, |
25 | 25 | { |
26 | 26 | "cell_type": "markdown", |
27 | | - "id": "25a89cd4", |
| 27 | + "id": "8d01e2d6", |
28 | 28 | "metadata": {}, |
29 | 29 | "source": [ |
30 | 30 | "## Why Python?\n", |
|
48 | 48 | }, |
49 | 49 | { |
50 | 50 | "cell_type": "markdown", |
51 | | - "id": "757dc2da", |
| 51 | + "id": "0513894c", |
52 | 52 | "metadata": {}, |
53 | 53 | "source": [ |
54 | 54 | "## Basics\n", |
|
122 | 122 | }, |
123 | 123 | { |
124 | 124 | "cell_type": "markdown", |
125 | | - "id": "d7c9a774", |
| 125 | + "id": "b0c01e83", |
126 | 126 | "metadata": {}, |
127 | 127 | "source": [ |
128 | 128 | "### Operators\n", |
|
140 | 140 | { |
141 | 141 | "cell_type": "code", |
142 | 142 | "execution_count": 1, |
143 | | - "id": "789d1369", |
| 143 | + "id": "8554f54a", |
144 | 144 | "metadata": {}, |
145 | 145 | "outputs": [ |
146 | 146 | { |
|
162 | 162 | { |
163 | 163 | "cell_type": "code", |
164 | 164 | "execution_count": 2, |
165 | | - "id": "3fbcd67e", |
| 165 | + "id": "c0c3e574", |
166 | 166 | "metadata": {}, |
167 | 167 | "outputs": [ |
168 | 168 | { |
|
183 | 183 | { |
184 | 184 | "cell_type": "code", |
185 | 185 | "execution_count": 3, |
186 | | - "id": "42193ad1", |
| 186 | + "id": "98d59155", |
187 | 187 | "metadata": {}, |
188 | 188 | "outputs": [ |
189 | 189 | { |
|
204 | 204 | { |
205 | 205 | "cell_type": "code", |
206 | 206 | "execution_count": 4, |
207 | | - "id": "eb2c1de9", |
| 207 | + "id": "5a24834f", |
208 | 208 | "metadata": {}, |
209 | 209 | "outputs": [ |
210 | 210 | { |
|
224 | 224 | }, |
225 | 225 | { |
226 | 226 | "cell_type": "markdown", |
227 | | - "id": "75e7101f", |
| 227 | + "id": "717a792a", |
228 | 228 | "metadata": {}, |
229 | 229 | "source": [ |
230 | 230 | "### Variables\n", |
|
243 | 243 | { |
244 | 244 | "cell_type": "code", |
245 | 245 | "execution_count": 6, |
246 | | - "id": "ca738e52", |
| 246 | + "id": "0563eb8f", |
247 | 247 | "metadata": {}, |
248 | 248 | "outputs": [ |
249 | 249 | { |
|
267 | 267 | { |
268 | 268 | "cell_type": "code", |
269 | 269 | "execution_count": 7, |
270 | | - "id": "6876244a", |
| 270 | + "id": "6b55146b", |
271 | 271 | "metadata": {}, |
272 | 272 | "outputs": [ |
273 | 273 | { |
|
289 | 289 | }, |
290 | 290 | { |
291 | 291 | "cell_type": "markdown", |
292 | | - "id": "7b8f2503", |
| 292 | + "id": "8d1935a6", |
293 | 293 | "metadata": {}, |
294 | 294 | "source": [ |
295 | 295 | "There are a few basic data types in Python:\n", |
|
302 | 302 | }, |
303 | 303 | { |
304 | 304 | "cell_type": "markdown", |
305 | | - "id": "065d39d0", |
| 305 | + "id": "529d01c9", |
306 | 306 | "metadata": {}, |
307 | 307 | "source": [ |
308 | 308 | "### Functions\n", |
|
321 | 321 | { |
322 | 322 | "cell_type": "code", |
323 | 323 | "execution_count": 8, |
324 | | - "id": "274bc7f3", |
| 324 | + "id": "1f5e00a8", |
325 | 325 | "metadata": {}, |
326 | 326 | "outputs": [ |
327 | 327 | { |
|
346 | 346 | { |
347 | 347 | "cell_type": "code", |
348 | 348 | "execution_count": 11, |
349 | | - "id": "1c96e931", |
| 349 | + "id": "0be9003a", |
350 | 350 | "metadata": {}, |
351 | 351 | "outputs": [ |
352 | 352 | { |
|
369 | 369 | { |
370 | 370 | "cell_type": "code", |
371 | 371 | "execution_count": 14, |
372 | | - "id": "4ddd5d5c", |
| 372 | + "id": "0f34eee1", |
373 | 373 | "metadata": {}, |
374 | 374 | "outputs": [ |
375 | 375 | { |
|
393 | 393 | }, |
394 | 394 | { |
395 | 395 | "cell_type": "markdown", |
396 | | - "id": "d757141a", |
| 396 | + "id": "831d2b85", |
397 | 397 | "metadata": {}, |
398 | 398 | "source": [ |
399 | 399 | "## Lists\n", |
|
408 | 408 | { |
409 | 409 | "cell_type": "code", |
410 | 410 | "execution_count": 15, |
411 | | - "id": "89da7bac", |
| 411 | + "id": "0acc93fa", |
412 | 412 | "metadata": {}, |
413 | 413 | "outputs": [ |
414 | 414 | { |
|
429 | 429 | { |
430 | 430 | "cell_type": "code", |
431 | 431 | "execution_count": 16, |
432 | | - "id": "5cfa87a8", |
| 432 | + "id": "db2d9595", |
433 | 433 | "metadata": {}, |
434 | 434 | "outputs": [ |
435 | 435 | { |
|
452 | 452 | }, |
453 | 453 | { |
454 | 454 | "cell_type": "markdown", |
455 | | - "id": "0a40d867", |
| 455 | + "id": "fbb59661", |
456 | 456 | "metadata": {}, |
457 | 457 | "source": [ |
458 | 458 | "It is possible to access individual elements of a list, either for reading or changing its value. To do so, the element is addressed by its index, which starts at zero. The syntax for this is `list[index]`. To reference the elements starting at the last one, negative indices are used. Here the last element has the index -1." |
|
461 | 461 | { |
462 | 462 | "cell_type": "code", |
463 | 463 | "execution_count": 19, |
464 | | - "id": "cb179a79", |
| 464 | + "id": "ebdf8692", |
465 | 465 | "metadata": {}, |
466 | 466 | "outputs": [ |
467 | 467 | { |
|
485 | 485 | { |
486 | 486 | "cell_type": "code", |
487 | 487 | "execution_count": 20, |
488 | | - "id": "1a455b04", |
| 488 | + "id": "92a6d554", |
489 | 489 | "metadata": {}, |
490 | 490 | "outputs": [ |
491 | 491 | { |
|
510 | 510 | }, |
511 | 511 | { |
512 | 512 | "cell_type": "markdown", |
513 | | - "id": "794632e6", |
| 513 | + "id": "6b4bebff", |
514 | 514 | "metadata": {}, |
515 | 515 | "source": [ |
516 | 516 | "## Strings\n", |
|
527 | 527 | { |
528 | 528 | "cell_type": "code", |
529 | 529 | "execution_count": 24, |
530 | | - "id": "f704d8c9", |
| 530 | + "id": "a6c0618d", |
531 | 531 | "metadata": {}, |
532 | 532 | "outputs": [ |
533 | 533 | { |
|
549 | 549 | }, |
550 | 550 | { |
551 | 551 | "cell_type": "markdown", |
552 | | - "id": "2435c155", |
| 552 | + "id": "16e8ea15", |
553 | 553 | "metadata": {}, |
554 | 554 | "source": [ |
555 | 555 | "To incorporate variable values into strings, formatted strings (f-strings) can be used. They allow to reference variable values and control their string representation. The placeholder is defined with `{variable_name}` and the f-string is declared with `f''` or `f\"\"`." |
|
558 | 558 | { |
559 | 559 | "cell_type": "code", |
560 | 560 | "execution_count": 26, |
561 | | - "id": "a2729b1c", |
| 561 | + "id": "240870f2", |
562 | 562 | "metadata": {}, |
563 | 563 | "outputs": [ |
564 | 564 | { |
|
586 | 586 | }, |
587 | 587 | { |
588 | 588 | "cell_type": "markdown", |
589 | | - "id": "72c94c38", |
| 589 | + "id": "0946d715", |
590 | 590 | "metadata": {}, |
591 | 591 | "source": [ |
592 | 592 | "## Flow Control\n", |
593 | 593 | "\n", |
| 594 | + "There exist two main structures, which are common in many programming languages and allow to build a complex execution flow.\n", |
| 595 | + "\n", |
| 596 | + "* **Loops** repeat a set of expressions (loop body) for a given loop variable. The value of the loop variable iterates throug a list of values.\n", |
| 597 | + "\n", |
| 598 | + "* **Branching** allows to execute expressions depending on a condition. \n", |
| 599 | + "\n", |
594 | 600 | "### Loops\n", |
595 | 601 | "\n", |
| 602 | + "The basic structure of a loop is\n", |
| 603 | + "\n", |
| 604 | + "```python\n", |
| 605 | + "# loop header\n", |
| 606 | + "for loop_variable in loop_values:\n", |
| 607 | + " # loop body\n", |
| 608 | + " loop_body()\n", |
| 609 | + "```\n", |
| 610 | + "\n", |
| 611 | + "Here, the name of the loop variable can be freely chosen and the loop body may consist of any number of expressions. The syntax for a loop definition includes the keywords `for` and `in`, while the header is closed with a `:`. The loop body is intended by one level (e.g. 4 spaces or a single tab)." |
| 612 | + ] |
| 613 | + }, |
| 614 | + { |
| 615 | + "cell_type": "code", |
| 616 | + "execution_count": 27, |
| 617 | + "id": "d0f68bf8", |
| 618 | + "metadata": {}, |
| 619 | + "outputs": [ |
| 620 | + { |
| 621 | + "name": "stdout", |
| 622 | + "output_type": "stream", |
| 623 | + "text": [ |
| 624 | + "4 16\n", |
| 625 | + "7 49\n", |
| 626 | + "2 4\n", |
| 627 | + "9 81\n" |
| 628 | + ] |
| 629 | + } |
| 630 | + ], |
| 631 | + "source": [ |
| 632 | + "# Loop example 1\n", |
| 633 | + "\n", |
| 634 | + "for i in [4,7,2,9]:\n", |
| 635 | + " print(i, i**2)" |
| 636 | + ] |
| 637 | + }, |
| 638 | + { |
| 639 | + "cell_type": "code", |
| 640 | + "execution_count": 29, |
| 641 | + "id": "fc0a60ff", |
| 642 | + "metadata": {}, |
| 643 | + "outputs": [ |
| 644 | + { |
| 645 | + "name": "stdout", |
| 646 | + "output_type": "stream", |
| 647 | + "text": [ |
| 648 | + "0 0\n", |
| 649 | + "1 10\n", |
| 650 | + "2 20\n", |
| 651 | + "3 30\n", |
| 652 | + "4 40\n", |
| 653 | + "5 50\n", |
| 654 | + "6 60\n", |
| 655 | + "7 70\n", |
| 656 | + "8 80\n", |
| 657 | + "9 90\n" |
| 658 | + ] |
| 659 | + } |
| 660 | + ], |
| 661 | + "source": [ |
| 662 | + "# Loop example 2\n", |
| 663 | + "\n", |
| 664 | + "for j in range(10):\n", |
| 665 | + " print(j, j*10)" |
| 666 | + ] |
| 667 | + }, |
| 668 | + { |
| 669 | + "cell_type": "markdown", |
| 670 | + "id": "4158a94e", |
| 671 | + "metadata": {}, |
| 672 | + "source": [ |
| 673 | + "The build-in function [`range()`](https://docs.python.org/3/library/functions.html#func-range) is often used in the context of loops to generate a structured sequence of values to be looped over. It can take up to three arguments (start, stop, step) to control the created sequance." |
| 674 | + ] |
| 675 | + }, |
| 676 | + { |
| 677 | + "cell_type": "code", |
| 678 | + "execution_count": 30, |
| 679 | + "id": "a4d6f177", |
| 680 | + "metadata": {}, |
| 681 | + "outputs": [ |
| 682 | + { |
| 683 | + "name": "stdout", |
| 684 | + "output_type": "stream", |
| 685 | + "text": [ |
| 686 | + "10\n", |
| 687 | + "12\n", |
| 688 | + "14\n", |
| 689 | + "16\n", |
| 690 | + "18\n", |
| 691 | + "20\n", |
| 692 | + "22\n", |
| 693 | + "24\n", |
| 694 | + "26\n", |
| 695 | + "28\n" |
| 696 | + ] |
| 697 | + } |
| 698 | + ], |
| 699 | + "source": [ |
| 700 | + "# Range example\n", |
| 701 | + "\n", |
| 702 | + "for k in range(10, 30, 2):\n", |
| 703 | + " print(k)" |
| 704 | + ] |
| 705 | + }, |
| 706 | + { |
| 707 | + "cell_type": "markdown", |
| 708 | + "id": "db4cbea3", |
| 709 | + "metadata": {}, |
| 710 | + "source": [ |
596 | 711 | "### Branching" |
597 | 712 | ] |
598 | 713 | }, |
599 | 714 | { |
600 | 715 | "cell_type": "markdown", |
601 | | - "id": "e8eea953", |
| 716 | + "id": "27a4dd9c", |
602 | 717 | "metadata": {}, |
603 | 718 | "source": [ |
604 | 719 | "## Modules\n", |
|
0 commit comments