Python Tutorial for Beginners - 7. Functions

Table of Contents

  1. Run Code 1 in Python
  2. Run Code 2 in Python
  3. Run Code 3 in Python
  4. Run Code 4 in Python
  5. Run Code 5 in Python
  6. Run Code 6 in Python
  7. Run Code 7 in Python
  8. Run Code 8 in Python
  9. Run Code 9 in Python
  10. Run Code 10 in Python
  11. Run Code 11 in Python

Run Code 1 in Python

1
2
3
4
def hello_func():
    pass

print(hello_func())

start.py

Run Code 2 in Python

1
2
3
4
def hello_func():
    print('Hello Function')

print(hello_func())

start.py

Run Code 3 in Python

1
2
3
4
def hello_func():
    print('Hello Function')

hello_func()

start.py

Run Code 4 in Python

1
2
3
4
5
6
7
def hello_func():
    print('Hello Function')

hello_func()
hello_func()
hello_func()
hello_func()

start.py

Run Code 5 in Python

1
2
3
4
def hello_func():
    return 'Hello Function'

print(hello_func())

start.py

Run Code 6 in Python

1
2
3
4
def hello_func():
    return 'Hello Function'

print(hello_func().upper())

start.py

Run Code 7 in Python

1
2
3
4
def hello_func(greeting):
    return '{} Function. '.format(greeting)

print(hello_func('Hi'))

start.py

Run Code 8 in Python

1
2
3
4
def hello_func(greeting, name = 'You'):
    return '{}, {}'.format(greeting, name)

print(hello_func('Hi', name = 'MT'))

start.py

Run Code 9 in Python

1
2
3
4
5
def student_info(*args, **kwargs):
    print(args)
    print(kwargs)

student_info('Math', 'Art', name= 'John', age=22)

start.py

Run Code 10 in Python

1
2
3
4
5
6
7
8
def student_info(*args, **kwargs):
    print(args)
    print(kwargs)

courses = ['Math', 'Art']
info = {'name': 'John', 'age': 22}

student_info(*courses, **info)

start.py

Run Code 11 in Python

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
# 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."""

    # year 2017
    # month 2
    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))

start.py