Write Functions in Python (Python Part 10)

This class teaches you how to write and use custom functions in Python.

  • 00:00 Intro
  • 06:25 Name Space, Flow Execution, Variable Scope
  • 12:32 Basic Function
  • 15:47 Sending Variables to a Function
  • 20:47 Function Returns
  • 27:23 Multiple Return Values
  • 34:05 Scope and Flow Examples
  • 42:20 Mortgage Repayment Example
  • 55:27 Final Thoughts

basic.py

hello()

def hello():
    print('hello world.  This is  cool!!!')

basic-variable.py

def say_hi(name):
    print(f'Hello {name}')

person = 'bob'

say_hi(person)

people = ['bob', 'sue', 'tim', 'fred']

for person in people:
    say_hi(person)

return.py

def porn_name(pet, street):
    name = f'{street} {pet}'
    return name

first_pet = 'goat'
first_street = 'main'

response = porn_name(first_pet, first_street)

print(f'Your Porn name is {response}')

multiple-variable.py

def my_math(num1, num2):
    answer = num1 + num2
    return answer

price1 = 885.12
price2 = 90.45

response = my_math(price1, price2)

print(response)

multiple-return.py

def price_stat(price):
    high = max(price)
    low = min(price)
    average = sum(price) / len(price)
    return high, low, average

price = [11.20, 55.60, 27.33, 25.66]

response = price_stat(price)

print(response)

print(f'max: {response[0]} min: {response[1]} avg: {response [2]}')

var-scope.py

message = 'words are cool'

def bad_function():
    global bad_var
    bad_var = 'bob'
    print(message)

bad_function()

print(bad_var)

mortgage.py

def mortgage(total, payment, interest):
    interest = (interest / 12) / 100
    month = 0
    paid = 0
    total_interest = 0
    while total >= 0:
        total_interest = (total * interest) + total_interest
        total = total - payment
        total = (total * interest) + total
        paid = paid + payment
        month += 1
        print(total)
    year = month / 12
    return year, paid, total_interest

price = 100000
interest = 7
payment = 400

response = mortgage(price, payment, interest)

print(f'Repayment Years: {response[0]}')
print(f'Total Paid: {response[1]}')
print(f'Interest Paid: {response[2]}')

mortgage-fixed.py

def mortgage(total, payment, interest):
    interest = (interest / 12) / 100
    month = 0
    paid = 0
    total_interest = 0
    if payment > (total * interest):
        while total >= 0:
            total_interest = (total * interest) + total_interest
            total = total - payment
            total = (total * interest) + total
            paid = paid + payment
            month += 1
        year = month / 12
    else:
        print('Minimum Payment is Too Low to EVER be Paid off')
        year = 'error'
        paid = 'error'
        total_interest = 'error'
    return year, paid, total_interest

price = 100000
interest = 7
payment = 70

response = mortgage(price, payment, interest)

print(f'Repayment Years: {response[0]}')
print(f'Total Paid: {response[1]}')
print(f'Interest Paid: {response[2]}')

Be the first to comment

Leave a Reply