|
| 1 | +import fractions |
| 2 | +import math |
| 3 | +import sys |
| 4 | +import cmath |
| 5 | + |
| 6 | + |
| 7 | +def boolean(): |
| 8 | + """ |
| 9 | + Evaluate boolean |
| 10 | + """ |
| 11 | + print('Evaluate boolean') |
| 12 | + size = 0 |
| 13 | + # The result of the expression size < 0 is always a boolean |
| 14 | + print("size > 0 is", size > 0) |
| 15 | + print("size == 0 is", size == 0) |
| 16 | + print('Boolean implicitly converted to int, 42 + True =', 42 + True) |
| 17 | + # False args, or args converted to False: None, 0, [], (), {}, set() |
| 18 | + |
| 19 | + |
| 20 | +def walrus(): |
| 21 | + """ |
| 22 | + The purpose of the walrus operator is to consolidate an assignment statement |
| 23 | + and a boolean expression, assigning value to variable within the expression |
| 24 | + Operator has analog in C |
| 25 | + if (int i = length()) |
| 26 | + Parenthesis are critical for to evaluate properly! |
| 27 | + """ |
| 28 | + # before |
| 29 | + my_list = [1, 2, 3, 4, 5] |
| 30 | + print("BEFORE: if len(my_list) > 3:") |
| 31 | + if len(my_list) > 3: |
| 32 | + n = len(my_list) |
| 33 | + print(f"The list is too long with {n} elements") |
| 34 | + |
| 35 | + # after |
| 36 | + print("AFTER: if (n := len(my_list)) > 3:") |
| 37 | + if (n := len(my_list)) > 3: |
| 38 | + print(f"The list is too long with {n} elements") |
| 39 | + |
| 40 | + |
| 41 | +def numbers(): |
| 42 | + """ |
| 43 | + Evaluate numbers |
| 44 | +
|
| 45 | + Python is dynamically typed. Using return or declaration types is just a hint |
| 46 | + Hints are not: |
| 47 | + - Not static types. The language is not enforcing anything |
| 48 | + - Not Performance boosting |
| 49 | + """ |
| 50 | + print('Evaluate numbers') |
| 51 | + |
| 52 | + # You can use the type() function to check the type of any value or variable |
| 53 | + # Similarly, you can use the isinstance() function |
| 54 | + # to check whether a value or variable is of a given type |
| 55 | + print("type(1) is", type(1)) |
| 56 | + print("isinstance(1, int) is", isinstance(1, int)) |
| 57 | + print("isinstance(1.0, float) is", isinstance(1.0, float)) |
| 58 | + |
| 59 | + # Adding an int to a float yields a float |
| 60 | + print("type(1+1) is", type(1+1)) |
| 61 | + print("type(1+1.0) is", type(1+1.0)) |
| 62 | + |
| 63 | + # interesting twist - 'type' itself is an object |
| 64 | + print("type(type(1)) is ", type(type(1))) |
| 65 | + |
| 66 | + # The value sys.maxsize reports the platform pointer size |
| 67 | + # Any integer too big to fit in 64 bits is handled in software |
| 68 | + print("sys.maxsize=", sys.maxsize) |
| 69 | + |
| 70 | + # infinity |
| 71 | + print('float("inf") = {}'.format(float("inf"))) |
| 72 | + |
| 73 | + # combined compare |
| 74 | + x = math.pi |
| 75 | + if 0 < x < 10: |
| 76 | + print("x between 0 and 10") |
| 77 | + |
| 78 | + |
| 79 | +def complex_num(): |
| 80 | + c1 = 2 + 7j |
| 81 | + c2 = complex(3, 6) |
| 82 | + print("2 + 7j = {}; type = {}".format(c1, type(c1))) |
| 83 | + print("complex(3, 6) = {}; type = {}".format(c2, type(c2))) |
| 84 | + |
| 85 | + print("Addition of two complex number =", c1 + c2) |
| 86 | + print("Subtraction of two complex number =", c1 - c2) |
| 87 | + |
| 88 | + # complex numbers don’t support comparison operators like <, >, <=, => |
| 89 | + # and it will raise TypeError message |
| 90 | + |
| 91 | + # Phase is angle between the real axis and the vector representing the imaginary part |
| 92 | + print('Phase = {} radians'.format(cmath.phase(c1))) |
| 93 | + |
| 94 | + |
| 95 | +def conversions(): |
| 96 | + """ |
| 97 | + Converts int<->float |
| 98 | + """ |
| 99 | + print('Convert numbers') |
| 100 | + # The int() function will truncate, not round |
| 101 | + # The int() function truncates negative numbers towards 0. |
| 102 | + # I.e. it’s a true truncate function, not a floor function |
| 103 | + # Floating point numbers are accurate to 15 decimal places |
| 104 | + # Integers can be arbitrarily large |
| 105 | + |
| 106 | + # See strong typing |
| 107 | + a: float = 2.9 |
| 108 | + print("int(2.9) =", int(a)) |
| 109 | + print("int(-2.9) =", int(-a)) |
| 110 | + very_long_int = 9999999999999999999 |
| 111 | + print("type(9999999999999999999) is", type(very_long_int)) |
| 112 | + |
| 113 | + |
| 114 | +def operations(): |
| 115 | + """ |
| 116 | + Simple math operations |
| 117 | + """ |
| 118 | + print('Simple operations') |
| 119 | + # The / operator performs floating point division |
| 120 | + # The // operator performs a quirky kind of integer division |
| 121 | + # When the result is positive, you can think of it as truncating (not rounding) |
| 122 | + print("11/2 =", 11/2) |
| 123 | + print("11//2 =", 11//2) |
| 124 | + |
| 125 | + # When integer-dividing negative numbers, the // operator rounds up to the nearest integer |
| 126 | + print("-11//2 =", -11//2) |
| 127 | + |
| 128 | + # The ** operator means raised to the power of |
| 129 | + print("4**2 =", 4**2) |
| 130 | + |
| 131 | + # The % operator gives the remainder after performing integer division |
| 132 | + print("11 % 2 =", 11 % 2) |
| 133 | + |
| 134 | + |
| 135 | +def fraction(): |
| 136 | + """ |
| 137 | + Simple fraction |
| 138 | + """ |
| 139 | + print('Simple fraction') |
| 140 | + |
| 141 | + # To start using fraction, import the fraction module |
| 142 | + x = fractions.Fraction(1, 3) |
| 143 | + print("Fraction(1, 3) =", x) |
| 144 | + |
| 145 | + # You can perform all the usual mathematical operations with fraction |
| 146 | + print("Fraction(1, 3)**2 =", x**2) |
| 147 | + |
| 148 | + # The Fraction object will automatically reduce fraction (6/4) = (3/2) |
| 149 | + # Python has the common sense not to create a fraction with a zero denominator |
| 150 | + y = fractions.Fraction(6, 4) |
| 151 | + print("Fraction(6, 4) =", y) |
| 152 | + |
| 153 | + |
| 154 | +def constants(): |
| 155 | + """ |
| 156 | + Simple math constants |
| 157 | + """ |
| 158 | + print('Simple math constants') |
| 159 | + print("pi =", math.pi) |
| 160 | + print("e =", math.e) |
| 161 | + print("tan(pi/4)=", math.tan(math.pi/4)) |
| 162 | + |
| 163 | + |
| 164 | +def else_forms(): |
| 165 | + """ |
| 166 | + 'else' in the end if 'for', 'while', 'try' means fallback branch, |
| 167 | + which works if none of iterations were performed |
| 168 | + """ |
| 169 | + items = [] |
| 170 | + for item in items: |
| 171 | + print("Item = {}".format(item)) |
| 172 | + else: |
| 173 | + print("Fallback branch") |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == '__main__': |
| 177 | + """ |
| 178 | + Choose one of examples |
| 179 | + """ |
| 180 | + function = sys.argv[1] |
| 181 | + try: |
| 182 | + locals()[function]() |
| 183 | + except KeyError as _: |
| 184 | + print("Choose one of module functions to call, e.g. operations") |
0 commit comments