#!/usr/bin/env python # coding: utf-8 # ## Built-in Functions # - built-in 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 # - 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) # 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[6]: # Other ways format(255, '#x'), format(255, 'x'), 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[12]: x = 10 # In[13]: 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[14]: 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[15]: 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[17]: print('max=', max(100, 8.9, 999, 1000.5)) # ### min(arg1, arg2, ...) # - returns the smallest of the the arguments (arg1, arg2...) # In[18]: print('min=', min(100, 8.9, 999, 1000.5)) # ### pow(base, exponent) # - returns base to the power exponent # In[19]: print('2 to the power 3 =', pow(2, 3)) # ### print(*object, sep=' ', end='\n', file=sys.stdout, flush=False) # - demo some print examples with different argument values # In[ ]: