How To Import A Module Given The Full Path

Summary: To import a module given the full path, the following methods can be used : Using the importlib module Using SourceFileLoader class Using sys.path.append() function Adding __init__ file to the folder containing the module and then importing it Problem: How to import a module if its full path has been given? Example: Consider we … Read more

How To Call An External Command In Python?

Summary: To call an external command in a Python script, use any of the following methods: Whether you are a developer or a system administrator, automation scripts are going to be a common part of your daily routine. These may include automating routine tasks like file backups or health checks. However, maintaining such shell scripts … Read more

How To Remove Items From A List While Iterating?

Summary: To remove items from a list while iterating, use any of the following methods. List comprehension, Reverse iteration with the remove() method, Lambda Function with the filter() method, or While loop with the copy(), pop() and append() functions. Let’s start with defining the exact problem you want to solve. Problem: Given a list. How … Read more

Python One Line Conditional Assignment

Problem: How to perform one-line if conditional assignments in Python? Example: Say, you start with the following code. You want to set the value of x to 42 if boo is True, and do nothing otherwise. Let’s dive into the different ways to accomplish this in Python. We start with an overview: Exercise: Run the … Read more

How To Ask Users For Input Until They Provide a Valid Input?

Summary: To accept valid inputs from the user either use a While Loop With Custom Validations or use the PyInputPlus module to avoid tedious validation definitions. You can also use recursion to prompt the user for entering the valid input. Overview Problem: Given a user input; accept the input only if it is valid otherwise … Read more

Python One Line Append

Do you want to one-linerize the append() method in Python? I feel you—writing short and concise one-liners can be an addiction! πŸ™‚ This article will teach you all the ways to append one or more elements to a list in a single line of Python code! Python List Append Let’s quickly recap the append method … Read more