Try sorting the list
org_list = [1, 20, 19, 4, 3]
sort_list = sorted(org_list)
print('org_list'+ str(org_list))
print('sort_list'+ str(sort_list))
Try to run
>test09_function.py
org_list[1, 20, 19, 4, 3]
sort_list[1, 3, 4, 19, 20]
I think you can sort and write by yourself, but you only need one line.
def function name (argument 1, argument 2):
Processing
Processing
Return Return value
Let's make the previous process a function
If you give a list, display the original list and the sorted list (try coding without looking at anything for the first time here)
def my_sort(org_list):
sort_list = sorted(org_list)
print('org_list'+ str(org_list))
print('sort_list'+ str(sort_list))
return
my_list = [1, 20, 19, 4, 3]
my_sort(my_list)
Try to run
>test10_myFunction.py
org_list[1, 20, 19, 4, 3]
sort_list[1, 3, 4, 19, 20]
Oh, it worked in one shot. I'm happy.
Earlier I tried a function with simple arguments. The return value is not dealt with in particular. Check the arguments and return value a little more here
There are the following cases about the return value
Case where return is not written >> Return value: None
Case of handling return without argument >> Return value: None
Case where return is handled with arguments >> Return value: Specified argument
def my_function1(a):
print('in function1:'+ str(a))
def my_function2(b):
print('in function1:'+ str(b))
return
def my_function3(c):
print('in function1:'+ str(c))
return 1
def my_function4(d):
print('in function1:'+ str(d))
return 'aaa'
aa = my_function1(5)
print (aa)
bb = my_function1(10)
print (bb)
cc = my_function1(15)
print (cc)
dd = my_function1(20)
print (dd)
>test11_myFunction2.py
in function1:5
None <<<Return value without return
in function2:10
None <<<Return value of return with no arguments
in function3:15
1 <<<return value of return 1
in function4:20
aaa <<< return 'aaa'Return value of
Function arguments can be handled as follows
Handle multiple arguments
"Keyword argument" that allows you to specify arguments in no particular order
Arguments with an undetermined number (variadic arguments)
Any number of arguments such as print () are OK
Multiple arguments and keyword arguments
def my_function(arg1, arg2, arg3):
print(str(arg1)+str(arg2)+str(arg3))
aa = arg1 * arg2 * arg3
print(aa)
return
my_function(2, 3, 4)
my_function(arg2=2, arg1=3, arg3=4)
Try to run
>test12_myFunction_arg.py
234
24
324 <<<Passed arguments out of order
24 <<<Naturally the result is the same
help (function name)
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
>>> help(print)
If you think that print just prints, you can specify a delimiter or write to a file. Sometimes when you don't want to start a new line at the end.
range function
A function that creates a serial number of numbers
a = range(5)
b = range(2, 5)
print(a)
print(list(a))
print(b)
print(list(b))
for i in range(4, 7):
print(i)
Try to run
>test13_range.py
range(0, 5) <<<Even if 0 is omitted, range(0, 5)Becomes
[0, 1, 2, 3, 4]
range(2, 5)
[2, 3, 4]
4 <<<<It seems that it can be used in a for statement
5
6
Recommended Posts