-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.Functions.py
65 lines (35 loc) · 1.81 KB
/
8.Functions.py
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
def hello_func1():
pass # Means don't do anything for now, so that it doesn't give errors
print(hello_func1(), '\n') # None, since there is nothing in the function
def hello_func2():
print('Hello Function\n')
hello_func2()
def hello_func3():
return 'Hello Function\n'
print(hello_func3().upper()) # Can use methods on return variable
def hello_func4(greeting, name='You'): # Default parameter for name
return '{} {} Function.'.format(greeting, name)
print(hello_func4('Whatsupp', 'Vishnu'), '\n') # Have to pass positional arguments before keyword arguments
def student_info(*args, **kwargs): # Allows to accept an arbitrary number of positional or keyword arguemtns
print(args)
print(kwargs)
student_info('Math', 'Art', name='John', age=22) # Math and Art are printed as a tuple in the positional arguent, kwargs is a dictionary with keywords and values in the keyword argument
print('')
courses = ['Math', 'Art']
info = {'name': 'John', 'age': 22}
student_info(courses, info) # Output {}, instead of passing the values in individually, it passed it both as positional arguments
print('')
student_info(*courses, **info) # Output same as first, Upacks the values and passes them individually
print('')
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # List, First index is just a placeholder it is not going to be used.
def is_leap(year):
"""Return True for leap years, false for non-leap years.""" # Docstring for describing program
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(2017, 2))