Skip to content

Commit 947ba1b

Browse files
committed
added loops
1 parent eef6ef3 commit 947ba1b

File tree

1 file changed

+144
-29
lines changed

1 file changed

+144
-29
lines changed

book/content/tools/03_analysis/01_python.ipynb

Lines changed: 144 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "7fbc4c40",
5+
"id": "3561769f",
66
"metadata": {},
77
"source": [
88
"# Python"
99
]
1010
},
1111
{
1212
"cell_type": "markdown",
13-
"id": "6d82b2da",
13+
"id": "79f02d37",
1414
"metadata": {},
1515
"source": [
1616
"## Disclaimer\n",
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "markdown",
27-
"id": "25a89cd4",
27+
"id": "8d01e2d6",
2828
"metadata": {},
2929
"source": [
3030
"## Why Python?\n",
@@ -48,7 +48,7 @@
4848
},
4949
{
5050
"cell_type": "markdown",
51-
"id": "757dc2da",
51+
"id": "0513894c",
5252
"metadata": {},
5353
"source": [
5454
"## Basics\n",
@@ -122,7 +122,7 @@
122122
},
123123
{
124124
"cell_type": "markdown",
125-
"id": "d7c9a774",
125+
"id": "b0c01e83",
126126
"metadata": {},
127127
"source": [
128128
"### Operators\n",
@@ -140,7 +140,7 @@
140140
{
141141
"cell_type": "code",
142142
"execution_count": 1,
143-
"id": "789d1369",
143+
"id": "8554f54a",
144144
"metadata": {},
145145
"outputs": [
146146
{
@@ -162,7 +162,7 @@
162162
{
163163
"cell_type": "code",
164164
"execution_count": 2,
165-
"id": "3fbcd67e",
165+
"id": "c0c3e574",
166166
"metadata": {},
167167
"outputs": [
168168
{
@@ -183,7 +183,7 @@
183183
{
184184
"cell_type": "code",
185185
"execution_count": 3,
186-
"id": "42193ad1",
186+
"id": "98d59155",
187187
"metadata": {},
188188
"outputs": [
189189
{
@@ -204,7 +204,7 @@
204204
{
205205
"cell_type": "code",
206206
"execution_count": 4,
207-
"id": "eb2c1de9",
207+
"id": "5a24834f",
208208
"metadata": {},
209209
"outputs": [
210210
{
@@ -224,7 +224,7 @@
224224
},
225225
{
226226
"cell_type": "markdown",
227-
"id": "75e7101f",
227+
"id": "717a792a",
228228
"metadata": {},
229229
"source": [
230230
"### Variables\n",
@@ -243,7 +243,7 @@
243243
{
244244
"cell_type": "code",
245245
"execution_count": 6,
246-
"id": "ca738e52",
246+
"id": "0563eb8f",
247247
"metadata": {},
248248
"outputs": [
249249
{
@@ -267,7 +267,7 @@
267267
{
268268
"cell_type": "code",
269269
"execution_count": 7,
270-
"id": "6876244a",
270+
"id": "6b55146b",
271271
"metadata": {},
272272
"outputs": [
273273
{
@@ -289,7 +289,7 @@
289289
},
290290
{
291291
"cell_type": "markdown",
292-
"id": "7b8f2503",
292+
"id": "8d1935a6",
293293
"metadata": {},
294294
"source": [
295295
"There are a few basic data types in Python:\n",
@@ -302,7 +302,7 @@
302302
},
303303
{
304304
"cell_type": "markdown",
305-
"id": "065d39d0",
305+
"id": "529d01c9",
306306
"metadata": {},
307307
"source": [
308308
"### Functions\n",
@@ -321,7 +321,7 @@
321321
{
322322
"cell_type": "code",
323323
"execution_count": 8,
324-
"id": "274bc7f3",
324+
"id": "1f5e00a8",
325325
"metadata": {},
326326
"outputs": [
327327
{
@@ -346,7 +346,7 @@
346346
{
347347
"cell_type": "code",
348348
"execution_count": 11,
349-
"id": "1c96e931",
349+
"id": "0be9003a",
350350
"metadata": {},
351351
"outputs": [
352352
{
@@ -369,7 +369,7 @@
369369
{
370370
"cell_type": "code",
371371
"execution_count": 14,
372-
"id": "4ddd5d5c",
372+
"id": "0f34eee1",
373373
"metadata": {},
374374
"outputs": [
375375
{
@@ -393,7 +393,7 @@
393393
},
394394
{
395395
"cell_type": "markdown",
396-
"id": "d757141a",
396+
"id": "831d2b85",
397397
"metadata": {},
398398
"source": [
399399
"## Lists\n",
@@ -408,7 +408,7 @@
408408
{
409409
"cell_type": "code",
410410
"execution_count": 15,
411-
"id": "89da7bac",
411+
"id": "0acc93fa",
412412
"metadata": {},
413413
"outputs": [
414414
{
@@ -429,7 +429,7 @@
429429
{
430430
"cell_type": "code",
431431
"execution_count": 16,
432-
"id": "5cfa87a8",
432+
"id": "db2d9595",
433433
"metadata": {},
434434
"outputs": [
435435
{
@@ -452,7 +452,7 @@
452452
},
453453
{
454454
"cell_type": "markdown",
455-
"id": "0a40d867",
455+
"id": "fbb59661",
456456
"metadata": {},
457457
"source": [
458458
"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,7 +461,7 @@
461461
{
462462
"cell_type": "code",
463463
"execution_count": 19,
464-
"id": "cb179a79",
464+
"id": "ebdf8692",
465465
"metadata": {},
466466
"outputs": [
467467
{
@@ -485,7 +485,7 @@
485485
{
486486
"cell_type": "code",
487487
"execution_count": 20,
488-
"id": "1a455b04",
488+
"id": "92a6d554",
489489
"metadata": {},
490490
"outputs": [
491491
{
@@ -510,7 +510,7 @@
510510
},
511511
{
512512
"cell_type": "markdown",
513-
"id": "794632e6",
513+
"id": "6b4bebff",
514514
"metadata": {},
515515
"source": [
516516
"## Strings\n",
@@ -527,7 +527,7 @@
527527
{
528528
"cell_type": "code",
529529
"execution_count": 24,
530-
"id": "f704d8c9",
530+
"id": "a6c0618d",
531531
"metadata": {},
532532
"outputs": [
533533
{
@@ -549,7 +549,7 @@
549549
},
550550
{
551551
"cell_type": "markdown",
552-
"id": "2435c155",
552+
"id": "16e8ea15",
553553
"metadata": {},
554554
"source": [
555555
"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,7 +558,7 @@
558558
{
559559
"cell_type": "code",
560560
"execution_count": 26,
561-
"id": "a2729b1c",
561+
"id": "240870f2",
562562
"metadata": {},
563563
"outputs": [
564564
{
@@ -586,19 +586,134 @@
586586
},
587587
{
588588
"cell_type": "markdown",
589-
"id": "72c94c38",
589+
"id": "0946d715",
590590
"metadata": {},
591591
"source": [
592592
"## Flow Control\n",
593593
"\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",
594600
"### Loops\n",
595601
"\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": [
596711
"### Branching"
597712
]
598713
},
599714
{
600715
"cell_type": "markdown",
601-
"id": "e8eea953",
716+
"id": "27a4dd9c",
602717
"metadata": {},
603718
"source": [
604719
"## Modules\n",

0 commit comments

Comments
 (0)