In Python, there are 2 ways to make a list of alphabet characters.
Using string Module
The string.ascii_lowercase is a pre-initialized string used as string constant. In Python, string ascii_lowercase will give the lowercase letters ‘abcdefghijklmno...
In Golang, using the Unmarshal JSON function is the easiest way to Marshal / Unmarshal json with a custom type attribute.
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Using UnmarshalJSON Function
Example on custom Unm...
In Python, using the timedelta object is the easiest way to calculate number of days between two given dates.
Using timedelta
If you have two date objects, you can just subtract them, which computes a timedelta object.
In this program, we will cal...
In Python, there are 2 ways to retrieve a module's path.
Using __file__ Attribute
You can retrieve a module's path using the __file__ attribute. For example,
#!/usr/bin/python3
# Import module
import os
print(os.__file__)
/usr/local/lib/pyt...
In Python, there are 2 ways to get an absolute file path.
Using abspath Function
The os.path.abspath() method returns a normalized absolutized version of the pathname path. For example,
#!/usr/bin/python3
# Import module
import os
abs = os.pat...
In Python, using the str.join function is the easiest way to concatenate a list of strings into a single string
Using str.join Function
The str.join() method returns a string which is the concatenation of the strings in iterable. A TypeError will be...
In Python, there are 2 ways to get a function name as a string.
Using __name__ Attribute
Using __qualname__ is the preferred method as it applies uniformly. It works on built-in functions as well:
#!/usr/bin/python3
def my_func():
pass
class...
In Python, using the uuid module is the easiest way to create a GUID/UUID.
Using uuid Module
uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUID...
In Python, using the timedelta object is the easiest way to subtract a day from a date.
Using timedelta Object
A timedelta object represents a duration, the difference between two dates or times.
See the following example:
#!/usr/bin/python3
#...
In Python, there are 3 ways to return dictionary keys as a list.
Using keys Function
The dict.keys() function returns a view object that displays a list of all the keys in the dictionary in order of insertion.
See the following example:
#!/usr/...
In Python, there are 3 ways to get file creation and modification dates times.
Using os.path Function
The os.path.getmtime() function returns the time of last modification of path.
The os.path.getctime() function returns the system’s ctime which, o...
In Python, there are 3 ways to check if a variable exists.
Using locals Function
The locals() function returns the dictionary of the current local symbol table.
#!/usr/bin/python3
vName = 1
if 'vName' in locals():
print("exists")
exists
...
In Python, there are 2 ways to determine if an object is iterable.
Using Duck Typing
Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing...
In Python, there are 3 ways to remove duplicates in list.
Using Built-in set Function
The built-in set() function returns a new set object, optionally with elements taken from iterable. If you later need a real list again, you can similarly pass the...
In Python, there are 3 ways to reverse a string.
Using slicing
Slice notation takes the form [start:stop:step].
#!/usr/bin/python3
s = "Hello World"
print(s[::-1])
# syntax
# a[start:stop] # items start through stop-1
# a[start:] # items...