Departement Informatik Strings Digital Medicine I Lists, strings, loops Repetition Hans-Joachim Böckenhauer Dennis Komm Autumn 2020 – October 15, 2020 Strings Strings – Operators Addition Strings are “lists of characters” (there are differences) Zahlen: Arithmetische Operation Characters correspond (mostly) to keys on keyboard Listen und Strings: Zusammenfügen (bei Strings existiert kein append ) Strings are written in quotation marks daten = [1, 4, 6] daten2 = daten + [5] Access to single characters using brackets satz = "Guten Tag" satz2 = satz + ", Urs" satz3 = satz2 + "." String word = "HELLO WORLD" Multiplikation word[0] is first character Zahlen: Arithmetische Operation word[1] is second character Listen und Strings: Zusammenfügen . . . daten = [2, 3] * 5 word[len(word)-1] is last character satz = "HA" * 3 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 1 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 2 / 40
Characters – The Unicode Table 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. 0 NUL 19 DC3 38 & 57 9 76 L 95 _ 114 r Use functions ord() and chr() 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 ord(x) returns position of character x in Unicode table 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 chr(y) returns character at position y in Unicode table 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 0 67 C 86 V 105 i 124 | 11 VT 30 RS 49 1 68 D 87 W 106 j 125 } x = input("Enter a character: ") 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 print("The character", x, "is at position", ord(x)) 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 o 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 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 4 / 40 Exercise – Print Characters Exercise – Print Characters Write a program that outputs the first 26 uppercase letters uses a for -loop to this end for i in range(65,91): print(chr(i)) 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 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 6 / 40
Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam, qui ipsorum lingua Celtae, Insecure channel nostra Galli appellantur. Caesar Encryption Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 7 / 40 Symmetric Encryption Caesar Encryption Recipient Sender Situation Parties A and B want to communicate over an insecure channel, this time plaintext plaintext using Caesar encryption Encryption Decryption Shared key k as number between 1 and 25 A encrypts message by adding k to each character Communication ciphertext medium ciphertext A sends encrypted message to B (messenger, internet, . . . ) B decrypts message by subtracting k from each character Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 8 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 9 / 40
Caesar Encryption Caesar Encryption 1. Entered letter is Unicode character between A and Z Shift characters by fixed value k by adding k A B . . . W X Y Z 65 66 . . . 87 88 89 90 Example 2. Subtract 65 so that the result is between 0 and 25 A B C D E F G H I J K L M A B . . . W X Y Z 0 1 . . . 22 23 24 25 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 3. Now add key (for instance, 3 ) and compute modulo 26 A B . . . W X Y Z J K L M N O P Q R S T U V 3 4 . . . 25 0 1 2 4. Finally add 65 to the result Plaintext: HELLO WORLD A B . . . W X Y Z Ciphertext: DAHHK SKNHZ 68 69 . . . 90 65 66 67 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 10 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 11 / 40 Euclidean Division (Modulo Operation) Exercise – Caesar Encryption Write a program that Using “ % , ” we obtain the residue of the integer division runs through a given string Analogously, “ // ” the part before the decimal point decrypts each letter with a key k because 9 = 3 · 3 10 % 3 = 1 , tries out each key k 10 % 4 = 2 , because 8 = 4 · 2 uses the following formula because 10 = 5 · 2 11 // 5 = 2 , e = ( v − 65 − k ) % 26 + 65 because 20 = 4 · 5 23 // 4 = 5 , 12 % 3 = 0 , because 12 = 4 · 3 Decrypt the ciphertext DLUUQLTHUKSHBAOLBYLRHYBMANPIALZZJOVNNP If x % y == 0 , x is divided by y Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 12 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 13 / 40
Exercise – Caesar Encryption for k in range(0, 26): for item in ciphertext: print(chr((ord(item) - 65 - k) % 26 + 65), end="") Changing the Step Size 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 Loops over Lists – Larger Steps The Syntax of range Traverse a list with steps of length 2 for i in range(start, end, step) Iteration over all positions from start up to end-1 with step length of step data = [5, 1, 4, 3] for i in range(0, len(data), 2): print(data[i]) Shorthand notation for i in range(start,end) ⇐ ⇒ for i in range(start,end,1) Output All elements at even positions from 0 up to at most len(data) are output Another shorthand notation ➯ 5,4 for i in range(end) ⇐ ⇒ for i in range(0, end) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 15 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 16 / 40
Improvement of Caesar Encryption Use two keys alternatingly for even and odd positions Logical Values k = int(input("First key: ")) l = int(input("Second key: ")) Boolean Values and Relational Operators 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 Boolean Values and Variables Relational Operators Boolean expressions can take on one of two values F or T (smaller than) x < y F corresponds to “false” (greater than) x >= y T corresponds to “true” (equals) x == y (unequal to) x != y Boolean variables in Python represent “logical values” Domain { False, True } number type × number type → {False, True} Example b = True # Variable with value True George Boole [Wikimedia] Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40
Boolean Functions in Mathematics Boolean function Logical Values f : { F , T } 2 → { F , T } Boolean Functions and Logical Operators F corresponds to “false” T corresponds to “true” Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 20 / 40 a ∧ b Logical Operator and “logical and” a ∧ b a b a and b (logical and) f : { F , T } 2 → { F , T } F F F F T F {False, True} × {False, True} → {False, True} T F F F corresponds to “false” T corresponds to “true” T T T n = -1 p = 3 c = (n < 0) and (0 < p) # c = True Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 21 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 22 / 40
a ∨ b Logical Operator or a b a ∨ b “logical or” a or b (logical or) f : { F , T } 2 → { F , T } F F F F T T {False, True} × {False, True} → {False, True} F corresponds to “false” T F T T corresponds to “true” T T T n = 1 p = 0 The logical or is always inclusive : a or b or both c = (n < 0) or (0 < p) # c = False Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 23 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 24 / 40 ¬ b Logical Operator not “logical not” f : { F , T } → { F , T } not b (logical not) ¬ b b F T {False, True} → {False, True} F corresponds to “false” T F T corresponds to “true” n = 1 a = not (n < 0) # a = True Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 25 / 40 Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 26 / 40
Recommend
More recommend