All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Make a List of Alphabet Characters in Python

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...
Patcher56

How to Marshal / Unmarshal Json with a Custom Type Attribute in Go

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...
aweis

How to Calculate Number of Days Between Two Given Dates in Python

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...
Unused

How to Retrieve a Module's Path in Python

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...
aweis

How to Get an Absolute File Path in Python

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...
aweis

How to Concatenate a List of Strings into a Single String in Python

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...
aweis

How to Get a Function Name as a String in Python

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...
ada

How to Create a GUID/UUID in Python

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...
aweis

How to Subtract a Day from a Date in Python

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 #...
Sambhav Khandelwal

How to Return Dictionary Keys as a List in Python

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/...
Sambhav Khandelwal

How to Get File Creation and Modification Dates Times in Python

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...
Patcher56

How to check if a variable exists in Python

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 ...
Sambhav Khandelwal

How to Determine if an Object is Iterable in Python

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...
Unused

How to Remove Duplicates in List in Python

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...
pooriabt

How to Reverse a String in Python

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...
pooriabt