Built-in Functions
chr complex delattr dict eval float frozenset getattr hasattr hex input int list long oct ord raw_input repr set setattr str tuple unichar
chr
  • Converts an integer to a character, the argument must be in the range [0..255]
  • Raise ValueError
  • #!/usr/bin/python
    
    for i in xrange(256):
        print(chr(i))
    		
    complex
  • Return a complex number
  • Raise ValueError
  • c = complex('1+2j') # convert a string to complex
    print c, c.real, c.imag
    		
    dict
  • Create a new dictionary
  • Raise ValueError
  • a = dict(one=1, two=2, three=3)
    c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
    d = dict([('two', 2), ('one', 1), ('three', 3)])
    e = dict({'three': 3, 'one': 1, 'two': 2})
    		
    eval
  • Evaluates a string and returns an object
  • Raise SyntaxError
  • x = 1
    print eval("1+x") # 2
    		
    float
  • Return a floating point number constructed from a number or string x
  • Raise ValueError
  • print(float('100')) # convert a string to float
    print(float(0b1111)) # convert a binary number to float
    print(float(0o1777)) # convert a octal number to float
    print(float(0xABCD)) # convert a hexical number to float
    		
    frozenset
  • Return a new frozenset object
  • s = frozenset([1, 2, 3, 4, 1])
    print type(s), s # (1, 2, 3, 4)
    		
    hex
  • Convert an integer number (of any size) to a lowercase hexadecimal string
  • Raise TypeError
  • h = hex(100)
    print(type(h), h) # str, '0x64'
    		
    input
  • return the result of the evaluated expression, equals to eval(raw_input())
  • If the input is not syntactically valid, a SyntaxError will be raised
  • l = input("Enter your input:") # 1+2
    print l, type(l) # 3, int
    
    l = raw_input("Enter your input:") # 1+2
    print eval(l), type(l) # 3, str
    		
    int
  • return an integer object constructed from a number or string x, or return 0 if no arguments are given
  • Raise ValueError
  • print(int('100')) # convert a string to integer
    print(int(0b1111)) # convert a binary number to integer
    print(int(0o1777)) # convert a octal number to integer
    print(int(0xABCD)) # convert a hexical number to integer
    		
    list
  • Return a list whose items are the same and in the same order as iterable’s items, iterable may be either a sequence, a container that supports iteration, or an iterator object
  • Raise TypeError
  • l = list([1, 2, 3, 4])
    l = list(xrange(10))
    l = list(iter(xrange(10)))
    print(l)
    		
    long
  • Return a long integer object constructed from a string or number x
  • Raise ValueError
  • print type(long(10)) # long
    print(long('100')) # convert a string to long
    print(long(0b1111)) # convert a binary number to long
    print(long(0o1777)) # convert a octal number to long
    print(long(0xABCD)) # convert a hexical number to long
    		
    oct
  • Convert an integer number (of any size) to an octal string
  • Raise TypeError
  • o = oct(100)
    print(type(o), o) # str, 0144
    		
    ord
  • Given a string of length one, return an integer representing the Unicode code point of the character
  • Raise TypeError
  • print(ord('a'))
    print(ord(u'a'))
    print(ord(u'\u2020'))
    		
    raw_input
  • return a string
  • When EOF is read, EOFError is raised
  • l = raw_input("Enter your input:") # 1+2
    print l, type(l) # 1+2, 'str'
    		
    repr
  • Return a string containing a nicely printable representation of an object
  • str() is used for creating output for end user while repr() is mainly used for debugging and development. repr’s goal is to be unambiguous and str’s is to be readable
  • #!/usr/bin/python
    
    print(str(10.0/3)) # 3.33333333333
    print(repr(10.0/3)) # 3.333333333333335
    
    import datetime
    today = datetime.datetime.now()
    
    print(str(today)) # 2018-10-12 00:32:14.820843
    print(repr(today)) # atetime.datetime(2018, 10, 12, 0, 32, 14, 820843)
    		
    set
  • Return a new set object
  • s = set([1, 2, 3, 4, 1])
    print type(s), s # (1, 2, 3, 4)
    		
    str
  • Return a string containing a nicely printable representation of an object
  • s = str(100); # convert an integer to string
    print s # 100, str
    		
    tuple
  • Return a tuple whose items are the same and in the same order as iterable’s items
  • t = tuple(range(4))
    print type(t), t # tuple, (0, 1, 2, 3)
    		
    unichar
  • Return the Unicode string
  • Raise ValueError
  • u = unichr(100)
    print type(u), u
    
    u = unichr(300)
    print type(u), u
    		
    Reference
  • Python Standard Library