Article From:https://www.cnblogs.com/URNOTBENBEN/p/9215545.html

First, function is a function, a method to simplify the code.
def say(name,sex=’Male ‘): function, parameter, formal parameter, variable.
#nameFor required parameters, position
#sexThe default value is the default value, and the default value is not passed.
print(‘%s Ha ha, gender%s’% (name, sex))

The variables in the function are all local variables. They can only be used in functions. When the function is executed, the variable can not be invoked.

def calc(a,b):
res = a * b
return res     #Return value. If return is encountered in the function, the function ends immediately.

If you need to use the result of the function, write return. If you don’t need it, you don’t need to write it.

def my():
for i in range(100):
print(i)
if i==2:
return

Two, small exercises

eg1：Write a program to check whether the input string is decimal.

1、Only a decimal point is used to determine the number of decimal points
2、 In the case of positive decimal numbers, the left and right sides of the decimal point are integers, which is legal [0, 12]

3、In the case of negative decimal, the integer on the right side of the decimal point, and the left side must start with a minus sign, with only a minus sign.
# -5.4
# [‘-5′,’4’]
# [1:]

def check_float(s):
s = str(s)
if s.count(‘.’)==1:
s_list = s.split(‘.’)
left = s_list[0] #’-98’on the left of the decimal point
right = s_list[1]#To the right of the decimal point
if left.isdigit() and right.isdigit(): #Here is the judgment of the decimal
return True
if left.startswith(‘-‘) and left[1:].isdigit() and right.isdigit():
return True
return False

eg2：Define the read and write file functions

def my_file(name,content=None):
with open(name,’a+’) as f:
f.seek(0)
if content:
f.write(content)
else: