Tuesday, June 18, 2024

Diploma Python Programming - Module 07 - Alison 03

 Setelah lulus assessment (Module 06) dengan nilai 96%, maka kita masuk ke Module 07 tentang Making Decision with code.


Image - Python programming (credit to Google).


Kita mulai dengan topik if statement (part 1).

if answer == "yes"
answer = input("would you like express shipping?")
if answer == "yes":
    print("will be an extra $10")
print("have a nice day")
Run python file:
would you like express shipping?yes
will be an extra $10
have a nice day

if answer == "no"
answer = input("would you like express shipping?")
if answer == "yes":
    print("will be an extra $10")
print("have a nice day")
Run python file:
would you like express shipping?no
have a nice day

if answer == "yes".....> 3 print, dengan 1 print un-indent
answer = input("would you like express shipping?")
if answer == "yes":
    print("will be an extra $10")
    print("but expensive for me")
print("have a nice day")
Run python file:
would you like express shipping?yes
will be an extra $10
but expensive for me
have a nice day

Note: Python is "case" sensitive----> perhatikan huruf besar atau kecil.


Continue to if statement part 2: If Statements - Part II - Online Course | Alison

Deposit = 150
deposit = 150
if deposit > 100:
    print("You get a free toaster")
print("Have a nice day")
Run python file:
You get a free toaster
Have a nice day

Deposit = 50
deposit = 50
if deposit > 100:
    print("You get a free toaster")
print("Have a nice day")
Run python file:
Have a nice day


Deposit = int(input)
deposit = int(input("how much do you want to deposit? "))
if deposit > 100:
    print("You get a free toaster")
print("Have a nice day")
Run python file:
how much do you want to deposit? 125
You get a free toaster
Have a nice day


Continue to Branching: Branching - Online Course | Alison

If deposit > 100
deposit = float(input("how much to deposit? "))
if deposit > 100:
    print("you get a free toaster")
else:
    print("you get a mug")
print("have a nice day")
Run python file:
how much to deposit? 110
you get a free toaster
have a nice day

if deposit < 100 
deposit = float(input("how much to deposit? "))
if deposit > 100:
    print("you get a free toaster")
else:
    print("you get a mug")
print("have a nice day")
Run python file:
how much to deposit? 95
you get a mug
have a nice day

if deposit = 100 
deposit = float(input("how much to deposit? "))
if deposit > 100:
    print("you get a free toaster")
else:
    print("you get a mug")
print("have a nice day")
Run python file:
how much to deposit? 100
you get a mug
have a nice day

# Boolean variable: True or False
Deposit > 100
deposit = int(input("how much to deposit? "))
if deposit > 100:
    freeToaster = True

if freeToaster:
    print("Enjoy your Toaster")
print("have a nice day")
Run python file:
how much to deposit? 110
you get a free toaster
have a nice day

Jika kita letak deposit < 100, maka akan terjadi:
error: NameError: name 'freeToaster' is not defined
Maka, kita harus tambah coding: freeToaster = False di atas.
freeToaster = False
deposit = int(input("how much to deposit? "))
if deposit > 100:
    freeToaster = True

if freeToaster:
    print("Enjoy your Toaster")
print("have a nice day")
Run python file:
how much to deposit? 80
you get a mug
have a nice day

Constraints for checking your if statements:
== is equal to
!= is not equal to
< is less than
> Is greater than
<= is less than or equal to
>= is greater than or equal to

When checking two strings are equal to each other with an if statement, it is often best to convert both of them to upper or lower case, as python is case sensitive and will not properly work less both are the same case.

There are generally two different ways to write an if statement you should always consider which method makes the most sense and is the simplest. 


Continue: Module 8: Complex Decisions with Code

Thursday, May 30, 2024

Diploma Python Programming - Module 05 - Alison 03

 Akhirnya kita sampai ke bagian ke 5 (Module 5), sebelum assessment. Topiknya berkaitan dengan Date dan Time.


Image - Python programming

Module 5, date and time: Dates - Online Course | Alison

Kita akan mulai Summary tentang tanggal dan waktu.
 

First, Install datetime library into Vstudiocode
> python -m pip install datetime

Install dateutil
python -m pip install python-dateutil

import datetime
currentDate = datetime.date.today()
print(currentDate)
print(currentDate.year)
print(currentDate.month)
Run Python File:
2024-05-30
2024
5....> month, May 2024


Bagian dua dari Module 05, bicara tentang date formats.

datetime and dateutil
> python -m pip install datetime
python -m pip install python-dateutil
import datetime
from dateutil.relativedelta import relativedelta

# Get today's date
currentDate = datetime.date.today()

# Get user input for birthday
userInput = input("Enter your birthday (mm/dd/yyyy): ")
birthday = datetime.datetime.strptime(userInput, "%m/%d/%Y").date()

# Calculate age using dateutil
age = relativedelta(currentDate, birthday).years

print(f"Your age is {age} years.")

days = birthday - currentDate
print(days)
Run Python File:
Enter your birthday (mm/dd/yyyy): 4/13/2023
Your age is 1 years.
-414 days, 0:00:00


import datetime
currentTime = datetime.datetime.now()
print(currentTime)
print(currentTime.hour)
print(currentTime.minute)
print(currentTime.second)
Run Python File:
2024-05-31 13:14:46.764686
13...> 13:14:46 PM
14
46


Key Points Summary:

For working with dates you will need to import the date library into your program.
library is a collection of precompiled routines and methods for you to use in your program.

Import is a function for making objects and libraries available to your program.
Without importing libraries, certain functions won’t be available to you, they won’t even show up on the InteliSense.



Complete this assessment to review your understanding of the following modules: - Introduction to Python Programming, - Displaying Text; - String Variables; - Storing Numbers; - Working with Dates and Time. You can repeat this assessment to pass it. (80%)

Score: 44%
1) 

JJJJJJ

Visual Studio is only an editor for creating programming applications. You can use other applications for creating Python programs. Answer: TRUE

If IntelliSense is not prompting you with a function, you may need to _____________ your variable. Answer: Initialize


Which line of code will work? Choose one.

There are a lot of different versions of Python that you could use, select three versions from the following list:

  • Answer: There are a lot of different variations of Python, IronPython, Ipython, CPython to name a few. Most of python’s variations only have some syntax differences but not too many.
  • IronPython, JPython, Jython

Which is the right method for creating a new variable that will hold a string? Choose one.

answer: var myName = "your name"; "your name" is called a string literal. 
Name = 'bob'

For converting data types you have different options depending on the type you want to convert to. Match the description to the conversion method from the drop-down list.

What is the symbol for commenting in Python?

Choose one. What is the mathematic operation for multiplication?

Fill in the blank. Visual Studio Express is a ____________ version of Visual Studio. Type the correct answer into the text box.

Answer: Visual Studio Express was an earlier version of Visual Studio, designed for specific use cases.
They are function-limited version of the non-free Visual Studio and require mandatory registration. Express editions started with Visual Studio 2005.

Fill in the blank. Most programmers use a casing scheme with their code. Two common types are ___________Casing and camelCasing. Type the correct answer into the text box.

Answer: pascalCasing

Fill in the blank. Strings and variables can be combined with the ________ symbol. Type the correct answer into the text box.

answer: Strings and variables can be combined using the plus symbol 

Fill in the blank. The mathematic operation module (%) gives you the of a division.

answer: The modulo operator (%) gives you the remainder of a division.

Fill in the blank. When reading user input for dates you will need to ___________ the user input to the date format.

Answer:  When reading user input for dates you will need to accept the user input to the date format.

In Python you can use triple quotes to enclose your text in the print statement to have your text display as you have typed into the editor. Why is this not a recommended method? Choose one.


Fill in the blank. The first program you write is hello world, the importance of writing this program first is to make sure that the programs and tools are ____________ correctly. Type the correct answer into the text box.

answer: working 

Choose one. You can format the date output you are using with the ____________ function.

Answer: you can use the following functions: strftime

Fill in the blank. For working with time in your code you will use the ____________ library.

Answer: datetime

Fill in the blank. Programmers do not memorize all the functions. When they need to use functions they use IntelliSense, _____________ or Internet searches. Type the correct answer into the text box.

Answer: documentation

Fill in the blank. Saving numeric variables as decimal/integer types is for storing ______________ numbers. Answer: whole

Fill in the blank. print(), .lower() and .upper() are all different _____________ you can use in python. Answer: Methods

Choose one. What does the date 12/06/09 represent? Answer: Depends on the formats you are using.

Choose one. To assign todays date to a variable call currentDate you would use which line of code? Answer: currentDate = datetime.date.today()

Which is the sign for assigning a value to a variable? Choose one. Answer =

True or False: You can use reserved words such as print for variable names. Answer: FALSE.

Choose one. Which line of code prints out todays date that has been assigned to the variable currentDate? Answer: print(currentDate)

Fill in the blank. For adding a new python file to your visual studio project you right click on the name of your project in the __________ explorer and add a new item. Answer: solution

Which of the following would be good names for variables? Choose two.

What is the keyboard short cut for launching your program with Debugging? Choose one.

answer: you can use the F5 keyboard shortcut.

Choose one. Which is the right method for inserting a float place holder with 3 decimal points ?

Answers: 1)Using f-strings ....> formatted_number = f"The number is {num:.3f}"
2) Using the format() method....>formatted_number = "The number is {:.3f}".format(num) 

Choose two recommended methods to help fix mistakes you can’t find or resolve in your code.

You have created a variable call message containing the text “Hello World” and wish to print it out in all upper case, which is the right line to do so? Choose one.

answer: print(message.upper())

True or False: You cannot change the value of a variable later in the code.

Choose one. Which is the right method for declaring a numeric variable?

Fill in the blank. For adding a numeric variable to a string in your print statement you need to put in a __________ holder for the value displaying.

Answer: place holder

Choose one. For assigning a value to the place holder in the following line of code Print(“I have {0:d) dogs” ), what would you add?

answer: .format(4)
JJJJJJJ


JJJJJJJJJJJJJJJJJ

Wednesday, May 29, 2024

Web Scrapping Courses - Chapter 01, 02 & 03

Ada ribuan jobs untuk web scraping, apakah full time, part time atau freelance di platform seperti UpWork, Amazon MTurk dan Indeed.


Image Web Scrapping (credit to Google)

Keahlian web scrapping biasanya dimasukkan kedalam keahlian sebagai Data Engineer, Data Scientist, and Web Content Specialist.



Langkah langkah install scrapy di VSC
> python -m venv scrapycourse
> scrapycourse\Scripts\activate
(scrapycourse) C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp> pip list
pip        24.0
setuptools 65.5.0

(scrapycourse) C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp> pip install scrapy

(scrapycourse) C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp>scrapy startproject codebear

(scrapycourse) C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp>
==================================

Chocolate project
(scrapycourse) C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp>scrapy startproject chocolatescraper
New Scrapy project 'chocolatescraper', using template directory 'C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp\scrapycourse\Lib\site-packages\scrapy\templates\project', created in:
    C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp\chocolatescraper        

You can start your first spider with:
    cd chocolatescraper
    scrapy genspider example example.com

(scrapycourse) C:\Users\Evi\.vscode\WebScrapCourse\FreeCodeCamp>





JJJJJJJJJJJJJJJJJJ

Selain kursus, ada dua website penting untuk install Python libraries yang berguna dalam web scraping:



JJJJJJJJJJJJJJJJJJJJJJJ

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