Python One Line FizzBuzz
The FizzBuzz problem is a common exercise posed in code interviews to test your proficiency in writing simple Python code. Problem: Print all numbers from 1-100 to the shell with three exceptions: For each number divisible by three you print “Fizz”, For each number divisible by five you print “Buzz”, and For each number divisible … 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 … Read more
Python One Line Function Definition
A lambda function allows you to define a function in a single line. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y: x+y calculates the sum of the two argument values x+y in one line … 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 … 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? … 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 Can I Read Inputs as Numbers in Python?
Are you ready to take your Python knowledge to the next level? Letβs have a little fun with the inputs. Only instead of having you enter your name, weβll work with numbers. Itβs a nice, simple calculator. You enter in two numbers and it adds them together to give you an answer. Simple, right? Well, … Read more
6 Easy Steps to Get Started with Python
When Guido van Rossum released the first viable Python version 0.9.0 in 1991, he couldnβt have expected (by a long shot) that he was on the verge of creating the most influential programming language in the world. Python has a bright future: every new Python version adds new features to the programming language. In this … 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
How to Sort and Return a Python List in One Line?
To sort and return a Python list in a single line of code, use the sorted(list) method that returns a new list of sorted elements. It copies only the references to the original elements so the returned list is not a deep but a shallow copy. Let’s dive into the challenge to learn about more … Read more
Python One Line Sum List
Article summary: Here’s a quick visual overview of the contents of this tutorial. Flat List: To sum over a list of numbers in a single line of Python code, use Python’s built-in function sum(list). Nested List: To sum over a list of lists in one line Python, use a generator expression to flatten the list … Read more