Python🍍

Python Function✨

Jeein0313 2022. 6. 27. 19:25

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

 

Code Examples😏:

def sum(x,y):
	return x+y;
print(sum(1,2)); #3

def my_function(*kids): #매개변수의 수를 모를 때
    print(kids); #('Emil', 'Tobias', 'Linus')
my_function("Emil","Tobias","Linus") #tuple

def keyword_function(child1, child2, child3):
    print(child3); #Linus
keyword_function(child1="Emil",child2="Tobias",child3="Linus");

def arbitrary_keyword_function(**kids): #keyword 매개변수의 수를 모를 때
    print(kids['child3']); #Linus
arbitrary_keyword_function(child1="Emil",child2="Tobias",child3="Linus");

def my_country(country="Korea"):
    print("My country is "+country);
my_country(); #My country is Korea  

#for some reason have a function definition with no content, put in the pass statement to avoid getting an error.
def myfunction():
  pass

'Python🍍' 카테고리의 다른 글

Error and Exception Handling🥑  (0) 2022.06.30
Python Lambda🍋  (0) 2022.06.27
Python 반복문 while, for⚡  (0) 2022.06.27
Python 조건문 If🙄  (0) 2022.06.27
Python Operators👀  (0) 2022.06.25