Python Articles

Page 125 of 855

How do I get an ISO 8601 date in string format in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 66K+ Views

The ISO 8601 standard defines an internationally recognised format for representing dates and times. ISO 8601 is a date and time format that helps remove different forms of the day, date, and time conventions worldwide. In this article, we will discuss several methods to get an ISO 8601 date in string format in Python. ISO 8601 Date Format In Python, ISO 8601 date is represented as "YYYY-MM-DDTHH:MM:SS.mmmmmm" format. For example, August 25, 2023, is represented as 2023-08-25T14:35:45.123456. YYYY: Year (four digits) MM: Month (from 1-12) ...

Read More

How to convert Python DateTime string into integer milliseconds?

Pranav Indukuri
Pranav Indukuri
Updated on 24-Mar-2026 7K+ Views

Python provides the time and datetime modules to convert a DateTime string into integer milliseconds. Key functions include time.time(), which gives the current time in seconds, and datetime.timestamp(), which converts datetime objects directly into seconds since the epoch. By multiplying these values by 1000, we can get the time in milliseconds. Using time.time() Method The time module in Python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch, which returns a floating-point number expressed in seconds. This value ...

Read More

How to compare calendar.timegm() vs. time.mktime() in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 1K+ Views

In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC. Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone. Understanding time.mktime() in Local Time Context The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC). ...

Read More

How to convert timestamp string to datetime object in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 23K+ Views

In many real-world applications, timestamps are used to represent dates and times, but they are not human-readable. To make them understandable or use them in various datetime manipulations, it's essential to convert them into Python's datetime object. Python's datetime module provides multiple functions to convert timestamps to datetime objects. Below are the various methods to accomplish this task ? Using datetime.fromtimestamp() Function Using datetime.fromtimestamp() & strftime() Using datetime.strptime() Function Parsing Mixed Text Using strptime() Function Using datetime.fromtimestamp() Function ...

Read More

How to measure elapsed time in python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

To measure time elapsed during program execution, Python provides several methods. The most common approaches are using time.time(), time.perf_counter(), or the timeit module for benchmarking purposes. Using time.time() The simplest method uses time.time() to capture timestamps before and after code execution − import time t0 = time.time() print("Hello") time.sleep(0.001) # Simulate some work t1 = time.time() - t0 print("Time elapsed:", t1, "seconds") Hello Time elapsed: 0.0015020370483398438 seconds Using time.perf_counter() (Recommended) For more precise measurements, use time.perf_counter() which provides the highest available resolution − import time ...

Read More

How to compare Python string formatting: % with .format?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 299 Views

Python provides two main approaches for string formatting: the older % formatting (printf-style) and the newer .format() method. Understanding their differences helps you choose the right approach for your code. % Formatting Issues The % operator can take either a variable or a tuple, which creates potential confusion. Here's a common pitfall ? my_tuple = (1, 2, 3) try: result = "My tuple: %s" % my_tuple print(result) except TypeError as e: print(f"Error: {e}") Error: not enough arguments for format string ...

Read More

How to get the timing Execution Speed of Python Code?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

Measuring the execution time of Python code is essential for performance optimization and benchmarking. Python provides several built-in modules like time and timeit to accurately measure code execution speed. Using time Module The time.perf_counter() function provides the highest available resolution and is recommended for measuring short durations ? import time t0 = time.perf_counter() print("Hello") t1 = time.perf_counter() print("Time elapsed:", t1 - t0, "seconds") Hello Time elapsed: 2.7499999851558823e-05 seconds Using time.time() For wall-clock time measurement, you can use time.time() ? import time start = time.time() # Simulate ...

Read More

How to find if 24 hrs have passed between datetimes in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 3K+ Views

To determine if 24 hours have passed between two datetimes in Python, you need to calculate the time difference and compare it to a 24-hour duration. The datetime module provides tools to perform this calculation using timedelta objects. Basic Approach Using total_seconds() Subtract two datetime objects to get a timedelta, then use total_seconds() to compare with 24 hours in seconds ⏤ from datetime import datetime NUMBER_OF_SECONDS = 86400 # seconds in 24 hours first = datetime(2017, 10, 10) second = datetime(2017, 10, 12) if abs((second - first).total_seconds()) > NUMBER_OF_SECONDS: ...

Read More

How do I get time of a Python program\'s execution?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 706 Views

Python provides different ways to measure the execution time of a script or specific code segments. The most common approaches use functions from the time module like time.time() and time.process_time(), or the dedicated timeit module for precise measurements. Here are the main methods to measure execution time in Python: Using time.time() Function Using time.process_time() Function Using timeit Module Using time.time() Function The time.time() function returns the current time as a floating-point number representing seconds since the Unix epoch (January 1, 1970). This ...

Read More

How to measure time with high-precision in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 5K+ Views

Python provides various modules, such as time, datetime, and timeit, to measure time with high accuracy. These modules offer high-resolution clocks to measure time intervals with precision needed for performance analysis and benchmarking. The following are several methods used to measure time with high precision in Python. Using time.time() Method Using time.perf_counter() Function Using timeit.default_timer() Using time.time() Method for Simple Timing The time.time() method returns the current time in seconds since the epoch as a floating-point number. The epoch is system-dependent, but ...

Read More
Showing 1241–1250 of 8,549 articles
« Prev 1 123 124 125 126 127 855 Next »
Advertisements