How to write and raise an a very simple custom exception in Python

Having gained some experience with the Python computer programming language, I truly know how important it is for the Python geek to learn and practice the exception handling so they can handle errors better in their applications and projects.

From my experience with research on the Internet about the Python computer programming language, coders face a lot of struggle when it comes to building their custom exceptions, especially the complete beginners. With the main purpose of introducing the Python beginner to the base builtin exception class and preparing them to write their own customs exceptions, I decided to share this short tutorial.

First of all, one important thing everyone of the readers should know about before going any further, is that all the different exceptions in the Python computer programming language are generated from the one shown below. Continue reading “How to write and raise an a very simple custom exception in Python”

How to trace your Python program with pdb

There are many useful Python packages to the Python coder out there. One of them is pdb, a package which can help to trace the state of a program in the Python computer programming language with the main purpose of debugging the code of an application or a project.

The purpose of this very short tutorial is to give the beginner a simple introduction to the builtin python debugger, through an example.

Before going any further with the article, make sure to write the following Python function in a new file, exactly like shown below.
Continue reading “How to trace your Python program with pdb”

A simple example of the try/except Python statements

There is so much confusion being created on the Internet geek communities when it comes to the try/except statements in the Python computer programming language, especially among the complete beginners who have no idea at all about error handling practices.

Having struggled myself with the try/except statements during the beginning of my Python coding journey, I decided to share a simple example which can be a benefit to a lot of people who are interested in learning the Python computer programming language. The sample code being shared through this tutorial, has not practical usage in real world scenarios, but for sure it is a great source to use when stuck in a try/except piece of Python code. Continue reading “A simple example of the try/except Python statements”

How to remove an item with the del statement in Python

When the Python coders work in the interactive console, or when they write scripts to be executed later in time, most of the time they make use of the del statement with the main purpose of removing an object. Beginners in the coding journey, need clear instructions so they can truly understand what a specific builtin utility achieves by being used in the Python computer programming language.

During this very short tutorial, codetheory will instruct the reader on the Python statement called del. According to the official documentation, the Python del statement can be used to remove entire variables. The syntax for it is being shown below. Continue reading “How to remove an item with the del statement in Python”

How to install a Python package with pip

Being an open source computer programming language, Python offers the opportunity to everyone out there to contribute their source code to the community, so people can benefit from the software packages of each other.

Based on my personal experience with the Python computer programming language so far, there are many packages out there which one can install to develop their software. And as far as my knowledge goes about the Python computer technology, there are many different ways to install a package in it.

A very practical way which can help the Python coder install packages on their machine, widely used by professional developers, is through pip. Continue reading “How to install a Python package with pip”

How to execute a Python script, for complete beginners

When one starts their journey with the Python computer programming language, there are many questions they ask themselves, most of which require an experienced geek to answer them. Frustration is a normal thing to experience during the journey as a coder, especially in the beginning, when one has no idea at all what they’re doing.

Being a Python geek myself, I must admit that the beginning of the journey with the Python computer programming language was not easy, especially when it came to practicing stuff in the interactive console.

During this short tutorial, I will teach the complete beginner how they can execute a Python script in their machine, without any kind of frustration or pain.

First of all, let’s define what is a script in the Python computer programming language. According to my knowledge on the Python computer programming technology, any file which ends in the .py extension, is considered to be a script.

For example, the following file is considered to be a script in terms of the Python computer programming language.

test.py

As far as my knowledge on the Python computer technology goes, a script acts as package for a group of statements with the main purpose of executing them together. Based on my experience with the Python technology, scripts can range from simple arithmetic operations, to complex stuff such as custom functions and classes.

For one to execute a script in Python computer programming language, they need to make sure they can access the Python interpreter from the console first.

Then, one can easily execute a Python script like shown below.

python test.py

From the above example, one can easily understand that python is the interpreter, and test.py is the script which is going to get executed.

Final thoughts

Depending on what the script contains, the results will be displayed on the console offered by the operating system which one is using while they’re executing and running the Python script.

How to join strings separated by a dot in Python

When one codes computer tools and software in their favorite computer programming language, they deal with a lot of frustration, especially in the beginning when they don’t know what they’re really doing. Based on my personal experience with the Python computer programming language, there are many different ways to accomplish a certain task with Python, but only a few of them are accepted as legit and the norm by those who get paid to write professional code.

Not that there is completely wrong to accomplish your tasks in Python computer programming language by going your own way, but it is always a good idea to stay updated with the latest tips from the professional software developers as they get paid to solve coding problems, and they know for sure what they do.

Right now, I am writing a project in Python computer programming language and there is a problem which has occurred to me. From my personal experience with the Python computer programming language, I believe I do know two ways to solve my problem from a coding perspective.

So what is the problem that has occurred to me and that needs to be solved? I need to join two strings separated by a dot like shown below.

test # first string
mp4 # second string
test.mp4 # final string

According to a book which I am studying, related to the Python computer programming language, one can easily perform string concatenation by using the addition arithmetic operation like shown in the following example.

‘test’ + ‘.’ + ‘mp4’

Although the above technique works when it comes to joining strings together in Python computer programming language, the professional software developers recommend another method to accomplish the same task. According to the official Python documentation, string objects support a specific method called join, which takes a list of strings and joins them together.

One can easily use the string specific method called join like shown below.

‘,’.join([‘a’, ‘b’])

When the above piece of Python code is been executed in the interactive console, the following output is being displayed.

‘a,b’

Based on the above technique, one can easily concatenate two strings together separated by a dot like shown in the following example. The syntax is very clear and easy for anyone to understand.

‘.’.join([‘test’, ‘mp4’])

Once the above Python operation is executed on my interactive console, the following comes up.

‘test.mp4’

As you can see from the above output, one can make use of the specific string method called join, to concatenate together a list of things.

Final thoughts

As I mentioned earlier in this article, one can accomplish a certain task in many different ways when it comes to the Python computer programming language, but there are only a few ways accepted and used by the professionals. The .join method is perfect when one needs to join strings together, especially separated by a dot.