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 8, 2020 Output The Function print() Output on screen with print() Digital Medicine I Lists, strings, loops


slide-1
SLIDE 1

Departement Informatik

Digital Medicine I

Lists, strings, loops

Hans-Joachim Böckenhauer Dennis Komm

Autumn 2020 – October 8, 2020

slide-2
SLIDE 2

Output

slide-3
SLIDE 3

The Function print()

Output on screen with print()

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

slide-4
SLIDE 4

The Function print()

Output on screen with print() Default: Linebreak after output Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

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

slide-5
SLIDE 5

The Function print()

Output on screen with print() Default: Linebreak after output Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

Multiple parameters also separated by comma

print("Two times the value of", x, "is", 2 * x)

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

slide-6
SLIDE 6

The Function print()

Output on screen with print() Default: Linebreak after output Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

Multiple parameters also separated by comma

print("Two times the value of", x, "is", 2 * x)

Default: Whitespace between strings Whitespace can be replaced by sep

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

slide-7
SLIDE 7

User Input

slide-8
SLIDE 8

User Input

Variables Variables are “containers” for values So far values have been fixed in program

name = "Brunhold" print("Hello", name)

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

slide-9
SLIDE 9

User Input

Variables Variables are “containers” for values So far values have been fixed in program

name = "Brunhold" print("Hello", name)

In the real world, values are mostly

entered by the user read in from file / data base

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

slide-10
SLIDE 10

User Input

Variables Variables are “containers” for values So far values have been fixed in program

name = "Brunhold" print("Hello", name)

In the real world, values are mostly

entered by the user read in from file / data base (later)

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

slide-11
SLIDE 11

Celsius to Fahrenheit Calculator

User input with function input()

name = input("Enter your name: ") print("Hello", name)

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

slide-12
SLIDE 12

Celsius to Fahrenheit Calculator

User input with function input()

name = input("Enter your name: ") print("Hello", name)

Attention Input is string (possibly made of digits) and no number

x = input("Enter a number: ")

  • utput = 3 * x

print(output) # String concatenation instead of multiplication

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

slide-13
SLIDE 13

Celsius to Fahrenheit Calculator

To get a number, the input has to be converted using the function int()

x = input("Enter a temperature in degree Celsius: ") celsius = int(x) fahrenheit = 9 * celsius / 5 + 32 print("The temperature in degree Fahrenheit is", fahrenheit)

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

slide-14
SLIDE 14

Celsius to Fahrenheit Calculator

To get a number, the input has to be converted using the function int()

x = input("Enter a temperature in degree Celsius: ") celsius = int(x) fahrenheit = 9 * celsius / 5 + 32 print("The temperature in degree Fahrenheit is", fahrenheit)

  • r shorter. . .

celsius = int(input("Enter a temperature in degree Celsius: ")) fahrenheit = 9 * celsius / 5 + 32 print("The temperature in degree Fahrenheit is", fahrenheit)

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

slide-15
SLIDE 15

Simple Loops

slide-16
SLIDE 16

for-Loops

Repeat a statement block a specific number of times

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

slide-17
SLIDE 17

for-Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts

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

slide-18
SLIDE 18

for-Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts A control variable, which is defined in the loop head

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

slide-19
SLIDE 19

for-Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts A control variable, which is defined in the loop head Additionally, a range is specified

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

slide-20
SLIDE 20

for-Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts A control variable, which is defined in the loop head Additionally, a range is specified In the loop body, the control variable consecutively takes all values in the provided range

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

slide-21
SLIDE 21

for-Loops

for i in range(1, 4): print(”Test”) print(i)

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

slide-22
SLIDE 22

for-Loops

for i in range(1, 4): print(”Test”) print(i)

Loop head

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

slide-23
SLIDE 23

for-Loops

for i in range(1, 4): print("Test") print(i)

Loop body (indented)

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

slide-24
SLIDE 24

for-Loops

for i in range(1, 4): print(”Test”) print(i)

Control variable

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

slide-25
SLIDE 25

for-Loops

for i in range(1, 4): print(”Test”) print(i)

Range

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

slide-26
SLIDE 26

for-Loops

for i in range(1, 4): print(”Test”) print(i)

Range

Attention Last value of i is 3 and not 4

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

slide-27
SLIDE 27

for-Loops

for i in range(1, 4): print("Test") print(i)

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

slide-28
SLIDE 28

for-Loops

for i in range(1, 4): print("Test") print(i)

results in. . .

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

slide-29
SLIDE 29

for-Loops

for i in range(1, 4): print("Test") print(i)

results in. . .

print("Test") print(1)

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

slide-30
SLIDE 30

for-Loops

for i in range(1, 4): print("Test") print(i)

results in. . .

print("Test") print(1) print("Test") print(2)

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

slide-31
SLIDE 31

for-Loops

for i in range(1, 4): print("Test") print(i)

results in. . .

print("Test") print(1) print("Test") print(2) print("Test") print(3)

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

slide-32
SLIDE 32

Exercise – Square Numbers

Write a program that gets an integer from the user stores the value in a variable x

  • utputs the first x square numbers

(starting with 1)

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

slide-33
SLIDE 33

Exercise – Square Numbers

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 9 / 37

slide-34
SLIDE 34

Nested Loops

slide-35
SLIDE 35

Nested Loops

Program

for i in range(0, 10): for j in range(0, 10): print(j, end=" ") print()

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

slide-36
SLIDE 36

Nested Loops

Program

for i in range(0, 10): for j in range(0, 10): print(j, end=" ") print()

Output 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

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

slide-37
SLIDE 37

Exercise – Number Triangle

Write a program that generates the following output: 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9 4 5 6 7 8 9 5 6 7 8 9 6 7 8 9 7 8 9 8 9 9

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

slide-38
SLIDE 38

Exercise – Number Triangle

for i in range(0, 10): for j in range(i, 10): print(j, end=" ") print()

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

slide-39
SLIDE 39

Lists

slide-40
SLIDE 40

Lists

Simple access to related data

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

slide-41
SLIDE 41

Lists

Simple access to related data Example

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

slide-42
SLIDE 42

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

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

slide-43
SLIDE 43

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0 Access to single element using brackets

data[0] = 5

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

slide-44
SLIDE 44

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0 Access to single element using brackets

data[0] = 5

Length of the list = Number of elements

len(data) = 4

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

slide-45
SLIDE 45

Loops over Lists

Output all elements of list

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

slide-46
SLIDE 46

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3] for item in data: print(item)

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

slide-47
SLIDE 47

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3] for item in data: print(item) Takes values of all elements in list

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

slide-48
SLIDE 48

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3] for item in data: print(item) Takes values of all elements in list

  • r alternatively. . .

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

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

slide-49
SLIDE 49

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3] for item in data: print(item) Takes values of all elements in list

  • r alternatively. . .

data = [5, 1, 4, 3] for i in range(0, len(data)): print(data[i]) Takes values of all indices of elements in list

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

slide-50
SLIDE 50

Loops over Lists – The Function reversed()

Output all elements of list backwards

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

slide-51
SLIDE 51

Loops over Lists – The Function reversed()

Output all elements of list backwards

data = [6, 7, 5, 1] for item in reversed(data): print(item, end=" ")

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

slide-52
SLIDE 52

Loops over Lists – The Function reversed()

Output all elements of list backwards

data = [6, 7, 5, 1] for item in reversed(data): print(item, end=" ")

Result

1 5 7 6

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

slide-53
SLIDE 53

Simple Initialization

Initialize list with same value in all cells

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

slide-54
SLIDE 54

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data)

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

slide-55
SLIDE 55

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data) data = [0, 0, 0, 0] * 250 print(data)

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

slide-56
SLIDE 56

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data) data = [0, 0, 0, 0] * 250 print(data)

Result

[0, 0, 0, 0, 0, 0, 0, ..., 0]

(1000 zeros)

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

slide-57
SLIDE 57

The Function append()

Add element to end of a list

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

slide-58
SLIDE 58

The Function append()

Add element to end of a list Example List data

data.append(5) inserts 5 at the end

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

slide-59
SLIDE 59

The Function append()

Add element to end of a list Example List data

data.append(5) inserts 5 at the end

data = [1, 4, 8] data.append(9) data.append(14) print(data)

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

slide-60
SLIDE 60

Exercise – Initialize List

Write a program that gets an integer from the user stores the value in a variable x initializes a list with the first x even numbers (starting at 0)

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

slide-61
SLIDE 61

Exercise – Initialize List

