** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
function
def say_something():
print('hi')
#Defined say_something()Call
say_something()
result
hi
At this time, you must add ()
after say_something
.
function
def say_something():
print('hi')
f = say_something
f()
result
hi
Of course, it is also possible to call after assigning to another variable like this.
function
def say_something():
print('hi')
print(type(say_something))
result
<class 'function'>
type is'function'.
function
def say_something():
s = 'hi'
return s
result = say_something()
print(result)
result
hi
The contents of say_something ()
here are
Substitute 'hi'
for s
and
Returns that s
It has become something like.
result
def what_is_this(color):
print(color)
what_is_this('red')
result
red
function
#Print the object according to the argument
def what_is_this(color):
if color == 'red':
return 'tomato'
elif color == 'green':
return 'green pepper'
else:
return "I don't know."
result = what_is_this('red')
print(result)
result = what_is_this('green')
print(result)
result = what_is_this('blue')
print(result)
result
tomato
green pepper
I don't know.
In this way, if you want to do the same process over and over, define the common part as a function and You should call that function.
Recommended Posts