Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to measure time with high-precision in Python?
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 MoreHow to get min, seconds and milliseconds from datetime.now() in Python?
Python's datetime module is used to extract various components of the current date and time, such as minutes, seconds, and even milliseconds. The datetime.now() method defined in the datetime module returns the current local date and time as a datetime object. This object allows us to access its individual components like minute, second, and microsecond. Using Attributes of datetime.now() to Extract Values Here we use the datetime.now() method to get the current minutes, seconds, and milliseconds. The now() function is defined under the datetime module. We retrieve the current minutes, seconds, and milliseconds by using .minute, .second, ...
Read MoreHow to convert unix timestamp string to readable date in Python?
You can use the fromtimestamp() function from the datetime module to convert a UNIX timestamp string to a readable date format. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp. Converting Unix Timestamp to Readable Date Here's how to convert a Unix timestamp string to a readable date format ? import datetime # Convert timestamp string to readable date timestamp_str = "1500000000" timestamp = datetime.datetime.fromtimestamp(int(timestamp_str)) print(timestamp.strftime('%Y-%m-%d %H:%M:%S')) The output of the above code is ? 2017-07-14 08:10:00 Using Different Date Formats ...
Read MoreHow to subtract Python timedelta from date in Python?
The Python datetime module provides various ways for manipulating dates and times. One of its key features is the timedelta object, which represents duration and also the difference between two dates or times. Subtracting a specific amount of time from a date can be done using timedelta. For example, if we want to find the date a day before today, then we create a timedelta object with days=1 and then subtract it from the current date ? Subtracting One Day from Today's Date The basic use case is subtracting a single day from today's date. This can ...
Read MoreHow to convert Python date format to 10-digit date format for mysql?
While working with databases like MySQL, it's necessary to store dates in a numeric format, especially for timestamps. MySQL commonly uses Unix timestamps, which are 10-digit numbers representing the number of seconds since January 1, 1970 (known as the epoch). The following are the different methods from the time and datetime modules to convert Python date formats into a 10-digit format (Unix timestamp) suitable for use with MySQL. Using mktime() Method The mktime() method from the Python time module is the inverse function of the localtime(). This method converts a time.struct_time object or a tuple with 9 ...
Read MoreHow to convert date to datetime in Python?
In this article, we will discuss how to convert a date to a datetime object in Python. We use the combine() method from the Date & Time module to combine a date object and a time object into a single datetime object. While the date object represents only the calendar date (year, month, day), sometimes we need the full datetime object that includes time (hour, minute, second) as well. Following are several ways to achieve this ? Syntax The syntax of the combine() method is as follows ? datetime.combine(date, time) Using combine() with ...
Read MoreWhich one is more accurate in between time.clock() vs. time.time()?
Two commonly used functions from the Python time module are time.time() and time.clock(). Each function provides a different purpose and returns different values depending on the platform (Windows vs. Unix). In Python 3.8, time.clock() was removed, so time.perf_counter() or time.process_time() are generally preferred over the older time.clock() for specific CPU time measurements. The time.clock() was designed for measuring process CPU time, while time.time() measures wall-clock time. time.time() is more accurate for measuring overall elapsed time (the duration of time that has passed between two specific points in time). Measuring Elapsed Time with time.time() The time.time() function ...
Read MoreWhat are negated character classes that are used in Python regular expressions?
While working with Python regex, if we want to match everything except certain characters, then we can use negated character classes by placing a caret (^) as the first character inside square brackets. The pattern [^abdfgh] will match any character not in that set. What is a Negated Character Class? A character class like [abc] matches any single character that is 'a', 'b', or 'c'. But if we use a ^ symbol at the beginning, like [^abc], it will match any character except 'a', 'b', or 'c'. This allows us to exclude certain characters from the match quickly. ...
Read MoreWhat are metacharacters inside character classes used in Python regular expression?
Python's regular expressions provide various ways to search and manipulate strings. Metacharacters are special characters that carry specific meaning in regex patterns. However, their behavior changes significantly when used inside character classes (denoted by square brackets []). Understanding how metacharacters behave within character classes is crucial for writing accurate regular expressions. Most metacharacters lose their special meaning inside character classes, but some retain or alter their behavior. Understanding Character Classes Character classes are denoted by square brackets [] and define a set of characters that can match at a single position. For example, [aeiou] matches any single ...
Read MoreWhat are repeating character classes used in Python regular expression?
A repeating character class in Python regular expressions is a character class followed by quantifiers like ?, *, or +. These quantifiers control how many times the entire character class should match. Basic Repeating Character Classes When you use quantifiers with character classes, they repeat the entire class, not just the specific character that was matched ? import re # [0-9]+ matches one or more digits (any combination) text = "Order 579 and 333 items" pattern = r'[0-9]+' matches = re.findall(pattern, text) print("Matches:", matches) Matches: ['579', '333'] Common Quantifiers ...
Read More