【python----定义函数】
# 创建函数
def info():
# age = 18
# name = 'lxc'
# print(age,name)
print('我是李轩臣')
print('我21')
info() # 调用函数 执行一次
# 我是李轩臣
# 我21
# 定义函数求和
def he():
a=5
b=3
print(a+b)
he()
def mdmP():
print('马什么梅?')
input('')
print('马东什么?')
input('')
print('o,马冬梅!')
mdmP()
def info():
print('*'*20)
return 6,7
print('------------')
return 5,6
info()
# 定义函数 查找元素是否在列表里 在 返回 位置 不在 返回 -1
# 两种方法
l = [1,2,3,6,5,8,9]
def info():
'''
:return: index -------- -1
'''
for i in l :
i = 5
if i in l:
return l.index(i)
else:
return -1
li = info()
print(li)
def findl():
l = [78,64,2,87,5]
for i in range(len(l)):
if l[i] == 2:
return i
else:
return -1
findl()
# 取最大值
def max_value():
l = [99,66,741,9898]
max_n = l[0]
for i in range(len(l)):
if l[i] > max_n:
max_n = l[i]
return max_n
re = max_value()
print(re)
a = 5
b= 6
c=1
def max_l():
re = (a if a>b else b) if (a if a>b else b)>c else c
rt = (a if a<b else b) if (a if a<b else b)<c else c
return re,rt
li = max_l()
print(li)
# 9* 9
def nine_nine():
for i in range(1,10):
for j in range(1,i+1):
print(f'{i}*{j}={i*j}',end='\t')
print()
nine_nine()
# re = [f'{i}*{j}={i*j}']
# 计算 字符长度
def info():
s= "hello word "
total =0
for i in s:
total = total+1
print(total)
info()
# 返回多个值
def rey_demo():
return 1,2,3,6,58,6
a,b,c,d,e_,f = rey_demo() # 拆包
re = rey_demo()
print(re)
print(e_)
a,_,_,_,_,f = rey_demo()
a,*_=rey_demo()
print(*_)
# 函数的参数
# 封闭原则
def paramater(s):
toatal = 0
for i in s:
toatal = toatal+1
return toatal
print(paramater('helloword'))
def info(name):
print(f"wojiaolixuanche{name}")
info('李先生')
def j(row,colime): # 形参
for i in range(row):
for j in range(1,colime):
print(f"*",end=" ")
print()
j(9,9) # 实参
def isexist(l,num):
for i in range(len(l)):
if l[i] == num:
return i
return -1
re = isexist([1,2,3,6,51,49,2,3,23,],3) # 传值
print(re)