-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Python provides a built-in module called time
to work with time-related tasks like delays, timestamps, and performance measurement.
You need to import it before use:
import time
print(time.time())
⏱️ Returns number of seconds since January 1, 1970 (Unix Epoch).
print("Start")
time.sleep(2)
print("End after 2 seconds")
local = time.localtime()
print(local)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
Code | Meaning |
---|---|
%Y |
Year (2025) |
%m |
Month (06) |
%d |
Day (21) |
%H |
Hour (24-hour) |
%I |
Hour (12-hour) |
%M |
Minutes |
%S |
Seconds |
%p |
AM/PM |
t = time.strptime("21 Jun 25", "%d %b %y")
print(t)
start = time.perf_counter()
# your code here
end = time.perf_counter()
print("Execution time:", end - start)
for i in range(5):
print(i)
time.sleep(1) # wait 1 second
Use sleep()
to delay output, format current time, and calculate code execution time.
Q1. Write a Python program to import the time
module.
Q2. Write a Python program to print the current Unix timestamp using time.time()
.
Q3. Write a Python program to pause the execution for 3 seconds using time.sleep(3)
.
Q4. Write a Python program to format and print the local time as HH:MM:SS
using time.strftime()
.
Q5. Write a Python program to show the full current date and time using strftime()
.
Q6. Write a Python program to use sleep()
inside a loop of 5 iterations, printing the count each second.
Q7. Write a Python program to convert the string "15 Aug 25"
to a time struct using time.strptime()
.
Q8. Write a Python program to use perf_counter()
to measure execution time of a code block.
Q9. Write a Python program to get local time using time.localtime()
and print it.
Q10. Write a Python program to print only the current minute and second from the current time.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms