{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Built-in Functions\n", "\"Open\n", "\n", "- named sequence of code that does some specific task or a function\n", "- we'll learn how to define our own functions in [User Defined Functions chapter](./Ch03-3-Functions-UserDefined.ipynb)\n", "- some built-in functions we've used so far: type( ), int( ), float( ), str( ), input( ), print( ), etc.\n", "- Python provides a list of built-in functions that are readily available for use:\n", "https://docs.python.org/3/library/functions.html\n", "- below are examples of some built-in functions that may be useful to know" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### bin(x)\n", "- converts an integer number to a binary string prefixed with \"0b\"." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b11'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### format(value, format_spec)\n", "- formats the give value using format spec" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function format in module builtins:\n", "\n", "format(value, format_spec='', /)\n", " Return value.__format__(format_spec)\n", " \n", " format_spec defaults to the empty string.\n", " See the Format Specification Mini-Language section of help('FORMATTING') for\n", " details.\n", "\n" ] } ], "source": [ "help(format)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0b11\n", "11\n" ] } ], "source": [ "# If prefix \"0b is desired or not, use either of the following ways\n", "print(format(3, '#b'))\n", "print(format(3, 'b'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### chr( uniCode )\n", "- returns the string representing a character whose Unicode code point is the integer uniCode\n", "- inverse of ord(character)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "a\n", "€\n" ] } ], "source": [ "print(chr(65))\n", "print(chr(97))\n", "print(chr(8364))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### globals() and locals()\n", "- globals() returns a dictionary representing the current global symbol table\n", "- locals() returns a dictionary representing the current local symbol table" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'__name__': '__main__',\n", " '__doc__': 'Automatically created module for IPython interactive environment',\n", " '__package__': None,\n", " '__loader__': None,\n", " '__spec__': None,\n", " '__builtin__': ,\n", " '__builtins__': ,\n", " '_ih': ['',\n", " 'bin(3)',\n", " '# If prefix \"0b is desired or not, use either of the following ways\\nprint(format(3, \\'#b\\'))\\nprint(format(3, \\'b\\'))',\n", " 'print(chr(65))\\nprint(chr(97))\\nprint(chr(8364))',\n", " 'globals()'],\n", " '_oh': {1: '0b11'},\n", " '_dh': ['/Volumes/Storage/GoogleDrive/CMU/Python/thinkpythonnotebooks'],\n", " '_sh': ,\n", " 'In': ['',\n", " 'bin(3)',\n", " '# If prefix \"0b is desired or not, use either of the following ways\\nprint(format(3, \\'#b\\'))\\nprint(format(3, \\'b\\'))',\n", " 'print(chr(65))\\nprint(chr(97))\\nprint(chr(8364))',\n", " 'globals()'],\n", " 'Out': {1: '0b11'},\n", " 'get_ipython': >,\n", " 'exit': ,\n", " 'quit': ,\n", " '_': '0b11',\n", " '__': '',\n", " '___': '',\n", " '_i': 'print(chr(65))\\nprint(chr(97))\\nprint(chr(8364))',\n", " '_ii': '# If prefix \"0b is desired or not, use either of the following ways\\nprint(format(3, \\'#b\\'))\\nprint(format(3, \\'b\\'))',\n", " '_iii': 'bin(3)',\n", " '_i1': 'bin(3)',\n", " '_1': '0b11',\n", " '_i2': '# If prefix \"0b is desired or not, use either of the following ways\\nprint(format(3, \\'#b\\'))\\nprint(format(3, \\'b\\'))',\n", " '_i3': 'print(chr(65))\\nprint(chr(97))\\nprint(chr(8364))',\n", " '_i4': 'globals()'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### hex(x)\n", "- convert an integer number to a lowercase hexadecimal string prefixed with \"0x\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0x2a\n", "-0x2a\n" ] } ], "source": [ "print(hex(42))\n", "print(hex(-42))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xff\n", "ff\n", "FF\n" ] } ], "source": [ "# Other ways\n", "print(format(255, '#x'))\n", "print(format(255, 'x'))\n", "print(format(255, 'X'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### oct(x)\n", "- return an octal string representation with \"0o\" prefix of a given integer x" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0o144\n" ] } ], "source": [ "print(oct(100))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0o12\n", "12\n" ] } ], "source": [ "print(format(10, '#o'))\n", "print(format(10, 'o'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ord(c)\n", "- return an integer representing the Unicode code of a given Unicode character" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32\n", "126\n" ] } ], "source": [ "print(ord(' '))\n", "print(ord('~'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### id(object)\n", "- return the 'identity' of an object\n", "- guaranteed unique integer and constant thoughout its lifetime" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "x = 10" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4525196800" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### divmod(a, b)\n", "- given two non-complex numbers as arguments, return a pair of numbers as tuple consisting of their quotient and remainder using integer division" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 1)\n", "2 1\n" ] } ], "source": [ "print(divmod(7, 3)) # return (quotient, remainder)\n", "quotient, remainder = divmod(7, 3)\n", "print(quotient, remainder)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### eval(expression, globals=None, locals=None)\n", "- the expression argument is parsed and evaluated as Python expression\n", "- syntax errors reported as exceptions" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "8\n" ] } ], "source": [ "y = 2\n", "print(eval('y**2'))\n", "print(eval('y+2*3'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### max(iterable, ...) or max(arg1, arg2, ...)\n", "- returns the largest item in an iterable or the largest of two or more arguments" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "max= 1000.5\n" ] } ], "source": [ "print('max=', max(100, 8.9, 999, 1000.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### min(arg1, arg2, ...)\n", "- returns the smallest of the the arguments (arg1, arg2...)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "min= 8.9\n" ] } ], "source": [ "print('min=', min(100, 8.9, 999, 1000.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pow(base, exponent) \n", "- returns base to the power exponent" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 to the power 3 = 8\n" ] } ], "source": [ "print('2 to the power 3 =', pow(2, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### print()\n", "- print(*object, sep=' ', end='\\\\n', file=sys.stdout, flush=False)\n", "- print takes many arguments and print arbitrary number of values\n", "- demo some print examples with different argument values" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hihello" ] } ], "source": [ "print('hi', 'hello', sep='', end='')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi hello" ] } ], "source": [ "print('hi', 'hello', sep=' ', end='')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi hellonext line?\n" ] } ], "source": [ "print('hi', 'hello', sep=' ', end='')\n", "print('next line?')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\thello\n", "next line?\n" ] } ], "source": [ "print('hi', 'hello', sep='\\t', end='\\n')\n", "print('next line?')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }