The tail command is a very useful Linux command when working with files. Do you want to know how to use it at its best? Keep reading :)
The Linux tail command allows to see the last lines of a text file in Linux. A scenario in which this can be extremely useful is when working with application logs. The tail command can also be used together with the pipe to filter the output of another command.
The basic usage of the tail command is pretty straightforward but in this tutorial we will look at practical use cases that make…
You might have seen a Python recursion error when running your Python code. Why does this happen? Is there a way to fix this error?
A Python RecursionError exception is raised when the execution of your program exceeds the recursion limit of the Python interpreter. Two ways to address this exception are increasing the Python recursion limit or refactoring your code using iteration instead of recursion.
Let’s go through some examples so you can understand how this works.
The recursion begins!
Let’s create a program to calculate the factorial of a number following the formula below:
n! = n *…
The next() function is useful when working with iterators and it’s a must-know for Python developers.
The Python next() function takes as first argument an iterator and as an optional argument a default value. Every time next() is called it returns the next item in the iterator until no items are left. At that point the next function returns a default value (if passed to it) or a StopIterarion exception is raised.
In this tutorial you will learn in what circumstances you can use the next() function as part of your Python programs.
Let’s get started!
The Python next function…
Knowing how to write a unit test in Python is critical for developers. Just writing your application code is not enough, tests are a must.
Unit tests allow to test self-contained units of your code independently from each other. Python provides the unittest framework that helps write unit tests following a pre-defined format. To test your code with the unittest framework you create test classes and test methods within each test class.
In this tutorial we will write unit tests for a simple class that represents a user in a video game.
Let’s get started!
The following class represents a…
In this tutorial we will create a stack in Python step-by-step. The stack is a LIFO (Last-in First-out) data structure.
To create a stack in Python you can use a class with a single attribute of type list. The elements of the stack are stored in the list using the push method and are retrieved using the pop method. Additional methods allow to get the size of the stack and the value of the element at the top of the stack.
We will build a custom class that implements the common operations provided by a stack data structure.
Knowing how to check if a Python string contains a number can be something you will have to do at some point in your application.
A simple approach to check if a Python string contains a number is to verify every character in the string using the string isdigit() method. Once that’s done we get a list of booleans and if any of its elements is True that means the string contains at least one number.
There are multiple ways to solve this problem and this tutorial goes through few of them.
Let’s get started!
Knowing how to write the elements of a list to a file in Python can be handy. In this tutorial you will see how to do that in multiple ways.
A common approach to write the elements of a list to a file using Python is to first loop through the elements of the list using a for loop. Then use a file object to write every element of the list to a file as part of each loop iteration. The file object needs to be opened in write mode.
There are many scenarios in which writing the items of…
The Python json module allows to use data in JSON format in your applications. Do you want to convert a Python object into JSON or viceversa? This is the right module to do that.
The Python json module provides functions to convert a Python object into JSON (dump() and dumps()), this conversion is called serialization or encoding. The json module also provides functions to convert JSON data into a Python object (load() and loads()), this is called deserialization or decoding.
The Python yield keyword is something that at some point you will encounter as developer. What is yield? How can you use it in your programs?
The yield keyword is used to return a value to the caller of a Python function without losing the state of the function. When the function is called again its execution continues from the line after the yield expression. A function that uses the yield keyword is called generator function.
This definition might not be enough to understand yield.
That’s why we will look at some examples of how to the yield keyword in…
If you want to serialize and deserialize Python objects you might have considered using the Python Pickle module.
The Python Pickle module allows to serialize and deserialize a Python object structure. Pickle provides two functions to write/read to/from file objects (dump() and load()). It also provides two functions to write/read to/from bytes objects.
We will go through few examples to show how pickle works both with file objects and bytes objects. We will also test it with multiple data types.
It’s time to pickle!
The Python Pickle module is used to perform serialization and deserialization of Python objects.
I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!