Python - Functions
** Functions
==========================================
def hello_func():
pass
print(hello_func())
---------------------------------
None
---------------------------------
==========================================
def hello_func():
pass
print(hello_func)
---------------------------------
function hello_func at 0x008C3FA0
---------------------------------
==========================================
def hello_func():
print('Hello Function!')
hello_func()
---------------------------------
Hello Function!
---------------------------------
==========================================
def hello_func():
print('Hello Function!')
print('Hello Function!')
print('Hello Function!')
print('Hello Function!')
print('Hello Function!')
---------------------------------
Hello Function!
Hello Function!
Hello Function!
Hello Function!
---------------------------------
==========================================
def hello_func():
print('Hello Function!')
hello_func()
hello_func()
hello_func()
hello_func()
---------------------------------
Hello Function!
Hello Function!
Hello Function!
Hello Function!
---------------------------------
==========================================
def hello_func():
return 'Hello Function!'
hello_func()
---------------------------------
---------------------------------
==========================================
def hello_func():
return 'Hello Function!'
print(hello_func())
---------------------------------
Hello Function!
---------------------------------
==========================================
def hello_func():
return 'Hello Function!'
print(hello_func().upper())
print(len('Test'))
---------------------------------
HELLO FUNCTION!
4
---------------------------------
==========================================
def hello_func(greeting):
return '{} Function!'.format(greeting)
print(hello_func())
---------------------------------
TypeError: hello_func() missing 1 required positional argument: 'greeting'
---------------------------------
==========================================
def hello_func(greeting):
return '{} Function!'.format(greeting)
print(hello_func('Hi'))
---------------------------------
Hi Function!
---------------------------------
==========================================
def hello_func(greeting, name = 'You'):
return '{}, {}'.format(greeting, name)
print(hello_func('Hi'))
---------------------------------
Hi, You
---------------------------------
==========================================
def hello_func(greeting, name = 'You'):
return '{}, {}'.format(greeting, name)
print(hello_func('Hi', name = 'HAN'))
---------------------------------
Hi, HAN
---------------------------------
==========================================
def hello_func(greeting, name = 'You'):
return '{}, {}'.format(greeting, name)
def student_info(*args, **kwargs):
print(args)
print(kwargs)
student_info('Math', 'Art', name = 'John', age=22)
---------------------------------
('Math', 'Art')
{'name': 'John', 'age': 22}
---------------------------------
==========================================
def student_info(*args, **kwargs):
print(args)
print(kwargs)
courses = ['Math', 'Art']
info = {'name' : 'John', 'age' : 22}
student_info(courses, info)
---------------------------------
(['Math', 'Art'], {'name': 'John', 'age': 22})
{}
---------------------------------
==========================================
def student_info(*args, **kwargs):
print(args)
print(kwargs)
courses = ['Math', 'Art']
info = {'name' : 'John', 'age' : 22}
student_info(*courses, **info)
---------------------------------
('Math', 'Art')
{'name': 'John', 'age': 22}
---------------------------------
==========================================
# Number of days per month, First value placeholder for indexing purposes.
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(is_leap(2020))
print(days_in_month(2020,2))
print('-'*80)
print(is_leap(2021))
print(days_in_month(2021,2))
---------------------------------
True
29
--------------------------------------------------------------------------------
False
28
---------------------------------