3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] Python Type "help", "copyright", "credits" or "license" for more information. 2 + 3 5 11 + 56 67 23 - 54 -31 4 * 5 20 8 / 4 2.0 8 / 5 1.6 # 4 + 5 # This is a comment. Whatever comes after the # is not executed # So far, we've seen two types of data # int, float # Float stands for "floating-point number" # Floating-point numbers are approximatiosn of real numbers # Division (/) produces a float # We can check the type of some data in Python type(3) type(3.0) 8 / 4 2.0 8 // 4 2 # // is integer division, and it produces an int 8 / 5 1.6 8 // 5 1 # // throws away the remainder 8 % 5 3 # % is the 'modulo' operator - gives the remainder 4 + 5 * 3 19 4 + (5 * 3) 19 (4 + 5) * 3 27 # ** is exponentiation 2 ** 5 # 2 to the power of 5 32 # Order of precedence for operations (highest to lowest) # ** # - (negation) # *, /, //, % (from left to right) # +, - (from left to right) 5 + (2 ** 3) + 5 18 5 + 2 ** 3 + 5 18 5 + ((2 ** 3) + 5) 18 # negation: -2 -2 5 - -2 7 5 - (-2) 7 5 - --2 3 5 - (-(-2)) 3 5 - -------------2 7 5 -------------2 3 # Whitespace: # in practise, the amount of whitespace doesn't matter # in these expressions 2 + 3 5 1+2 3 1 * 2 2 # but, for good style (which you will be marked on), one space # between each operator and operand 123 123 1 2 3 Traceback (most recent call last): Python Shell, prompt 55, line 1 Syntax Error: invalid syntax: , line 1, pos 3 2 * * 5 Traceback (most recent call last): Python Shell, prompt 56, line 1 Syntax Error: invalid syntax: , line 1, pos 5 3 + Traceback (most recent call last): Python Shell, prompt 57, line 1 Syntax Error: invalid syntax: , line 1, pos 4 4 + 5) * 4 Traceback (most recent call last): Python Shell, prompt 58, line 1 Syntax Error: invalid syntax: , line 1, pos 6 (4 + 5 fdjfhjdf ) Traceback (most recent call last): Python Shell, prompt 59, line 2 Syntax Error: invalid syntax: , line 2, pos 8 4 / 0 Traceback (most recent call last): Python Shell, prompt 60, line 1 builtins.ZeroDivisionError: division by zero # Built-in functions type(45) type(56.34234) min(34, 12) 12 min(34, 12, 4) 4 max(34, 12, 4) 34 dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip'] abs(-56) 56 abs(-56.45345) 56.45345 help(abs) Help on built-in function abs in module builtins: abs(x, /) Return the absolute value of the argument. 2 + 2 4 abs(2 + 2) 4 help(max) Help on built-in function max in module builtins: max(...) max(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument. help(pow) Help on built-in function pow in module builtins: pow(x, y, z=None, /) Equivalent to x**y (with two arguments) or x**y % z (with three arguments) Some types, such as ints, are able to use a more efficient algorithm when invoked using the three argument form. pow(2, 5) 32 pow(2, 5, 3) # (2 ** 5) % 3 2 help(round) Help on built-in function round in module builtins: round(number, ndigits=None) Round a number to a given precision in decimal digits. The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as the number. ndigits may be negative. round(4.5678) 5 round(4.5678, 3) 4.568 round(412.5678, -2) 400.0 round(5.5) 6 round(4.5) 4 pi Traceback (most recent call last): Python Shell, prompt 83, line 1 builtins.NameError: name 'pi' is not defined import math pi Traceback (most recent call last): Python Shell, prompt 85, line 1 builtins.NameError: name 'pi' is not defined math.pi 3.141592653589793 8 % 5 3 8 // 5 1 8 / 5 1.6 round(4.5, -1) 0.0 round(4.5, 0) 4.0 round(4.5, 0) 4.0