Python time Module

→ All of these assume:

import time

⏱️ Python time Module – 20 Basic Assignments


1) Print Current Time (Seconds Since Epoch)

Use time.time() to print how many seconds have passed since January 1, 1970.


2) Print Current Local Time Tuple

Call time.localtime() and print the full tuple.


3) Print Current Hour

Extract and print just the hour from time.localtime().


4) Print Current Minute

Extract and print the minute.


5) Print Current Second

Extract and print the second.


6) Pause for 3 Seconds

Use time.sleep(3) then print:


7) Simple Timer

Record start time, sleep 2 seconds, record end time, then print elapsed seconds.


8) Measure Loop Time

Measure and print how long it takes to run a loop that prints numbers 1–5.


9) Format Time String

Use time.ctime() on current timestamp and print.


10) Delay With Countdown

Use time.sleep(1) to build a countdown from 5 to 1.


11) Custom Delay Input

Ask the user for a number n and pause for n seconds before printing “Done!”.


12) Local Time to Readable

Get time.localtime() and format with time.asctime().


13) Time Struct Members

Print year, month, and day from a time.localtime() struct.


14) Convert Time Tuple to Timestamp

Use time.mktime() on a time tuple and print the result.

Example tuple:


15) Sleep and Print Time

Record and print time before and after a sleep of 2 seconds.


16) Print UTC Time

Use time.gmtime() and print the result.


17) Pause Loop Execution

Print numbers 1–3 with a 1-second pause between each.


18) Timer Function

Write a function timer(n) that waits n seconds and prints elapsed time.


19) Timestamp Then Local Time

Get epoch seconds using time.time() and convert to local time using time.localtime().


20) Print ISO-like Time

Combine parts from time.localtime() to print time like:


🧠 Concepts Practiced

Topic
Example Methods

Seconds since epoch

time.time()

Pause execution

time.sleep()

Local time tuple

time.localtime()

UTC time tuple

time.gmtime()

Human-readable time

time.ctime(), time.asctime()

Convert tuple to timestamp

time.mktime()


📌 Starter Snippets

Get current timestamp

Get local time

Print just hour, min, sec

Pause execution

Measure elapsed time


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated