Digital Medicine I Lists, strings, loops Hans-Joachim Bckenhauer - - PowerPoint PPT Presentation

digital medicine i
SMART_READER_LITE
LIVE PREVIEW

Digital Medicine I Lists, strings, loops Hans-Joachim Bckenhauer - - PowerPoint PPT Presentation

Departement Informatik Digital Medicine I Lists, strings, loops Hans-Joachim Bckenhauer Dennis Komm Autumn 2020 October 15, 2020 Strings Repetition Strings Strings are lists of characters (there are differences) Characters


slide-1
SLIDE 1

Departement Informatik

Digital Medicine I

Lists, strings, loops

Hans-Joachim Böckenhauer Dennis Komm

Autumn 2020 – October 15, 2020

slide-2
SLIDE 2

Strings

Repetition

slide-3
SLIDE 3

Strings

Strings are “lists of characters” (there are differences) Characters correspond (mostly) to keys on keyboard Strings are written in quotation marks Access to single characters using brackets String word = "HELLO WORLD"

word[0] is first character word[1] is second character

. . .

word[len(word)-1] is last character

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 1 / 40

slide-4
SLIDE 4

Strings – Operators

Addition Zahlen: Arithmetische Operation Listen und Strings: Zusammenfügen (bei Strings existiert kein append)

daten = [1, 4, 6] daten2 = daten + [5] satz = "Guten Tag" satz2 = satz + ", Urs" satz3 = satz2 + "."

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 2 / 40

slide-5
SLIDE 5

Strings – Operators

Addition Zahlen: Arithmetische Operation Listen und Strings: Zusammenfügen (bei Strings existiert kein append)

daten = [1, 4, 6] daten2 = daten + [5] satz = "Guten Tag" satz2 = satz + ", Urs" satz3 = satz2 + "."

Multiplikation Zahlen: Arithmetische Operation Listen und Strings: Zusammenfügen

daten = [2, 3] * 5 satz = "HA" * 3

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 2 / 40

slide-6
SLIDE 6

Characters – The Unicode Table

0–18 19–37 38–56 57–75 76–94 95–113 114–127 Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. NUL 19 DC3 38 & 57 9 76 L 95 _ 114 r 1 SOH 20 DC4 39 ’ 58 : 77 M 96 ‘ 115 s 2 STX 21 NAK 40 ( 59 ; 78 N 97 a 116 t 3 ETX 22 SYN 41 ) 60 < 79 O 98 b 117 u 4 EOT 23 ETB 42 * 61 = 80 P 99 c 118 v 5 ENQ 24 CAN 43 + 62 > 81 Q 100 d 119 w 6 ACK 25 EM 44 , 63 ? 82 R 101 e 120 x 7 BEL 26 SUB 45

  • 64

@ 83 S 102 f 121 y 8 BS 27 ESC 46 . 65 A 84 T 103 g 122 z 9 HT 28 FS 47 / 66 B 85 U 104 h 123 { 10 LF 29 GS 48 67 C 86 V 105 i 124 | 11 VT 30 RS 49 1 68 D 87 W 106 j 125 } 12 FF 31 US 50 2 69 E 88 X 107 k 126 ~ 13 CR 32 SP 51 3 70 F 89 Y 108 l 127 DEL 14 SO 33 ! 52 4 71 G 90 Z 109 m . . . 15 SI 34 "’ 53 5 72 H 91 [ 110 n 16 DLE 35 # 54 6 73 I 92 \ 111

  • 17

DC1 36 $ 55 7 74 J 93 ] 112 p 18 DC2 37 % 56 8 75 K 94 ˆ 113 q Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 3 / 40

slide-7
SLIDE 7

Characters – The Unicode Table

Use functions ord() and chr()

  • rd(x) returns position of character x in Unicode table

chr(y) returns character at position y in Unicode table

x = input("Enter a character: ") print("The character", x, "is at position", ord(x))

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 4 / 40

slide-8
SLIDE 8

Exercise – Print Characters

