{"id":43,"date":"2016-11-20T16:29:35","date_gmt":"2016-11-20T16:29:35","guid":{"rendered":"http:\/\/python.wp.w3.pt\/?p=43"},"modified":"2016-11-21T01:13:29","modified_gmt":"2016-11-21T01:13:29","slug":"funcoes-predefinidas-ii","status":"publish","type":"post","link":"http:\/\/python.w3.pt\/?p=43","title":{"rendered":"Fun\u00e7\u00f5es predefinidas II"},"content":{"rendered":"<p>Eis a lista de fun\u00e7\u00f5es predefinidas do Python atualmente.<\/p>\n<style>\ntd,th {padding:1px;font-family:courier,monospace;}<br \/>a.nlnk {text-decoration:none;box-shadow: 0 0 0 0 transparent;}<br \/>a.nlnk:hover {text-decoration:underline;}<br \/><\/style>\n<div style=\"font-size: 0.8em;\">\n<table border=\"1\">\n<colgroup>\n<col width=\"21%\" \/>\n<col width=\"19%\" \/>\n<col width=\"20%\" \/>\n<col width=\"19%\" \/>\n<col width=\"22%\" \/> <\/colgroup>\n<thead valign=\"bottom\">\n<tr>\n<th style=\"text-align: center;\" colspan=\"5\">Built-in Functions<\/th>\n<\/tr>\n<\/thead>\n<tbody valign=\"top\">\n<tr>\n<td><a class=\"nlnk\" title=\"abs\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#abs\">abs()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"divmod\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#divmod\">divmod()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"input\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#input\">input()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"open\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#open\">open()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"staticmethod\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#staticmethod\">staticmethod()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"all\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#all\">all()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"enumerate\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#enumerate\">enumerate()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"int\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#int\">int()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"ord\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#ord\">ord()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"str\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#str\">str()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"any\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#any\">any()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"eval\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#eval\">eval()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"isinstance\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#isinstance\">isinstance()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"pow\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#pow\">pow()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"sum\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#sum\">sum()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"basestring\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#basestring\">basestring()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"execfile\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#execfile\">execfile()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"issubclass\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#issubclass\">issubclass()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"print\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#print\">print()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"super\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#super\">super()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"bin\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#bin\">bin()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"file\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#file\">file()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"iter\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#iter\">iter()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"property\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#property\">property()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"tuple\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#tuple\">tuple()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"bool\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#bool\">bool()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"filter\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#filter\">filter()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"len\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#len\">len()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"range\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#range\">range()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"type\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#type\">type()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"bytearray\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#bytearray\">bytearray()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"float\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#float\">float()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"list\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#list\">list()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"raw_input\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#raw_input\">raw_input()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"unichr\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#unichr\">unichr()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"callable\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#callable\">callable()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"format\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#format\">format()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"locals\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#locals\">locals()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"reduce\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#reduce\">reduce()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"unicode\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#unicode\">unicode()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"chr\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#chr\">chr()<\/a><\/td>\n<td><a class=\"nlnk\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#func-frozenset\">frozenset()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"long\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#long\">long()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"reload\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#reload\">reload()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"vars\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#vars\">vars()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"classmethod\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#classmethod\">classmethod()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"getattr\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#getattr\">getattr()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"map\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#map\">map()<\/a><\/td>\n<td><a class=\"nlnk\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#func-repr\">repr()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"xrange\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#xrange\">xrange()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"cmp\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#cmp\">cmp()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"globals\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#globals\">globals()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"max\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#max\">max()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"reversed\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#reversed\">reversed()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"zip\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#zip\">zip()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"compile\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#compile\">compile()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"hasattr\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#hasattr\">hasattr()<\/a><\/td>\n<td><a class=\"nlnk\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#func-memoryview\">memoryview()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"round\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#round\">round()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"__import__\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#__import__\">__import__()<\/a><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"complex\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#complex\">complex()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"hash\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#hash\">hash()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"min\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#min\">min()<\/a><\/td>\n<td><a class=\"nlnk\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#func-set\">set()<\/a><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"delattr\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#delattr\">delattr()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"help\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#help\">help()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"next\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#next\">next()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"setattr\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#setattr\">setattr()<\/a><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#func-dict\">dict()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"hex\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#hex\">hex()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"object\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#object\">object()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"slice\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#slice\">slice()<\/a><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><a class=\"nlnk\" title=\"dir\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#dir\">dir()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"id\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#id\">id()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"oct\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#oct\">oct()<\/a><\/td>\n<td><a class=\"nlnk\" title=\"sorted\" href=\"https:\/\/docs.python.org\/2\/library\/functions.html#sorted\">sorted()<\/a><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Segue-se a exposi\u00e7\u00e3o de algumas dessas fun\u00e7\u00f5es de \u00e2mbito mais geral. As primeiras fun\u00e7\u00f5es j\u00e1 foram apresentadas no <a href=\"http:\/\/python.w3.pt\/?p=36\/\">artigo anterior<\/a>.<\/p>\n<p>As fun\u00e7\u00f5es <em>chr()<\/em> e<em> ord()<\/em> convertem uma letra ASCII no seu valor num\u00e9rico e vice-versa.<\/p>\n<pre>&gt;&gt;&gt; chr(65)\r\n'A'\r\n&gt;&gt;&gt; ord('B')\r\n66\r\n&gt;&gt;&gt; chr(225)\r\n'\\xe1'\r\n<\/pre>\n<p>As fun\u00e7\u00f5es <em>complex()<\/em>, <em>str()<\/em>, <em>int()<\/em>, <em>long()<\/em> e <em>float()<\/em> convertem string para n\u00famero ou vice-versa.<br \/>\nA fun\u00e7\u00e3o <em>hex()<\/em> converte um inteiro para o formato hexadecimal.<br \/>\nA fun\u00e7\u00e3o <em>oct()<\/em> converte um inteiro para o formato octal.<\/p>\n<pre>&gt;&gt;&gt; complex(\"5+2j\")\r\n(5+2j)\r\n&gt;&gt;&gt; str(3+6)\r\n'9'\r\n&gt;&gt;&gt; int(\"4\")\r\n4\r\n&gt;&gt;&gt; float(\"3.14\")\r\n3.14\r\n&gt;&gt;&gt; hex(1234)\r\n'0x4d2'\r\n&gt;&gt;&gt; oct(123)\r\n'0173'\r\n<\/pre>\n<p><em>divmod(a,b)<\/em> calcula a divis\u00e3o inteira e o resto da divis\u00e3o inteira entre dois n\u00fameros (a\/\/b,a%b).<\/p>\n<pre>&gt;&gt;&gt; divmod(7,3)\r\n(2, 1)\r\n<\/pre>\n<p><em> enumerate(sequence, start=0)<\/em> devolve uma sequ\u00eancia enumerada<\/p>\n<pre>&gt;&gt;&gt; seasons = ['Spring', 'Summer', 'Fall', 'Winter']\r\n&gt;&gt;&gt; list(enumerate(seasons))\r\n[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\r\n&gt;&gt;&gt; list(enumerate(seasons, start=1))\r\n[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]\r\n<\/pre>\n<p>A fun\u00e7\u00e3o eval() \u00e9 semelhante \u00e0 fun\u00e7\u00e3o hom\u00f3nima do Javascript: avalia uma express\u00e3o contida numa string.<\/p>\n<pre>&gt;&gt;&gt; x = 1\r\n&gt;&gt;&gt; print eval('x+1')\r\n<\/pre>\n<p>O comando exec() permite executar c\u00f3digo contido numa string. A fun\u00e7\u00e3o execfile() executa c\u00f3digo contido num ficheiro.<\/p>\n<pre>&gt;&gt;&gt; exec(\"x=3\")\r\n&gt;&gt;&gt; print x\r\n3\r\n<\/pre>\n<p>As fun\u00e7\u00f5es <em>file()<\/em> e <em>open()<\/em> permitem criar e\/ou abrir um ficheiro, mas a \u00faltima \u00e9 prefer\u00edvel.<br \/>\nA fun\u00e7\u00e3o <em>format(value[, format_spec])<\/em> converte um valor para um formato definido por <em>format_spec<\/em> (ver <a href=\"https:\/\/docs.python.org\/2\/library\/string.html#formatspec\">documenta\u00e7\u00e3o<\/a>).<\/p>\n<pre>&gt;&gt;&gt; s = \"python string!\"\r\nb = \"i am a {0}\".format(s)\r\n&gt;&gt;&gt; a =  \"i am a %s\" % s\r\nprint \"--{:^30}--\".format(s)\r\n&gt;&gt;&gt; b = \"i am a {0}\".format(s)\r\n&gt;&gt;&gt; print a\r\n'{0}{1}{0}'.format('abra', 'cad')\r\ni am a python string!\r\n\r\n&gt;&gt;&gt; print b\r\ni am a python string!\r\n&gt;&gt;&gt; print \"--{:^30}--\".format(s)\r\n'Coordinates: {latitude}, {longitude}'.format(**coord)\r\n\r\n--        python string!        --\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; '{2}, {1}, {0}'.format('a', 'b', 'c')\r\n'c, b, a'\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; '{0}{1}{0}'.format('abra', 'cad')\r\n'abracadabra'\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')\r\n'Coordinates: 37.24N, -115.81W'\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; coord = {'latitude': '37.24N', 'longitude': '-115.81W'}\r\n&gt;&gt;&gt; 'Coordinates: {latitude}, {longitude}'.format(**coord)\r\n'Coordinates: 37.24N, -115.81W'\r\n<\/pre>\n<p><em>globals()<\/em> devolve um dicion\u00e1rio (objeto JSON) com a tabela de s\u00edmbolos globais.<br \/>\n<em>locals()<\/em> devolve um dicion\u00e1rio (objeto JSON) com a tabela de s\u00edmbolos locais.<\/p>\n<pre>&gt;&gt;&gt; globals()\r\n{'__builtins__': &lt;module '__builtin__' (built-in)&gt;, '__name__': '__main__', '__doc__': None, '__package__': None}\r\n&gt;&gt;&gt; locals()\r\n{'__builtins__': &lt;module '__builtin__' (built-in)&gt;, '__name__': '__main__', '__doc__': None, '__package__': None}\r\n<\/pre>\n<p><em>len()<\/em> devolve o n\u00famero de elementos de um objeto.<\/p>\n<pre>&gt;&gt;&gt; len(\"Ol\u00e1\")\r\n3\r\n&gt;&gt;&gt; len([1,3,5])\r\n3\r\n&gt;&gt;&gt; len(globals())\r\n4\r\n<\/pre>\n<p><em>map(function, iterable, &#8230;)<\/em> aplica uma fun\u00e7\u00e3o aos valores de um iter\u00e1vel.<br \/>\n<em>max(iterable[, key])<\/em> ou <em>max(arg1, arg2, *args[, key])<\/em> determinam o m\u00e1ximo de um conjunto de elementos.<br \/>\n<em>min(iterable[, key])<\/em> ou <em>min(arg1, arg2, *args[, key])<\/em> determinam o m\u00e1ximo de um conjunto de elementos.<br \/>\n<em>sum(iterable[, start])<\/em> soma os elementos de um iter\u00e1vel.<\/p>\n<pre>&gt;&gt;&gt; map(lambda x: 2*x, [1,2,4,6,2,3])\r\n[2, 4, 8, 12, 4, 6]\r\n&gt;&gt;&gt; max([1,2,4,6,2,3])\r\n6\r\n&gt;&gt;&gt; min([1,2,4,6,2,3])\r\n1\r\n&gt;&gt;&gt; sum([1,2,4,6,2,3])\r\n18\r\n<\/pre>\n<p><em>next(iterator[, default])<\/em> obt\u00e9m o pr\u00f3ximo elemento de um iterador.<\/p>\n<p><em>pow(x, y[, z])<\/em> calcula a pot\u00cancia de x elevado a y m\u00f3dulo z, de forma eficiente. z \u00e9 opcional, e nesse caso pode calcular-se x ** y.<\/p>\n<pre>&gt;&gt;&gt; pow(2,3,5)\r\n3\r\n&gt;&gt;&gt; pow(2,3)\r\n8\r\n&gt;&gt;&gt; 2**3\r\n8\r\n<\/pre>\n<p><em>print(*objects, sep=&#8217; &#8216;, end=&#8217;\\n&#8217;, file=sys.stdout)<\/em> imprime objetos num fluxo (<em>stream<\/em>).<\/p>\n<p><em>range()<\/em> cria um intervalo de inteiros.<\/p>\n<pre>&gt;&gt;&gt; range(10)\r\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\r\n&gt;&gt;&gt; range(5,10)\r\n[5, 6, 7, 8, 9]\r\n&gt;&gt;&gt; range(0, -10, -1)\r\n[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\r\n<\/pre>\n<p>O Python foi criado com o nome de um grupo de animadores brit\u00e2nicos, tal como se percebe pelo exemplo da fun\u00e7\u00e3o <em>raw_input()<\/em>.<\/p>\n<pre>&gt;&gt;&gt; s = raw_input('--&gt; ')\r\n--&gt; Monty Python's Flying Circus\r\n&gt;&gt;&gt; s\r\n\"Monty Python's Flying Circus\"\r\n<\/pre>\n<p><em>reduce(function, iterable[, initializer])<\/em> aplica uma fun\u00e7\u00e3o de dois arguemntos, iterativamente a um iter\u00e1vel. Por exemplo, <strong>reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])<\/strong> calcula <strong>((((1+2)+3)+4)+5)<\/strong>.<\/p>\n<pre>&gt;&gt;&gt; def soma(x,y):\r\n...   return x+2*y\r\n...\r\n&gt;&gt;&gt; reduce(soma, [1, 2, 3, 4, 5])\r\n29\r\n<\/pre>\n<p><em>round()<\/em> arredonda um n\u00famero.<\/p>\n<pre>&gt;&gt;&gt; round(3.14)\r\n3.0\r\n&gt;&gt;&gt; round(3.14,1)\r\n3.1\r\n&gt;&gt;&gt; round(3.7)\r\n4.0\r\n<\/pre>\n<p><em>frozenset<\/em>, <em>list, set, tuple<\/em> e <em>dict<\/em> s\u00e3o fun\u00e7\u00f5es que permitem criar iter\u00e1veis ou cole\u00e7\u00f5es.<\/p>\n<p><em>sorted(iterable[, cmp[, key[, reverse]]])<\/em> ordena um iter\u00e1vel.<\/p>\n<pre>&gt;&gt;&gt; sorted( [1,2,4,6,2,3], cmp=lambda x,y: cmp(x, y) )\r\n[1, 2, 2, 3, 4, 6]\r\n&gt;&gt;&gt; sorted( [1,2,4,6,2,3], cmp=lambda x,y: cmp(y, x) )\r\n[6, 4, 3, 2, 2, 1]\r\n<\/pre>\n<p><em>tuple([iterable])<\/em> cria um tuplo a partir de um iter\u00e1vel.<\/p>\n<pre>&gt;&gt;&gt; tuple(\"xpto\")\r\n('x', 'p', 't', 'o')\r\n<\/pre>\n<p>A fun\u00e7\u00e3o <em>type(object)<\/em> obt\u00e9m o tipo de um objeto.<\/p>\n<pre>&gt;&gt;&gt; type(\"xpto\")\r\n&lt;type 'str'&gt;\r\n&gt;&gt;&gt; type([1,2,4,6,2,3])\r\n&lt;type 'list'&gt;\r\n&gt;&gt;&gt; type(3+6j)\r\n&lt;type 'complex'&gt;<\/pre>\n<p>A fun\u00e7\u00e3o type(name, bases, dict) pode ser usada com 3 pr\u00e2metros. Nesse caso cria uma classe. As duas instru\u00e7\u00f5es seguintes s\u00e3o equivalentes.<\/p>\n<pre>\r\n>>> class X(object):\r\n...     a = 1\r\n...\r\n>>> X = type('X', (object,), dict(a=1))\r\n<\/pre>\n<p><em>unichr(i)<\/em> devolve a string Unicode, cujo c\u00f3digo \u00e9 i.<\/p>\n<p><em>zip([iterable, &#8230;])<\/em> cria uma lista de tuplos emparelhados pelo \u00edndice dos argumentos. A lista gerada tem o comprimento do iter\u00e1vel mais curto.<\/p>\n<pre>\r\n>>> zip([1,2,3],\"abc\")\r\n[(1, 'a'), (2, 'b'), (3, 'c')]\r\n>>> zip([1,2,3,4,5,6],\"xpto\")\r\n[(1, 'x'), (2, 'p'), (3, 't'), (4, 'o')]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Eis a lista de fun\u00e7\u00f5es predefinidas do Python atualmente. Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file() iter() property() tuple() bool() filter() len() range() type() bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/python.w3.pt\/?p=43\" class=\"more-link\">Continuar a ler <span class=\"screen-reader-text\">&#8220;Fun\u00e7\u00f5es predefinidas II&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/43"}],"collection":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":40,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":84,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/84"}],"wp:attachment":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}