Monday, May 27, 2024

Diploma Python Programming - Module 04 - Alison 03

Kita akan membuat summary tentang "storing number" di Module ke 4, pelajaran Python programming ini. 


Image - Python programming (credit to Google)

Storing number

width = 20
height = 5

area = width * height
perimeter = 2*(width + height)
print(area)
print(perimeter)
Run Python File:
100
50




Tak bisa digabung string + number
area = 0
width = 20
height = 10

# Triangle
area = width * height/2
print("The area ytiangle would be " + area)
Run Python File:
print("The area ytiangle would be " + area)

Note:
TypeError: can only concatenate str (not "float") to str


Now, we change to %d and %f...> d for decimal and f for float
area = 0
width = 20
height = 10

# Triangle
area = width * height/2
print("The area triangle would be %d" % area)
print("The area triangle would be %f" % area)
Run Python File:
The area triangle would be 100
The area triangle would be 100.000000

we change to %0.1f
area = 0
width = 20
height = 10

# Triangle
area = width * height/2
print("The area triangle would be %d" % area)
print("The area triangle would be %f" % area)
print("The area triangle would be %0.1f" % area)
Run Python File:
The area triangle would be 100
The area triangle would be 100.000000
The area triangle would be 100.0


Next Lesson, Inputting Numbers: Inputting Numbers - Online Course | Alison

Yang diprint string
salary = input("enter your salary: ")
bonus = input("enter your bonus: ")

paycheckamount = salary + bonus

print(paycheckamount)
Run Python File:
enter your salary: 500
enter your bonus: 75
50075 ...> wrong, should be 575

Now, we change string to be float or int
salary = input("enter your salary: ")
bonus = input("enter your bonus: ")

paycheckamount = float(salary) + float(bonus)
paycheckamount = int(salary) + int(bonus)

print(paycheckamount)
Run Python File:
enter your salary: 500
enter your bonus: 75
575

Challenge:
user enter loan cost + interest rate + number of years.

calculate monthly payment: 
M = L [i(1+i)n] / [(1+i)n-1]
M = Monthly payment
L = Loan amount
i = interest rate, i = 5% = 0.05
n = number of payment

loan = input("enter your loan amount: ")
numberpay = input("enter your number of payment: ")
interest = 0.05
MonthlyPay = loan[interest(1+interest)numberpay] / [(1+interest)numberpay-1]

Fix from copilot:
loan = float(input("Enter your loan amount: "))
number_of_payments = int(input("Enter the number of payments: "))
interest_rate = 0.05

monthly_interest_rate = interest_rate / 12
denominator = (1 + monthly_interest_rate) ** number_of_payments - 1

monthly_payment = loan * (monthly_interest_rate * (1 + monthly_interest_rate) ** number_of_payments) / denominator

print(f"Your monthly payment is: ${monthly_payment:.2f}")
Note : The formula for the monthly payment is based on the annuity formula.


VSCode
loan = float(input("Enter your loan amount: "))
number_of_payments = int(input("Enter the number of payments: "))
interest_rate = 0.05

monthly_interest_rate = interest_rate / 12
denominator = (1 + monthly_interest_rate) ** number_of_payments - 1

monthly_payment = loan * (monthly_interest_rate * (1 + monthly_interest_rate) ** number_of_payments) / denominator

print(f"Your monthly payment is: ${monthly_payment:.2f}")
Run Python File:
Enter your loan amount: 140000
Enter the number of payments: 175
Your monthly payment is: $1128.40


When working with variables remember you can store numbers as well as strings, the quotes around the text indicates a string if you want to assign a numeric value to the variable you simply enter the number.

You can perform math operation on variables containing numeric values, the operation symbols for maths in programming are slightly different but many are the same.

Common math operations:+ Addition- Subtraction* Multiplication/ Division** Exponent (is for cubing or the power of a value)% Modulo (is giving you the remainder of a division)

The math rules of order operations are the same in programming as it is in normal mathematics  

After completing this module, you will be able to:
Recognize the process of using dates in your program.
Identify the different methods to format dates in your program.
Recognize the use the same function to format time as you did for dates. 

Next Lesson: Module 05

========================================
========================================

Dave Gray


JJJJJJJJJJJJJJ



No comments:

Post a Comment