x = int(input("Input: ")) data = [] for i in range(0, x): data.append(2 * i) print(data)

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

slide-62
SLIDE 62

Merging lists

Merge lists using +

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

slide-63
SLIDE 63

Merging lists

Merge lists using + Example List data

data + [1, 2] adds elements 1 and two at the end

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

slide-64
SLIDE 64

Merging lists

Merge lists using + Example List data

data + [1, 2] adds elements 1 and two at the end

data = [4, 6, 7] data2 = data + [9] data2 = [1, 3] + data2 + [12, 14] print(data2)

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

slide-65
SLIDE 65

Strings

slide-66
SLIDE 66

Strings

Strings are “lists of characters” (there are differences)

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

slide-67
SLIDE 67

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

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

slide-68
SLIDE 68

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 21 / 37

slide-69
SLIDE 69

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 22 / 37

slide-70
SLIDE 70

Characters – The Unicode Table

Use functions ord() and chr()

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

slide-71
SLIDE 71

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

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

slide-72
SLIDE 72

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 23 / 37

slide-73
SLIDE 73

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 24 / 37

slide-74
SLIDE 74

Exercise – Print Characters

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

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

slide-75
SLIDE 75

Caesar Encryption

slide-76
SLIDE 76

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

slide-77
SLIDE 77

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 26 / 37

slide-78
SLIDE 78

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 26 / 37

slide-79
SLIDE 79

Insecure channel

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

slide-80
SLIDE 80

Symmetric Encryption

Situation

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

slide-81
SLIDE 81

Symmetric Encryption

Situation Two parties A and B want to communicate through an insecure channel Shared key k

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

slide-82
SLIDE 82

Symmetric Encryption

Situation Two parties A and B want to communicate through an insecure channel Shared key k

A encrypts message with k A sends encrypted message to B

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

slide-83
SLIDE 83

Symmetric Encryption

Situation Two parties A and B want to communicate through an insecure channel Shared key k

A encrypts message with k A sends encrypted message to B B decrypts message with k

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

slide-84
SLIDE 84

Symmetric Encryption

Situation Two parties A and B want to communicate through an insecure channel Shared key k

A encrypts message with k A sends encrypted message to B B decrypts message with k A is called sender B is called recipient

Symmetric: Same key for encryption and decryption

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

slide-85
SLIDE 85

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 28 / 37

slide-86
SLIDE 86

Caesar Encryption

Situation

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

slide-87
SLIDE 87

Caesar Encryption

Situation Parties A and B want to communicate over an insecure channel, this time using Caesar encryption

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

slide-88
SLIDE 88

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

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

slide-89
SLIDE 89

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

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

slide-90
SLIDE 90

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

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

slide-91
SLIDE 91

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 29 / 37

slide-92
SLIDE 92

Caesar Encryption

Shift characters by fixed value k by adding k

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

slide-93
SLIDE 93

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 30 / 37

slide-94
SLIDE 94

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 30 / 37

slide-95
SLIDE 95

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 31 / 37

slide-96
SLIDE 96

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 31 / 37

slide-97
SLIDE 97

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 31 / 37

slide-98
SLIDE 98

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 31 / 37

slide-99
SLIDE 99

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 32 / 37

slide-100
SLIDE 100

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 32 / 37

slide-101
SLIDE 101

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 32 / 37

slide-102
SLIDE 102

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

DLUUQLTHUKSHBADBOBBYBMANPIALZZJOVNNP

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

slide-103
SLIDE 103

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 34 / 37

slide-104
SLIDE 104

Changing the Step Size

slide-105
SLIDE 105

Loops over Lists – Larger Steps

Traverse a list with steps of length 2

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

slide-106
SLIDE 106

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 35 / 37

slide-107
SLIDE 107

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 35 / 37

slide-108
SLIDE 108

The Syntax of range

for i in range(start, end, step)

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

slide-109
SLIDE 109

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 36 / 37

slide-110
SLIDE 110

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 36 / 37

slide-111
SLIDE 111

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 36 / 37

slide-112
SLIDE 112

Improvement of Caesar Encryption

Use two keys alternatingly for even and odd positions

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

slide-113
SLIDE 113

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 37 / 37

slide-114
SLIDE 114

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 37 / 37

slide-115
SLIDE 115

Thanks for your attention