1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| ## basic def hello_func(): pass
print(hello_func) # <function hello_func at 0x000001FC6BA4C430> print(hello_func()) # None
## function without input def hello_func(): return 'hello' print(hello_func()) # hello
## function with input def hello_func(greeting): return '{} Function.'.format(greeting) print(hello_func('Hi')) # Hi Function.
## default value def hello_func(greeting, name='You'): return '{}, {} Function.'.format(greeting, name)
print(hello_func('Hi')) # Hi, You Function. ## positional arguments, keyword arguments ## 1 def student_info(*args, **kwargs): print(args) print(kwargs)
student_info('Math', 'Art', name='John', age=22) # ('Math', 'Art') # {'name': 'John', 'age': 22} ## 2 courses = ['Math', 'Art'] info = {'name': 'John', 'age': 22} student_info(courses, info) # (['Math', 'Art'], {'name': 'John', 'age': 22}) # {} ## 3 courses = ['Math', 'Art'] info = {'name': 'John', 'age': 22} student_info(*courses, **info) # ('Math', 'Art') # {'name': 'John', 'age': 22} ## example print number of month days month_days = [0,31,28,31,30,31,30,31,31,30,31,30,31]
def is_leap(year): """Return True for leap years, False for 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(days_in_month(2020,2)) # 29 print(days_in_month(2021,2)) # 28
|