Month: September 2020

Hello World! A Python One-Liner to Get Started with Python Quickly

The “hello world program” is used in programming languages to set up a minimal programming environment and execute the first trivial program in it. It helps you get your environment going and take your first steps towards a more complicated program. This short tutorial will show you the fastest possible way to write your first …

Hello World! A Python One-Liner to Get Started with Python Quickly Read More »

Python Control Flow Statements

Among the ingredients that make a programming language powerful are control flow statements. The Python for loop is one such control flow statement. The if statement is another one. In this tutorial, you’ll learn about both! Python For Loop The world around us is built around repetition. The sun goes up every morning and after …

Python Control Flow Statements Read More »

How To Resolve UnboundLocalError On Local Variable When Reassigned After The First Use?

Summary: To resolve an UnboundLocalError when the local variable is reassigned after the first use, you can either use the global keyword or the nonlocal keyword. The global keyword allows you to modify the values of a global variable from within a function’s local scope while the nonlocal keyword provides similar functionality in case of …

How To Resolve UnboundLocalError On Local Variable When Reassigned After The First Use? Read More »

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 …

How To Import A Module Given The Full Path Read More »

Python Semicolons: How They Work and Why Haters Tell You to Avoid Them

Every Python coder hates semicolons ; to terminate statements. If you’re like me and somebody shows you the following Python code, you’ll start to wonder if the person confused Python with Java or C++. Python Semicolon Example [One-Liner] Here’s how one may use the semicolon: If you run this Python one-liner with semicolons, you’ll get …

Python Semicolons: How They Work and Why Haters Tell You to Avoid Them Read More »

How to Access an Object Attribute Given the Attribute Name as a String?

You may know the following problem: You have an object’s attribute name as a string—say, you’ve read it from a file—and you want to access the attribute with the given name. But you cannot use the syntax object.”attribute” because the dot notation only allows for name-access like this: object.attribute. How do you resolve this problem? …

How to Access an Object Attribute Given the Attribute Name as a String? 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: subprocess.call() function subprocess.run() function subprocess.Popen Class os.system() function os.popen() function 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 …

How To Call An External Command In Python? Read More »