PyUGAT

Treffen vom 10. 07. 2019

Agenda

   1 >>> seq = [1,2,3,4,5,6,7]
   2 >>> val1, *_, num2, num3 = seq
   3 >>> _
   4 [2, 3, 4, 5]
   5 >>> val1
   6 1
   7 >>> num2
   8 6
   9 >>> num3
  10 7
  11 
  12 >>> 3 + 6
  13 9
  14 >>> _
  15 9
  16 # What does _ stand for?
  17 
  18 >>> import math
  19 >>> math.sin
  20 <built-in function sin>
  21 >>> _
  22 <built-in function sin>
  23 >>> _(5)
  24 -0.9589242746631385
  25 >>> _(5)
  26 Traceback (most recent call last):
  27   File "<stdin>", line 1, in <module>
  28 TypeError: 'float' object is not callable
  29 >>> -0.9589242746631385(5)
  30 Traceback (most recent call last):
  31   File "<stdin>", line 1, in <module>
  32 TypeError: 'float' object is not callable
  33 >>> _
  34 -0.9589242746631385
  35 >>> _ * 2
  36 -1.917848549326277
  37 >>> _ * 2
  38 -3.835697098652554
  39 >>> _ * 2
  40 -7.671394197305108
  41 >>> _ * 2
  42 -15.342788394610215
  43 >>> _ * 2
  44 -30.68557678922043
  45 >>> _ * 2
  46 -61.37115357844086
  47 >>> _ * 2
  48 -122.74230715688172
  49 >>> _
  50 -122.74230715688172
  51 >>> _ = 42
  52 >>> 3 * 9
  53 27
  54 >>> _
  55 42
  56 >>> del _
  57 >>> _
  58 42
  59 >>> 3 * 9
  60 27
  61 >>> _
  62 27
  63 
  64 >>> math.sin(2*math.pi)
  65 -2.4492935982947064e-16
  66 >>> math.isclose(math.sin(2*math.pi), 0)
  67 False
  68 >>> math.isclose(math.sin(2*math.pi), 0, 0.0000001)
  69 Traceback (most recent call last):
  70   File "<stdin>", line 1, in <module>
  71 TypeError: Function takes at most 2 positional arguments (3 given)
  72 >>> math.isclose(math.sin(2*math.pi), 0.000001)
  73 False
  74 >>> math.isclose(math.sin(2*math.pi), -0.000001)
  75 False
  76 >>> math.isclose(math.sin(2*math.pi), 1)
  77 False
  78 >>> math.sin(0)
  79 0.0
  80 >>> math.sin(math.pi)
  81 1.2246467991473532e-16
  82 >>> math.sin(math.pi*2)
  83 -2.4492935982947064e-16
  84 # is math.sin(2*math.pi) close to zero? Lack of definition what is interpreted as close....
  85 
  86 >>> def print_x_is_4():
  87 ...     x = 4
  88 ...     print(x)
  89 ... 
  90 >>> x = 3
  91 >>> x
  92 3
  93 >>> print_x_is_4()
  94 4
  95 
  96 >>> def print_x():
  97 ...     print(x)
  98 ... 
  99 >>> print_x()
 100 3
 101 
 102 >>> def print_x_plus_x():
 103 ...     x += x
 104 ...     print(x)
 105 ... 
 106 >>> print_x_plus_x()
 107 Traceback (most recent call last):
 108   File "<stdin>", line 1, in <module>
 109   File "<stdin>", line 2, in print_x_plus_x
 110 UnboundLocalError: local variable 'x' referenced before assignment
 111 >>> ergebnis = x + x
 112 >>> x = ergebnis
 113 >>> x
 114 6
 115 # x is defined in local scope
 116 
 117 >>> def print_local_scope():
 118 ...     x = 4
 119 ...     x += x
 120 ...     print(x)
 121 ... 
 122 >>> print_local_scope()
 123 8
 124 >>> x
 125 6
 126 # operation in local scope has no effect to global scope
 127 
 128 >>> def print_global_x():
 129 ...     global x
 130 ...     x += x
 131 ...     print(x)
 132 ... 
 133 >>> x
 134 6
 135 >>> print_global_x()
 136 12
 137 >>> x
 138 12
 139 # x is defined in global scope out of the function
 140 
 141 >>> def draussen():
 142 ...     x = 8
 143 ...     def drinnen():
 144 ...         nonlocal x
 145 ...         x += x
 146 ...         print("drinnen:", x)
 147 ...     print("vorher:", x)
 148 ...     drinnen()
 149 ...     print("nachher:", x)
 150 ... 
 151 >>> draussen()
 152 vorher: 8
 153 drinnen: 16
 154 nachher: 16
 155 >>> x
 156 12
 157 >>> drinnen()
 158 Traceback (most recent call last):
 159   File "<stdin>", line 1, in <module>
 160 NameError: name 'drinnen' is not defined
 161 # function drinnen() only exists in the function draussen()


CategoryShownotes

PyUGAT: Treffen/2019-07-Studygroup (last edited 2020-10-18 21:26:55 by hop)