- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34488 Articles for Programming
3K+ Views
You can use the fromtimestamp() function from the datetime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp. Exmaple import datetime timestamp = datetime.datetime.fromtimestamp(1500000000) print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))OutputThis will give the output −2017-07-14 08:10:00
1K+ Views
In this article, we will convert a python date format to 10-digit format for MySQL. We use the mktime() method from the time module provided by python. The mktime() method The python time method mktime() is the inverse function of the localtime(). The time.mktime() method of the Time module is used to convert a time.struct_time object or a tuple of 9 elements(which represents the time.struct_time object) to time in seconds since the epoch of the local system. Syntax The syntax of mktime() method is as follows. Time.mktime(t) Where, t is a time.struct_time object or a tuple containing 9 elements ... Read More
38K+ Views
In this article, we will discuss how to convert a date to a datetime object in Python. We use the combine method from the datetime module to combine a date and time object to create a datetime object. Syntax The syntax of combine() method is as follows. datetime.combine(date, time) Where, date is a date object. time is a time object. This method returns a datetime object which is the combination of the above two objects (date, time). Converting a date object to datetime object If we have a date object and don't have a time object, then ... Read More
325 Views
You can suspend the calling thread for a given time in Python using the sleep method from the time module. It accepts the number of seconds for which you want to put the calling thread on suspension for. Exampleimport time while(True): print("Prints every 10 seconds") time.sleep(10)OutputPrints every 10 seconds Prints every 10 seconds Prints every 10 seconds ...Each of the above statements will be printed every 10 seconds.Note that this method also supports floating point values so you can also put the calling thread to sleep for fractions of seconds.
315 Views
Use time.clock() for timing/benchmarking if you need to choose between time and clock in Python.time() returns the the seconds since the epoch, in UTC, as a floating point number on all platforms. On Unix time.clock() measures the amount of CPU time that has been used by the current process, so it's no good for measuring elapsed time from some point in the past. On Windows it will measure wall-clock seconds elapsed since the first call to the function.Changing the system time affects time.time() but not time.clock().If you're timing the execution of a block of code for benchmarking/profiling purposes, you should ... Read More
4K+ Views
In this article, we retrieve the current CPU time in Python. We use the time() method which is imported from the python time module. 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. It returns a floating-point number expressed in seconds. Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in ... Read More
899 Views
We come across negated character classes in Python regular expressions. An regex of ‘[abdfgh]’ matches any single character which is one of ‘a’, ‘b’, ‘d’, ’f’, ’g’ or ‘h’. This is termed a character class.An regex of ‘[^abdfgh]’ will match any single character which is NOT one of ‘a’, ‘b’, ‘d’, ’f’, ’g’ or ‘h’. This is a negated character class, and is indicated by the ‘^’ character at the start of the character class.The character ‘^’ has a special meaning at the start of the character class. if it is used elsewhere in that character class it simply means a ... Read More
315 Views
Most letters and characters simply match themselves. However, there are some characters called metacharacters, that don’t match themselves. Instead, they indicate that some pattern should be matched, or they repeat or change parts of the regular expression. Here’s a complete list of the metacharacters. ^ $ * + ? { } [ ] \ | ( )At first we’ll look at [ and ]. They’re used for indicating a character class, which is a set of characters that you want to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating ... Read More
439 Views
A character class followed by operators like '?', '*' or '+' are called repeating character classes.If you repeat a character class by using the '?', '*' or '+' operators, you will repeat the entire character class, and not just the character that it matched. The regex '[0-9]+' can match '579' as well as '333'. If you want to repeat the matched character, rather than the class, you will need to use backreferences. '([0- 9])\1+' will match '333' but not “579”. When applied to the string “922226”, it will match '2222' in the middle of this string. If you do not ... Read More
1K+ Views
Character class operations are a way to match certain types of characters in a string using regular expressions in Python. In regular expressions, a "character class" is a set of characters that you want to match. You can use square brackets `[]` to create a character class. For example, if you want to match any vowel, you can use the character class `[aeiou]`. This will match any single character that is either 'a', 'e', 'i', 'o', or 'u'. Example This code will search for all the vowels in the `text` string using the regular expression `[aeiou]`, and it will return ... Read More