{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A4 – Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Can you answer these questions?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - What is a Python object?\n", " \n", "*Everything (which is not considered a syntax element) is considered an object in Python: Variables, functions, modules, ...*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - How would you assign a value to a variable?\n", " \n", "*Use the* `=` *operator to assign from right to left.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - How can you know, something is an object in Python?\n", " \n", "*Objects have a type. Discover the type by using* `type(object)`*.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - How can you know a Python object is a function?\n", " \n", "*Learn details about a Python object by using* `help(object)`*. Functions can be called as* `function(arguments)`*.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - How can you get an overview of all the attributes of an object?\n", "```python\n", "dir(object)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - How can you discover all objects currently available?\n", "```python\n", "dir()\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Call `help()` on the built-in Python function `help` to see what it does. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on _Helper in module _sitebuiltins object:\n", "\n", "class _Helper(builtins.object)\n", " | Define the builtin 'help'.\n", " | \n", " | This is a wrapper around pydoc.help that provides a helpful message\n", " | when 'help' is typed at the Python interactive prompt.\n", " | \n", " | Calling help() at the Python prompt starts an interactive help session.\n", " | Calling help(thing) prints help for the python object 'thing'.\n", " | \n", " | Methods defined here:\n", " | \n", " | __call__(self, *args, **kwds)\n", " | Call self as a function.\n", " | \n", " | __repr__(self)\n", " | Return repr(self).\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n" ] } ], "source": [ "help(help)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3__ Which methods (disregarding double underscore methods `__method__`) does a `str` object have? Try out those of them with an example of your choice that deal with case-formatting (upper/lower case letters) and explain shortly what they do." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['capitalize',\n", " 'casefold',\n", " 'center',\n", " 'count',\n", " 'encode',\n", " 'endswith',\n", " 'expandtabs',\n", " 'find',\n", " 'format',\n", " 'format_map',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isdecimal',\n", " 'isdigit',\n", " 'isidentifier',\n", " 'islower',\n", " 'isnumeric',\n", " 'isprintable',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper',\n", " 'join',\n", " 'ljust',\n", " 'lower',\n", " 'lstrip',\n", " 'maketrans',\n", " 'partition',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rpartition',\n", " 'rsplit',\n", " 'rstrip',\n", " 'split',\n", " 'splitlines',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'title',\n", " 'translate',\n", " 'upper',\n", " 'zfill']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get all callable attributes (no double underscores)\n", "[\n", " a\n", " for a in dir(str)\n", " if (not a.startswith('__')) # no dunder?\n", " & hasattr(getattr(str, a), \"__call__\") # callable?\n", "]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "run_control": { "marked": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Blind text\n", "BLIND tEXT\n", "blind text\n", "BLIND TEXT\n", "blind text\n" ] } ], "source": [ "s = \"blind Text\"\n", "print(s.capitalize()) # Upper case first letters\n", "print(s.swapcase()) # Invert lower to upper case and vice versa\n", "print(s.lower()) # All lower case\n", "print(s.upper()) # All upper case\n", "print(s.casefold()) # Case less comparison" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__4__ Which methods (disregarding double underscore methods `__method__`) does a `complex` object have? Try out all of them with an example of your choice and explain shortly what they do." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['conjugate']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get all callable attributes (no double underscores)\n", "[\n", " a\n", " for a in dir(complex)\n", " if (not a.startswith('__')) # no dunder?\n", " & hasattr(getattr(complex, a), \"__call__\") # callable?\n", "]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "c = 1 + 1j\n", "print(c.conjugate)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__5__ When you call `print(string)`, the value of `string` is printed to the screen. Call `help()` on the built-in Python function `print` to see what it does. Can you figure out just by reading the help message how to automatically print an exclamation mark (`!`) after the print? " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function print in module builtins:\n", "\n", "print(...)\n", " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", " \n", " Prints the values to a stream, or to sys.stdout by default.\n", " Optional keyword arguments:\n", " file: a file-like object (stream); defaults to the current sys.stdout.\n", " sep: string inserted between values, default a space.\n", " end: string appended after the last value, default a newline.\n", " flush: whether to forcibly flush the stream.\n", "\n" ] } ], "source": [ "help(print)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string!" ] } ], "source": [ "print(\"string\", end=\"!\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }