




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Saiba como definir, chamar e utilizar funções em Python. Aprenda a reutilizar código e escrever programas mais limpos e organizados. Este documento aborda o conceito básico de funções, tipos de funções, argumentos e parâmetros, retorno de valores e o uso de funções pré-definidas da biblioteca Python.
Typology: Cheat Sheet
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























www.pythonlearn.com
big = max('Hello world') print(big) w
“Hello world” (uma string) ‘w’ (uma string) Uma função é algum código a r m a z e n a d o q u e n ó s utilizamos. Uma função recebe alguma entrada e produz alguma saída.
def max(inp): blah blah blah blah “Hello world” (uma string) ‘w’ (uma string)
big = max('Hello world') print(big) w Uma função é algum código a r m a z e n a d o q u e n ó s utilizamos. Uma função recebe alguma entrada e produz alguma saída.
Programa: x = 5 print('Hello') def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print('Yo') x = x + 2 print(x) Saída: Hello Yo 7 Exemplo PythonTutor: https://tinyurl.com/y5x5ygyl
Argumento
Um parâmetro é uma variável que usamos dentro da definição da função. É uma espécie de “ligação” que permite que o código da função acesse os argumentos em uma invocação específica da função. def greet(lang): if lang == 'es': print('Hola') elif lang == 'fr': print('Bonjour') else: print('Hello') greet('en') greet('es') greet('fr') PythonTutor: https://tinyurl.com/yxkptxlz
Argumentos, Parâmetros e Resultados
big = max('Hello world') print(big) w def max(inp): blah blah blah blah return ‘w’ “Hello world” ‘w’ Argumento Parâmetro Resultado
retorna um valor, nós a chamamos de função "void”
função não possui um comando return (não retorna um valor), ela retorna o tipo especial None def get_ing(wd): return wd + 'ing' def print_ing(wd): print(wd + 'ing') foo = get_ing('interest') print(foo) faa = print_ing('interest') print(faa) PythonTutor: https://tinyurl.com/yxnumnon
( all batteries included )
coisas)
criptografia, comunicação em rede, GUI, etc.