Write a program that

  • utputs the first 26 uppercase letters

uses a for-loop to this end Recall The letter A is located at position 65 in the Unicode table

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 5 / 40

slide-9
SLIDE 9

Exercise – Print Characters

for i in range(65,91): print(chr(i))

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 6 / 40

slide-10
SLIDE 10

Caesar Encryption

slide-11
SLIDE 11

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 7 / 40

slide-12
SLIDE 12

Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam, qui ipsorum lingua Celtae, nostra Galli appellantur. Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 7 / 40

slide-13
SLIDE 13

Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam, qui ipsorum lingua Celtae, nostra Galli appellantur. Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 7 / 40

slide-14
SLIDE 14

Insecure channel

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 7 / 40

slide-15
SLIDE 15

Symmetric Encryption

Sender Recipient

plaintext Encryption ciphertext Communication medium

(messenger, internet, . . . )

ciphertext Decryption plaintext

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 8 / 40

slide-16
SLIDE 16

Caesar Encryption

Situation Parties A and B want to communicate over an insecure channel, this time using Caesar encryption Shared key k as number between 1 and 25

A encrypts message by adding k to each character A sends encrypted message to B B decrypts message by subtracting k from each character

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 9 / 40

slide-17
SLIDE 17

Caesar Encryption

Shift characters by fixed value k by adding k

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 10 / 40

slide-18
SLIDE 18

Caesar Encryption

Shift characters by fixed value k by adding k Example A B C D E F G H I J K L M W X Y Z A B C D E F G H I N O P Q R S T U V W X Y Z J K L M N O P Q R S T U V

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 10 / 40

slide-19
SLIDE 19

Caesar Encryption

Shift characters by fixed value k by adding k Example A B C D E F G H I J K L M W X Y Z A B C D E F G H I N O P Q R S T U V W X Y Z J K L M N O P Q R S T U V Plaintext: HELLO WORLD Ciphertext: DAHHK SKNHZ

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 10 / 40

slide-20
SLIDE 20

Caesar Encryption

  • 1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z 65 66 . . . 87 88 89 90

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 11 / 40

slide-21
SLIDE 21

Caesar Encryption

  • 1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z 65 66 . . . 87 88 89 90

  • 2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z 1 . . . 22 23 24 25

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 11 / 40

slide-22
SLIDE 22

Caesar Encryption

  • 1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z 65 66 . . . 87 88 89 90

  • 2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z 1 . . . 22 23 24 25

  • 3. Now add key (for instance, 3) and compute modulo 26

A B . . . W X Y Z 3 4 . . . 25 1 2

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 11 / 40

slide-23
SLIDE 23

Caesar Encryption

  • 1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z 65 66 . . . 87 88 89 90

  • 2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z 1 . . . 22 23 24 25

  • 3. Now add key (for instance, 3) and compute modulo 26

A B . . . W X Y Z 3 4 . . . 25 1 2

  • 4. Finally add 65 to the result

A B . . . W X Y Z 68 69 . . . 90 65 66 67

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 11 / 40

slide-24
SLIDE 24

Euclidean Division (Modulo Operation)

Using “%, ” we obtain the residue of the integer division Analogously, “//” the part before the decimal point

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 12 / 40

slide-25
SLIDE 25

Euclidean Division (Modulo Operation)

Using “%, ” we obtain the residue of the integer division Analogously, “//” the part before the decimal point

10 % 3 = 1,

because 9 = 3 · 3

10 % 4 = 2,

because 8 = 4 · 2

11 // 5 = 2,

because 10 = 5 · 2

23 // 4 = 5,

because 20 = 4 · 5

12 % 3 = 0,

because 12 = 4 · 3

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 12 / 40

slide-26
SLIDE 26

Euclidean Division (Modulo Operation)

Using “%, ” we obtain the residue of the integer division Analogously, “//” the part before the decimal point

10 % 3 = 1,

because 9 = 3 · 3

10 % 4 = 2,

because 8 = 4 · 2

11 // 5 = 2,

because 10 = 5 · 2

23 // 4 = 5,

because 20 = 4 · 5

12 % 3 = 0,

because 12 = 4 · 3 If x % y == 0, x is divided by y

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 12 / 40

slide-27
SLIDE 27

Exercise – Caesar Encryption

Write a program that runs through a given string decrypts each letter with a key k tries out each key k uses the following formula e = (v − 65 − k) % 26 + 65 Decrypt the ciphertext

DLUUQLTHUKSHBAOLBYLRHYBMANPIALZZJOVNNP

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 13 / 40

slide-28
SLIDE 28

Exercise – Caesar Encryption

for k in range(0, 26): for item in ciphertext: print(chr((ord(item) - 65 - k) % 26 + 65), end="") print() for k in range(0, 26): for i in range(0, len(ciphertext)): print(chr((ord(ciphertext[i]) - 65 - k) % 26 + 65), end="") print()

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 14 / 40

slide-29
SLIDE 29

Changing the Step Size

slide-30
SLIDE 30

Loops over Lists – Larger Steps

Traverse a list with steps of length 2

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 15 / 40

slide-31
SLIDE 31

Loops over Lists – Larger Steps

Traverse a list with steps of length 2

data = [5, 1, 4, 3] for i in range(0, len(data), 2): print(data[i])

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 15 / 40

slide-32
SLIDE 32

Loops over Lists – Larger Steps

Traverse a list with steps of length 2

data = [5, 1, 4, 3] for i in range(0, len(data), 2): print(data[i])

Output All elements at even positions from 0 up to at most len(data) are output

➯ 5,4

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 15 / 40

slide-33
SLIDE 33

The Syntax of range

for i in range(start, end, step)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 16 / 40

slide-34
SLIDE 34

The Syntax of range

for i in range(start, end, step)

Iteration over all positions from start up to end-1 with step length of step

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 16 / 40

slide-35
SLIDE 35

The Syntax of range

for i in range(start, end, step)

Iteration over all positions from start up to end-1 with step length of step Shorthand notation

for i in range(start,end) ⇐ ⇒ for i in range(start,end,1)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 16 / 40

slide-36
SLIDE 36

The Syntax of range

for i in range(start, end, step)

Iteration over all positions from start up to end-1 with step length of step Shorthand notation

for i in range(start,end) ⇐ ⇒ for i in range(start,end,1)

Another shorthand notation

for i in range(end) ⇐ ⇒ for i in range(0, end)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 16 / 40

slide-37
SLIDE 37

Improvement of Caesar Encryption

Use two keys alternatingly for even and odd positions

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 17 / 40

slide-38
SLIDE 38

Improvement of Caesar Encryption

Use two keys alternatingly for even and odd positions

k = int(input("First key: ")) l = int(input("Second key: ")) x = input("Text (only uppercase, even length): ") for i in range(0, len(x), 2): print(chr((ord(text[i])-65 + k) % 26 + 65), end="") print(chr((ord(text[i+1])-65 + l) % 26 + 65), end="") print()

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 17 / 40

slide-39
SLIDE 39

Improvement of Caesar Encryption

Use two keys alternatingly for even and odd positions

k = int(input("First key: ")) l = int(input("Second key: ")) x = input("Text (only uppercase, even length): ") for i in range(0, len(x), 2): print(chr((ord(text[i])-65 + k) % 26 + 65), end="") print(chr((ord(text[i+1])-65 + l) % 26 + 65), end="") print()

Still Caesar encryption remains insecure

➯ Project 1

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 17 / 40

slide-40
SLIDE 40

Logical Values

Boolean Values and Relational Operators

slide-41
SLIDE 41

Boolean Values and Variables

Boolean expressions can take on one of two values F or T

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40

slide-42
SLIDE 42

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false” T corresponds to “true”

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40

slide-43
SLIDE 43

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false” T corresponds to “true”

George Boole [Wikimedia] Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40

slide-44
SLIDE 44

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false” T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python represent “logical values” Domain {False, True}

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40

slide-45
SLIDE 45

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false” T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python represent “logical values” Domain {False, True} Example

b = True # Variable with value True

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40

slide-46
SLIDE 46

Relational Operators

x < y

(smaller than) number type × number type → {False, True}

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-47
SLIDE 47

Relational Operators

x < y

(smaller than)

b = (1 < 3) # b =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-48
SLIDE 48

Relational Operators

x < y

(smaller than)

b = (1 < 3) # b = True

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-49
SLIDE 49

Relational Operators

x >= y

(greater than)

x = 0 b = (x >= 3) # b =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-50
SLIDE 50

Relational Operators

x >= y

(greater than)

x = 0 b = (x >= 3) # b = False

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-51
SLIDE 51

Relational Operators

x == y

(equals)

x = 4 b = (x % 3 == 1) # b =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-52
SLIDE 52

Relational Operators

x == y

(equals)

x = 4 b = (x % 3 == 1) # b = True

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-53
SLIDE 53

Relational Operators

x != y

(unequal to)

x = 1 b = (x != 2 * x - 1) # b =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-54
SLIDE 54

Relational Operators

x != y

(unequal to)

x = 1 b = (x != 2 * x - 1) # b = False

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

slide-55
SLIDE 55

Logical Values

Boolean Functions and Logical Operators

slide-56
SLIDE 56

Boolean Functions in Mathematics

Boolean function

f : {F, T}2 → {F, T}

F corresponds to “false” T corresponds to “true”

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 20 / 40

slide-57
SLIDE 57

a ∧ b

“logical and”

f : {F, T}2 → {F, T}

F corresponds to “false” T corresponds to “true” a b a ∧ b

F F F F T F T F F T T T

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 21 / 40

slide-58
SLIDE 58

Logical Operator and

a and b

(logical and)

{False, True} × {False, True} → {False, True}

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 22 / 40

slide-59
SLIDE 59

Logical Operator and

a and b

(logical and)

n = -1 p = 3 c = (n < 0) and (0 < p) # c =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 22 / 40

slide-60
SLIDE 60

Logical Operator and

a and b

(logical and)

n = -1 p = 3 c = (n < 0) and (0 < p) # c = True

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 22 / 40

slide-61
SLIDE 61

a ∨ b

“logical or”

f : {F, T}2 → {F, T}

F corresponds to “false” T corresponds to “true” a b a ∨ b

F F F F T T T F T T T T

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 23 / 40

slide-62
SLIDE 62

a ∨ b

“logical or”

f : {F, T}2 → {F, T}

F corresponds to “false” T corresponds to “true” a b a ∨ b

F F F F T T T F T T T T

The logical or is always inclusive: a or b or both

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 23 / 40

slide-63
SLIDE 63

Logical Operator or

a or b

(logical or)

{False, True} × {False, True} → {False, True}

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 24 / 40

slide-64
SLIDE 64

Logical Operator or

a or b

(logical or)

n = 1 p = 0 c = (n < 0) or (0 < p) # c =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 24 / 40

slide-65
SLIDE 65

Logical Operator or

a or b

(logical or)

n = 1 p = 0 c = (n < 0) or (0 < p) # c = False

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 24 / 40

slide-66
SLIDE 66

¬b

“logical not”

f : {F, T} → {F, T}

F corresponds to “false” T corresponds to “true” b ¬b

F T T F

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 25 / 40

slide-67
SLIDE 67

Logical Operator not

not b

(logical not)

{False, True} → {False, True}

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 26 / 40

slide-68
SLIDE 68

Logical Operator not

not b

(logical not)

n = 1 a = not (n < 0) # a =

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 26 / 40

slide-69
SLIDE 69

Logical Operator not

not b

(logical not)

n = 1 a = not (n < 0) # a = True

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 26 / 40

slide-70
SLIDE 70

Logical Values

Precedences

slide-71
SLIDE 71

Precedences

not b and a

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

slide-72
SLIDE 72

Precedences

not b and a

  • (not b) and a

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

slide-73
SLIDE 73

Precedences

a and b or c and d

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

slide-74
SLIDE 74

Precedences

a and b or c and d

  • (a and b) or (c and d)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

slide-75
SLIDE 75

Precedences

a or b and c or d

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

slide-76
SLIDE 76

Precedences

a or b and c or d

  • a or (b and c) or d

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

slide-77
SLIDE 77

Precedences

7 + x < y and y != 3 * z or not b

The unary logical operator not

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 28 / 40

slide-78
SLIDE 78

Precedences

7 + x < y and y != 3 * z or (not b)

The unary logical operator not

➯ provides a stronger binding than binary arithmetic operators

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 28 / 40

slide-79
SLIDE 79

Precedences

(7 + x) < y and y != (3 * z) or (not b)

The unary logical operator not

➯ provides a stronger binding than binary arithmetic operators ➯ These bind stronger than relational operators

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 28 / 40

slide-80
SLIDE 80

Precedences

((7 + x) < y) and (y != (3 * z)) or (not b)

The unary logical operator not

➯ provides a stronger binding than binary arithmetic operators ➯ These bind stronger than relational operators ➯ and these bind stronger than binary logical operators

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 28 / 40

slide-81
SLIDE 81

Precedences

(((7 + x) < y) and (y != (3 * z))) or (not b)

The unary logical operator not

➯ provides a stronger binding than binary arithmetic operators ➯ These bind stronger than relational operators ➯ and these bind stronger than binary logical operators

Some parentheses on the previous slides were actually redundant, but should still be used

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 28 / 40

slide-82
SLIDE 82

DeMorgan Rules

not (a and b) == (not a or not b)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 29 / 40

slide-83
SLIDE 83

DeMorgan Rules

not (a and b) == (not a or not b) not (a or b) == (not a and not b)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 29 / 40

slide-84
SLIDE 84

DeMorgan Rules

not (a and b) == (not a or not b) not (a or b) == (not a and not b)

Examples (not black and not white) == not (black or white) not (rich and beautiful) == (poor or ugly)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 29 / 40

slide-85
SLIDE 85

Application – either . . . or (XOR)

(a or b) and not (a and b)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-86
SLIDE 86

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-87
SLIDE 87

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

(a or b) and (not a or not b)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-88
SLIDE 88

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

(a or b) and (not a or not b)

a or b, and one of them not

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-89
SLIDE 89

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

(a or b) and (not a or not b)

a or b, and one of them not

not (not a and not b) and not (a and b)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-90
SLIDE 90

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

(a or b) and (not a or not b)

a or b, and one of them not

not (not a and not b) and not (a and b)

not none and not both

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-91
SLIDE 91

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

(a or b) and (not a or not b)

a or b, and one of them not

not (not a and not b) and not (a and b)

not none and not both

not ((not a and not b) or (a and b))

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-92
SLIDE 92

Application – either . . . or (XOR)

(a or b) and not (a and b)

a or b, and not both

(a or b) and (not a or not b)

a or b, and one of them not

not (not a and not b) and not (a and b)

not none and not both

not ((not a and not b) or (a and b))

not: both or none

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

slide-93
SLIDE 93

Control Structures

slide-94
SLIDE 94

Control Flow

So far. . . Up to now linear (from top to bottom)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 31 / 40

slide-95
SLIDE 95

Control Flow

So far. . . Up to now linear (from top to bottom)

for loop to repeat blocks x = int(input("Input: ")) for i in range(1, x+1): print(i*i)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 31 / 40

slide-96
SLIDE 96

Control Structures

Selection Statements

slide-97
SLIDE 97

Selection Statements

Implement branches

if statement if-else statement if-elif-else statement (later)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 32 / 40

slide-98
SLIDE 98

if Statement

if condition:

statement

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 33 / 40

slide-99
SLIDE 99

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0: print("even")

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 33 / 40

slide-100
SLIDE 100

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0: print("even")

If condition is true, then statement is executed

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 33 / 40

slide-101
SLIDE 101

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0: print("even")

If condition is true, then statement is executed statement:

arbitrary statement body of the if-Statement

condition: Boolean expression

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 33 / 40

slide-102
SLIDE 102

if-else Statement

if condition:

statement1

else:

statement2

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 34 / 40

slide-103
SLIDE 103

if-else Statement

if condition:

statement1

else:

statement2

x = int(input("Input: ")) if x % 2 == 0: print("even") else: print("odd")

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 34 / 40

slide-104
SLIDE 104

if-else Statement

if condition:

statement1

else:

statement2

x = int(input("Input: ")) if x % 2 == 0: print("even") else: print("odd")

If condition is true, then statement1 is executed,

  • therwise statement2 is executed

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 34 / 40

slide-105
SLIDE 105

if-else Statement

if condition:

statement1

else:

statement2

x = int(input("Input: ")) if x % 2 == 0: print("even") else: print("odd")

If condition is true, then statement1 is executed,

  • therwise statement2 is executed

condition: Boolean expression statement1: body of the if-branch statement2: body of the else-branch

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 34 / 40

slide-106
SLIDE 106

Layout

x = int(input("Input: ")) if x % 2 == 0: print("even") else: print("odd")

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 35 / 40

slide-107
SLIDE 107

Layout

x = int(input("Input: ")) if x % 2 == 0: print("even") else: print("odd")

Indentation Indentation

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 35 / 40

slide-108
SLIDE 108

Control Structures

while Loops

slide-109
SLIDE 109

while Loops

while condition:

statement statement:

arbitrary statement body of the while loop

condition: Boolean expression

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 36 / 40

slide-110
SLIDE 110

while Loops

while condition:

statement

Indentation

statement:

arbitrary statement body of the while loop

condition: Boolean expression

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 36 / 40

slide-111
SLIDE 111

while Loops

while condition:

statement condition is evaluated True: iteration starts

statement is executed

False: while loop ends

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 37 / 40

slide-112
SLIDE 112

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-113
SLIDE 113

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-114
SLIDE 114

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1 i <= 2?

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-115
SLIDE 115

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-116
SLIDE 116

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-117
SLIDE 117

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-118
SLIDE 118

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2 i <= 2?

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-119
SLIDE 119

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

true

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-120
SLIDE 120

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

true

s = 3

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-121
SLIDE 121

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

true

s = 3 i = 3

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-122
SLIDE 122

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

true

s = 3 i = 3 i <= 2?

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-123
SLIDE 123

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

true

s = 3 i = 3

false

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-124
SLIDE 124

while Loops

s = 0 i = 1 while i <= 2: s = s + i i = i + 1 i

condition

s i = 1

true

s = 1 i = 2

true

s = 3 i = 3

false

s = 3

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 38 / 40

slide-125
SLIDE 125

Incrementation of Variables

Use simplified syntax for changing values of variables

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-126
SLIDE 126

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-127
SLIDE 127

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1 n = n + i is written as n += i

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-128
SLIDE 128

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1 n = n + i is written as n += i n = n - 15 is written as n -= 15

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-129
SLIDE 129

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1 n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-130
SLIDE 130

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1 n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j n = n ** 4 is written as n **= 4

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-131
SLIDE 131

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1 n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j n = n ** 4 is written as n **= 4

. . .

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 39 / 40

slide-132
SLIDE 132

The Jump Statements break

break

Immediately leave the enclosing loop Useful in order to be able to break a loop “in the middle”

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 40 / 40

slide-133
SLIDE 133

The Jump Statements break

break

Immediately leave the enclosing loop Useful in order to be able to break a loop “in the middle”

s = 0 while True: x = int(input("Enter a positive number, abort with 0: ")) if x == 0: break s += x print(s)

Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 40 / 40

slide-134
SLIDE 134

Thanks for your attention