#!/usr/bin/env python # coding: utf-8 # ## Built-in Functions # Open In Colab # # - named sequence of code that does some specific task or a function # - we'll learn how to define our own functions in [User Defined Functions chapter](./Ch03-3-Functions-UserDefined.ipynb) # - some built-in functions we've used so far: type( ), int( ), float( ), str( ), input( ), print( ), etc. # - Python provides a list of built-in functions that are readily available for use: # https://docs.python.org/3/library/functions.html # - below are examples of some built-in functions that may be useful to know # ### bin(x) # - converts an integer number to a binary string prefixed with "0b". # In[1]: bin(3) # ### format(value, format_spec) # - formats the give value using format spec # In[1]: help(format) # In[2]: # If prefix "0b is desired or not, use either of the following ways print(format(3, '#b')) print(format(3, 'b')) # ### chr( uniCode ) # - returns the string representing a character whose Unicode code point is the integer uniCode # - inverse of ord(character) # In[3]: print(chr(65)) print(chr(97)) print(chr(8364)) # ### globals() and locals() # - globals() returns a dictionary representing the current global symbol table # - locals() returns a dictionary representing the current local symbol table # In[4]: globals() # ### hex(x) # - convert an integer number to a lowercase hexadecimal string prefixed with "0x" # In[5]: print(hex(42)) print(hex(-42)) # In[2]: # Other ways print(format(255, '#x')) print(format(255, 'x')) print(format(255, 'X')) # ### oct(x) # - return an octal string representation with "0o" prefix of a given integer x # In[7]: print(oct(100)) # In[8]: print(format(10, '#o')) print(format(10, 'o')) # ### ord(c) # - return an integer representing the Unicode code of a given Unicode character # In[9]: print(ord(' ')) print(ord('~')) # ### id(object) # - return the 'identity' of an object # - guaranteed unique integer and constant thoughout its lifetime # In[10]: x = 10 # In[11]: id(x) # ### divmod(a, b) # - given two non-complex numbers as arguments, return a pair of numbers as tuple consisting of their quotient and remainder using integer division # In[12]: print(divmod(7, 3)) # return (quotient, remainder) quotient, remainder = divmod(7, 3) print(quotient, remainder) # ### eval(expression, globals=None, locals=None) # - the expression argument is parsed and evaluated as Python expression # - syntax errors reported as exceptions # In[13]: y = 2 print(eval('y**2')) print(eval('y+2*3')) # ### max(iterable, ...) or max(arg1, arg2, ...) # - returns the largest item in an iterable or the largest of two or more arguments # In[14]: print('max=', max(100, 8.9, 999, 1000.5)) # ### min(arg1, arg2, ...) # - returns the smallest of the the arguments (arg1, arg2...) # In[15]: print('min=', min(100, 8.9, 999, 1000.5)) # ### pow(base, exponent) # - returns base to the power exponent # In[16]: print('2 to the power 3 =', pow(2, 3)) # ### print() # - print(*object, sep=' ', end='\\n', file=sys.stdout, flush=False) # - print takes many arguments and print arbitrary number of values # - demo some print examples with different argument values # In[17]: print('hi', 'hello', sep='', end='') # In[19]: print('hi', 'hello', sep=' ', end='') # In[20]: print('hi', 'hello', sep=' ', end='') print('next line?') # In[21]: print('hi', 'hello', sep='\t', end='\n') print('next line?') # In[ ]: