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

No comments:

Post a Comment