# define the function
def hello_func():
pass # do not do anything with this for now but it won't throw any errors for leaving it blank
hello_func() # run the function
# define the function
def hello_func():
print("hello function!")
hello_func() # run the function
'''
out:
hello function!
'''
# define the function
def hello_func():
return "hello function!"
hello_func() # run the function
'''
out:
[Finished in 53ms]
'''
print(hello_func())
'''
out:
hello function!
[Finished in 52ms]
'''
def hello_func(gretting):
return '{} function!'.format(gretting)
print(hello_func('hello'))
'''
out:
hello function!
[Finished in 56ms]
'''
def hello_func(gretting, name='you'):
return '{}, {}!'.format(gretting,name)
print(hello_func('hello'))
'''
out:
hello, you!
[Finished in 56ms]
'''
print(hello_func('hello','Eric'))
'''
hello, Eric!
'''
接收任意数量的
def student_info(*args, **kwargs):
print(args)
print(kwargs)
student_info('Math', 'Art', name='John', age=22)
'''
out:
('Math', 'Art')
{'name': 'John', 'age': 22}
[Finished in 53ms]
'''
#--------
def student_info(*args, **kwargs):
print(args)
print(kwargs)
courses = ['Math', 'Art']
info = {'name': 'John', 'age': 22}
student_info(courses, info)
'''
out:
(['Math', 'Art'], {'name': 'John', 'age': 22})
{}
'''
student_info(***courses, **info**)
'''
out:
('Math', 'Art')
{'name': 'John', 'age': 22}
[Finished in 53ms]
'''
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap(year):
'''Return True or False for leap years or non-leap years.'''
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def days_in_month(year, month):
'''Return number of days in that month in that year.'''
if not 1 <= month <= 12:
return 'Invalid month'
if month == 2 and is_leap(year):
return 29
return month_days[month]
print(is_leap(2017))
print(is_leap(2020))
print(days_in_month(2017,2))
print(days_in_month(2020,2))
'''
out:
False
True
28
29
[Finished in 54ms]
'''