digital medicine i
play

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


  1. 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

  2. The Syntax of range for i in range(start, end, step) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 16 / 40

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Logical Values Boolean Values and Relational Operators

  10. 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

  11. 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

  12. 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

  13. Boolean Values and Variables Boolean expressions can take on one of two values F or T F corresponds to “false” T corresponds to “true” Boolean variables in Python represent “logical values” Domain { False, True } George Boole [Wikimedia] Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 18 / 40

  14. Boolean Values and Variables Boolean expressions can take on one of two values F or T F corresponds to “false” T corresponds to “true” Boolean variables in Python represent “logical values” Domain { 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

  15. Relational Operators (smaller than) x < y number type × number type → {False, True} Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  16. Relational Operators (smaller than) x < y b = (1 < 3) # b = Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  17. Relational Operators (smaller than) x < y b = (1 < 3) # b = True Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  18. Relational Operators (greater than) x >= y x = 0 b = (x >= 3) # b = Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  19. Relational Operators (greater than) x >= y x = 0 b = (x >= 3) # b = False Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  20. Relational Operators (equals) x == y x = 4 b = (x % 3 == 1) # b = Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  21. Relational Operators (equals) x == y x = 4 b = (x % 3 == 1) # b = True Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  22. Relational Operators (unequal to) x != y x = 1 b = (x != 2 * x - 1) # b = Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  23. Relational Operators (unequal to) x != y x = 1 b = (x != 2 * x - 1) # b = False Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 19 / 40

  24. Logical Values Boolean Functions and Logical Operators

  25. 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

  26. a ∧ b “logical and” a ∧ b a b f : { F , T } 2 → { F , T } F F F F T F T F F F corresponds to “false” T corresponds to “true” T T T Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 21 / 40

  27. Logical Operator and (logical and) a and b {False, True} × {False, True} → {False, True} Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 22 / 40

  28. Logical Operator and (logical and) a and b n = -1 p = 3 c = (n < 0) and (0 < p) # c = Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 22 / 40

  29. Logical Operator and (logical and) a and b 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

  30. a ∨ b a ∨ b a b “logical or” f : { F , T } 2 → { F , T } F F F F T T F corresponds to “false” T F T T corresponds to “true” T T T Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 23 / 40

  31. a ∨ b a ∨ b a b “logical or” f : { F , T } 2 → { F , T } F F F F T T F corresponds to “false” T F T T corresponds to “true” 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

  32. Logical Operator or (logical or) a or b {False, True} × {False, True} → {False, True} Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 24 / 40

  33. Logical Operator or (logical or) a or b n = 1 p = 0 c = (n < 0) or (0 < p) # c = Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 24 / 40

  34. Logical Operator or (logical or) a or b 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

  35. ¬ b “logical not” f : { F , T } → { F , T } ¬ b b F T F corresponds to “false” T F T corresponds to “true” Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 25 / 40

  36. Logical Operator not not b (logical not) {False, True} → {False, True} Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 26 / 40

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

  38. 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

  39. Logical Values Precedences

  40. Precedences not b and a Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

  41. Precedences not b and a � (not b) and a Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

  42. Precedences a and b or c and d Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

  43. 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

  44. Precedences a or b and c or d Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 27 / 40

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

  51. DeMorgan Rules not (a and b) == (not a or not b) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 29 / 40

  52. 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

  53. 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

  54. 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

  55. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

  56. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) (a or b) and (not a or not b) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

  57. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) a or b, and one of them not (a or b) and (not a or not b) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

  58. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) a or b, and one of them not (a or b) and (not a or not b) not (not a and not b) and not (a and b) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

  59. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) a or b, and one of them not (a or b) and (not a or not b) not none and not both not (not a and not b) and not (a and b) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

  60. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) a or b, and one of them not (a or b) and (not a or not b) not none and not both not (not a and not b) and not (a and b) not ((not a and not b) or (a and b)) Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 30 / 40

  61. Application – either . . . or (XOR) a or b, and not both (a or b) and not (a and b) a or b, and one of them not (a or b) and (not a or not b) not none and not both not (not a and not b) and not (a and b) 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

  62. Control Structures

  63. 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

  64. 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

  65. Control Structures Selection Statements

  66. 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

  67. if Statement if condition : statement Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 33 / 40

  68. 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

  69. if Statement if condition : If condition is true, statement then statement is executed x = int(input("Input: ")) if x % 2 == 0: print("even") Digital Medicine I – Lists, strings, loops Autumn 2020 Böckenhauer, Komm 33 / 40

Recommend


More recommend