{"id":101,"date":"2016-11-22T01:50:57","date_gmt":"2016-11-22T01:50:57","guid":{"rendered":"http:\/\/python.wp.w3.pt\/?p=101"},"modified":"2016-11-22T02:02:08","modified_gmt":"2016-11-22T02:02:08","slug":"instrucoes","status":"publish","type":"post","link":"http:\/\/python.w3.pt\/?p=101","title":{"rendered":"Instru\u00e7\u00f5es"},"content":{"rendered":"<p>A instru\u00e7\u00e3o de atribui\u00e7\u00e3o do Python permite ter uma lista como destino da atribui\u00e7\u00e3o. J\u00e1 tinha visto noutras linguagens: no PHP e no Prolog. Eis um exemplo em Python, onde a atribui\u00e7\u00e3o \u00e0 lista <strong>a,b<\/strong> premite fazer a troca das vari\u00e1veis, sem vari\u00e1vel auxiliar:<\/p>\n<pre>\r\n>>> a=2\r\n>>> b=5\r\n>>> a,b=b,a\r\n>>> print a, b\r\n5 2\r\n<\/pre>\n<p>A instru\u00e7\u00e3o <em>pass<\/em> \u00e9 uma instru\u00e7\u00e3o nula, n\u00e3o executa nenhuma opera\u00e7\u00e3o, e \u00e9 \u00fatil para quando a sintaxe obriga \u00e0 exist\u00eancia de uma instru\u00e7\u00e3o num local onde n\u00e3o pretendemos a\u00e7\u00e3o alguma.<\/p>\n<pre>\r\ndef f(arg): pass    # a function that does nothing (yet)\r\n\r\nclass C: pass       # a class with no methods (yet)\r\n<\/pre>\n<p>A instru\u00e7\u00e3o <em>del<\/em> pemite apagar uma lista de objetos. As vari\u00e1veis a e b do exemplo acima, podem ser apagadas da forma seguinte:<\/p>\n<pre>\r\ndel a,b\r\n<\/pre>\n<p>A instru\u00e7\u00e3o <em>print<\/em> imprime um objeto ou uma lista de objetos.<\/p>\n<pre>\r\n>>> a=2\r\n>>> b=5\r\n>>> print a, b\r\n2 5\r\n<\/pre>\n<p>A instru\u00e7\u00e3o <em>return<\/em> permite devolver um objeto ou uma lista de objetos, como resultado de uma fun\u00e7\u00e3o.<\/p>\n<p>A instru\u00e7\u00e3o <em>yield<\/em> \u00e9 usada com fun\u00e7\u00f5es geradoras. Para perceber a instru\u00e7\u00e3o <em>yield<\/em> \u00e9 preciso perceber o que s\u00e3o fun\u00e7\u00f5es geradoras (<em>generators<\/em>). Uma fun\u00e7\u00e3o geradora \u00e9 semelhante a um iter\u00e1vel, mas \u00e9 criada <em>on-the-fly<\/em>, e o seu valor s\u00f3 pode ser usado uma vez, pois n\u00e3o fica guardado. A instru\u00e7\u00e3o <em>yield<\/em> faz o retorno de uma fin\u00e7\u00e3o geradora e, nesse contexto, \u00e9 semelhante a um <em>return<\/em>.<\/p>\n<pre>\r\n>>> def createGenerator():\r\n...     mylist = range(3)\r\n...     for i in mylist:\r\n...         yield i*i\r\n...\r\n>>>\r\n>>> mygenerator = createGenerator() # create a generator\r\n>>>\r\n>>> print(mygenerator) # mygenerator is an object!\r\n<generator object createGenerator at 0x7f90c2ac66e0>\r\n>>> for i in mygenerator:\r\n...      print(i)\r\n...\r\n0\r\n1\r\n4\r\n<\/pre>\n<p>As instru\u00e7\u00f5es <em>break<\/em> e <em>continue<\/em> podem ser usadas apenas no \u00e2mbito de instru\u00e7\u00f5es <em>for<\/em> e <em>while<\/em>.<\/p>\n<p>A instru\u00e7\u00e3o <em>global<\/em> cria identificadores globais.<\/p>\n<p>A instru\u00e7\u00e3o <em>exec<\/em> permite a execu\u00e7\u00e3o din\u00e2mica de c\u00f3digo Python. Segue-se um exemplo de execu\u00e7\u00e3o de c\u00f3digo que est\u00e1 contido numa string.<\/p>\n<pre>\r\n>>> mycode = 'print \"hello world\"'\r\n>>> exec(mycode)\r\nhello world\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A instru\u00e7\u00e3o de atribui\u00e7\u00e3o do Python permite ter uma lista como destino da atribui\u00e7\u00e3o. J\u00e1 tinha visto noutras linguagens: no PHP e no Prolog. Eis um exemplo em Python, onde a atribui\u00e7\u00e3o \u00e0 lista a,b premite fazer a troca das vari\u00e1veis, sem vari\u00e1vel auxiliar: >>> a=2 >>> b=5 >>> a,b=b,a >>> print a, b 5 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/python.w3.pt\/?p=101\" class=\"more-link\">Continuar a ler <span class=\"screen-reader-text\">&#8220;Instru\u00e7\u00f5es&#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\/101"}],"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=101"}],"version-history":[{"count":3,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":104,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/104"}],"wp:attachment":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}