Python3.5有68个内建函数(Built-inFunction)
https://docs.python.org/3/library/functions.html
deffunctionName (arg1,arg2):
return 'someThing'
#华氏摄氏温度转换(有返回)
def fahrenheit_convert(C):
fahrenheit=C*9/5+32
return str(fahrenheit)+'F'
print(fahrenheit_convert(30))
#华氏摄氏温度转换(无返回)
def fahrenheit_converter(C):
fahrenheit = C * 9/5 + 32
print(str(fahrenheit) + '˚F')
fahrenheit_convert(30)
注意:批量注释和批量取消注释Ctrl+/
单行注释使用 #
多行注释使用 ''' '''(建议给描述使用) 或 """ """
给函数传参
deftrapezoid_area(base_up, base_down, height):
return 1/2 * (base_up + base_down) * height
trapezoid_area(1,2,3)
trapezoid_area(base_up=1,base_down=2, height=3)
trapezoid_area(height=3,base_down=2, base_up=1) # RIGHT!
trapezoid_area(height=3,base_down=2, 1) # WRONG!
trapezoid_area(base_up=1,base_down=2, 3) # RIGHT!
trapezoid_area(1,2, height=3) # RIGHT!
def text_create(name, msg):
#desktop_path = '/Users/Hou/Desktop/'
desktop_path = r'C:\Users\HouDesktop\'
full_path = desktop_path + name + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print('Done')
text_create('hello','hello world') #调用函数
a=10,b=20
a**b | a 的 b 次方 |
a//b | 取商的整数 |