Professional Documents
Culture Documents
https://docs.python.org/3/library/datetime.html
1
Walk through exercise – date
datetime.date(year, month, day)
Step 1 from datetime import datetime, date, time, timedelta
Step 3 print(to_day)
Todo: Explore the properties of date
to print the day, year, month …
Task a print(to_day.xxxx)
Step 1 import datetime
2
Walk through exercise – datetime
datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0)
Step 1 from datetime import datetime, date, time, timedelta
Step 3 print(independence_day )
Todo: print out how old is Singapore this year?
Task b print(xxxx)
Returns a string representing the date following the given format. Can be called from
date, datetime objects.
The following example prints out the current date and time
Step 1 from datetime import datetime, date, time, timedelta
Other useful directives:
Step 2 moment = datetime.now() %a ‐ day of the week (short form)
%A ‐ day of the week
%B ‐ Month in English
Step 3 print(moment.strftime("%d/%m/%y")) %Y ‐ year
%m ‐ month
Step 4 print( moment.strftime("%H:%M")) %d ‐ day
%H ‐ hour
%M ‐ minute
%S ‐ second
3
Task 6.1 File Creation (Text File)
Complete the following python program to ask user for name, and body
temperature in celsius. Record down current the details (date, time,
name and temperature) into a text file named: “temperature.txt”.
filename = _________________ # create a variable to keep file name
name = ______("May I have your name? ") # Getting user input
temperature = ______("What's your body temperature in celsius?")
moment = _________.now() # Get current date and time
datenow = _______.________("%d/%m/%y") # Obtain a string display on date today from moment
timenow = ________._______("%H:%M") # Obtain a string display on time now from moment
file = ______(filename, ___) # open the file for writing
# construct a row representation containing the details from the above variables
row = datenow + “," + ____________ + "," + ____________ + "," + ____________
file.______(____) # write the row to file
____.______() # closing the file
import ___
filename = _____________
name = _____________
temperature = _____________
moment = _____________
datenow = _____________
timenow = _____________
____ open(filename, ___, , newline="") as _________:
csv_pointer = csv.______(file_pointer)
# create list of data
row = [_________ , _________, _________, _________]
_________.writerow(___)
4
Task 6.3a Revenue Report
1. Write a function that gets user to input the monthly revenues for the
past quarters. It then returns a variable of list data type to caller with
the corresponding values.
2. Then generate a revenue report denoting the month, revenue, and
cumulative total for each month. It ends with a message that sums
up the total revenue. Appropriate formatting has to be in place for
numeric numbers (decimals and comma) and spacing. The output
may look like the following.
5
Task 6.4 File Reading
Write a Python program to read in the content from the earlier file,
“temperature.txt”. The program will process the data and print out Red
Alert if the temperature is above 37.5 degree Celsius. Try changing the
temperature from text file to verify if it prints out the Red Alert
accordingly. Nothing will be printed if temperature is 37.5 and below.
See the text file on the left diagram and program output from the right.