Search

From Carbon Dioxide To Liquid Fuel With New, Cheaper, Efficient, And Innovative Technique

Human activity has been impacting the environment in negative ways. The greenhouse effect which produces climate change due to the trapping of the sunlight energy in the atmosphere is caused by having extra carbon dioxide in the atmosphere which is not removed by the photosynthetic processes of green plants. These extra carbon dioxide causes global warming. Climate change mitigation, or actions to reduce the magnitude and rate of global warming, along with climate change mitigation strategies, are now very popular. Several approaches have been proposed to remove these extra carbon dioxide or what is called carbon sequestration. Today, we will focus on a new innovation that not only promises to help the environment, but is commercially viable.

Scientists at the U.S Department of Energy’s (D.O.E) Argonne National Laboratory in collaboration with Northern Illinois University have undertaken research that have realized a way to not only remove carbon dioxide from the environment, but to also break it down and use it to manufacture ethanol.

Carbon dioxide to fuel
 

The discovery involves using catalysts, specifically electrocatalysts under low voltage. A catalyst is a substance that increases the rate of a chemical reaction without undergoing in the reaction itself and electrocatalysts are types of catalysts that function at electrode surfaces or they may be the electrode themselves. The electrocatalyst that was used by the researchers was copper, or atomically dispersed copper on carbon-powdered supports. These copper was used to break down trapped carbon-dioxide and water molecules and then these molecules were selectively reassembled into ethanol under an external electric field. When the efficiency was measured, it was found that the electrocatalytic selectivity process was 90 percent efficient, much better than existing techniques for converting carbon dioxide to ethanol. Furthermore, over extended periods of time it was found to operate under stable conditions at low voltages. The researchers also say that the costs for the process is also reasonable.

So one may ask: why convert carbon dioxide and water to ethanol? This is because ethanol is widely used in the U.S. It is used to produce gasoline and it is the chemical for many personal care and household products. Also, industries need ethanol to manufacture a host of products that provide lots of benefits to other industries and humans.

This is not the first time though that carbon dioxide will be converted into ethanol. But this method is more efficient and more cost-effective. Furthermore, the researchers say it is more stable than other previous methods. According to Tao Xu, a professor in physical chemistry and nanotechnology from Northern Illinois University, this process would open the doors to technology that would convert carbon dioxide electrocatalytically not only to ethanol, but to a vast array of other industrial chemicals.

So what are the benefits of removing carbon dioxide from the environment? The benefits are immense. Reusing carbon dioxide to manufacture ethanol would provide raw materials for industries making use of this ethanol at a cheaper cost. It reduces the increase in global temperatures. Presently, the world is working to make sure global temperatures do not exceed the two degrees Celsius mark. This approach will contribute its share. Also, greenhouse gases are being removed from the environment, helping to slow down climate change. Greta Thunberg, the Swedish teenage climate activist, would be happy to promote this technique. Also, since this approach has an efficient and reasonable cost, it will confer a lot of benefit to fossil fuel industries and alcohol fermentation plants who emit a lot of carbon dioxide annually into the atmosphere. They could derive some revenue by converting that carbon dioxide into ethanol. Furthermore, lots of jobs and career options would be created in the process in the U.S and around the world if this efficient technique is implemented.

The research has become successful that the researchers are in collaborative talks with industries to start producing ethanol. According to Di-Jia Liu, a senior chemist at Argonne’s Chemical Science and Engineering division, and one of the authors, they have plans to collaborate with industries in order to advance the promising technology. There are also plans to produce several other catalysts.

Material for this post was taken from Argonne National Laboratory press release.

From Zero To Hero In Handling Python Exceptions

No code is perfect. No matter how expert you are at programming, once in a while an error will occur. For beginners, this is more common. One area of errors that beginners encounter are syntax errors. These are errors that occur due to the fact that the programmer did not follow one of the rules of the language. In whatever IDE you are working with, syntax errors are usually highlighted so that the programmer can learn from them. We will not be talking about syntax errors in this post. Rather our focus will be on another type of error that occurs in programming, and that is exceptions.

python exceptions
 

Exceptions are errors that are detected during the execution of the program. They are different from syntax errors. They disrupt the normal operation of the program and tends to make the program produce results that are, though not fatal, unintended by the programmer. In python, there are standard ways of handling exceptions and we will discuss on those ways.

All exceptions in python have a type, and that type derives from a parent class, the BaseException class. If you go to the python documentation page on exceptions, you will realize that there are so many documented exceptions in python and ways to handle them. You can take a look at the various ones. But we will be discussing on only a representative few.

As we have said before, when a code contains lines that would cause unusual execution in the program, such as asking for a division by zero, an exception will be raised. When an exception is raised if it is not handled, python will ask the program to stop execution. Therefore, it is in our best interest to catch and handle exceptions if we want our programs to run to the end.

Let’s look at some examples of common exceptions you must have already encountered in your programs: ZeroDivisionError and IndexError.

A ZeroDivisionError is raised when the denominator for a division or modulus operation is zero. Because you cannot divide by zero, this is an error in the code that must have to be handled, otherwise python will stop execution of the program. If it is not handled, python will stop execution and print out a stack traceback report, indicating where the exception happened and the type of the exception. Let’s take an example to illustrate this. Just run the code below.

The code has a list of denominators we want to use to divide the numerator, 20. It runs well until the denominator becomes 0 and we run into an exception. Because the exception is not handled, python stops execution of the program and prints out a stack traceback of the exception so the programmer can understand where it occurred.

Now, let’s talk about the IndexError exception. IndexError exceptions are raised when a sequence subscript is out of range. Remember that the index for a sequence starts from zero to length of the sequence minus 1, so if we call for an index that is out of that range, an IndexError exception is raised. Here is an example.

When we called for the fourth fruit, it raised an error which was not handled and the program had to stop execution. Notice the message in the stack traceback. It gives the type of exception and what caused the exception to happen.

How to handle exceptions in python.

When you notice that a line of code might result in an error, you can handle it using that try…except…else…finally blocks. I will describe each of them in turn. But here is how they would be arranged in code.


try:
    # place relevant code that you expect
    # will result in an error here
except ExceptionErrorToCatch:
    # place code to handle the exception here
else:
    # place code for what to do after 
    # the code runs successfully at 
    # try block
finally:
    # place clean up code here 

The try block:

The try block is where you place the code that can raise an exception. More than one type of exception can occur at the try block but they can all be handled at the except block area.

The except block:

At the except block is where you place code that will handle the exception After the except statement you state the exception error that you want to handle or catch. It could be a ZeroDivisionError, IndexError, AttributeError etc. You just state it here. You could decide to handle all the exceptions separately and so this means you can write more than one except statement, but if you decide to use the same code to handle all the exceptions coming from the try statement, then you will denote the exceptions you are catching one after the other in a single except statement and separate them using a comma. For example, if your code will result in a ValueError or ZeroDivisonError, you could handle them in the following ways:


try:
    m = int(input()) #expect ValueError here
    n = 20/m         #Expect ZeroDivisionError here
except (ZeroDivisionError, ValueError):
    print('Exception handled here')    

You can see the both exceptions I was expecting were placed in a tuple. You use code like this to handle more than one exception at the same time. But if you want to handle each exception separately, you could use the code below:


try:
    m = int(input()) #expect ValueError here
    n = 20/m         #Expect ZeroDivisionError here
except ZeroDivisionError:
    print('ZeroDivisionError exception handled here')
except ValueError:
    print('ValueError exception handled here')        

Some people use a generic Exception clause when handling exceptions and I would encourage you not to do so. When handling exceptions be specific as to the type of exception you want to handle.

Note: when the statements in the try block are run, if no exception occurs, then the except block is not triggered, instead it is skipped. And that is when you come to the else block.

The else block:

In case you want to run some code if no exception was raised in the try blocks or statement, then you would use an else block to write the code. The else block continues execution when no exception was realized in the try statement. But if an exception is realized, python uses the except blocks to try to handle the exception. Here is some code that illustrates the use of the try…except…else statements. It will ask you to input a number of your choice when you run it.

Try running the above code with the following scenarios to see how the try…except…else statements with work. When the program asks for your input, first 1. Insert a zero to see how the exception will be handled. 2. Second, insert something that is not a number, like a string, ‘mmj’, to see how it handles the exception where something that is not a number is entered. 3. Enter a valid number to see what is printed out. You will notice that the code in the else block is executed when a valid number is entered.

Finally, there is a fourth block we have not discussed, the finally block.

The finally block.

The finally block is an optional block. It is used to clean up resources after the try block and other blocks have been run. No matter what happens in the code, whether an exception is raised or not, the finally block is called last before the program leaves the exception handling area. Note that if an exception occurs during the try block and it is not handled, python will re-raise the exception again after it has run the finally block. Exceptions can also sometimes occur during execution of the except clause, it will be re-raised again after the execution of the statements in the finally block.

Let’s demonstrate an example of how finally block can be used.

Try to run the scenarios again that I gave in the else paragraph to see how the finally block will be run. It is executed every time, whether an exception is raised or not. Whenever you are using up resources, or opening and writing to files, you use the finally block to do clean up of those resources.

The Arguments of an Exception:

Sometimes, we might want to retrieve the additional information that comes with the exception. That is when we supply the arguments variable when specifying the exception(s) in the except block. You state the arguments variable after specifying the name of the exception. In the code below, I used the keyword, as, to separate the exception name from the argument, I then referenced the argument in the exception handling code.

Raising an Exception

What if, as a programmer, you want to raise an exception when a condition is breached. For example, instead of waiting for python to detect the exception in the division by zero case above, you want to check if the user inputted zero yourself. You can raise an exception to handle that error yourself. You raise an exception by calling the raise statement like this: raise ExceptionName(argument). The argument could be optional.

Here is some example:

In line 3, I checked on the condition that the user entered 0 and if that is the case, I raised an exception which was handled by the except block in lines 5 and 6. Notice that I supplied an argument to the exception when I raised it and that argument was retrieved when the exception was handled for informative debugging. Try running the code above using two scenarios: 1. Enter 0 on input to see how it will run. 2. Enter a number on input to see how it skips the conditional in the try statement without raising an exception and goes to the else block. Please, don’t enter a non-number; I'm not handling that exception.

So, you have all you need to work with exceptions. I wish you get creative and start experimenting with them. In case you want further information on exceptions and how to handle them, you can reference the python documentation page on exceptions here.

5 Interesting Ways To Remove Items From A List.

In python, a list is an ordered sequence of elements that is mutable, or changeable. Each element in a list is called an item. Although the types of items in a list can vary, most times you will find that the items in a list are of the same type. You denote a list by having the items enclosed between []. The items in a list could be mutable or immutable data types like strings, integers, floats, or even lists themselves in a nested list structure.

python list
 

Lists have several functions that could be used to operate on the items like adding items to a list, reversing the items in a list, and sorting the items in a list according to defined keys or order. In this post, we will be discussing five ways you can use to remove an item or items from a list.

The different ways are divided into two groups: removing a single item from the list and when you want to remove more than one item in a single operation.

Removing a single item.

In this section I will mention two ways you can use just to remove a single item. They are designed for just that.

list.remove(x):

The remove function is a function that comes with the list class in python. It is used to remove a single item, x, from the list and it does not return anything. It removes the first item, x, from the list even if there are more than one occurrence of the item in the list.

Now for some code to demonstrate it:

You can see that I asked the remove function to remove ‘mango’ from the list of fruits and it removed the first occurrence of the fruit, mango, leaving behind the second. If there is no item like mango in the list, remove function would have returned an error.

list.pop([i]):

This function gives you the ability to remove an item by index. Although you can remove only one item at a time, using the pop built-in function, you do not remove by value but by index in the list. And remember, the indices of lists in python starts from 0. In the syntax above, the item at index i is being removed from the list. If you do not specify an index to pop, it then removes the last item in the list thereby acting as a stack, i.e last in first out behavior. The function also returns the item you popped, giving you the added ability to reuse that item that was popped from the list.

Here is some code on the fruits list to demonstrate that.

You could experiment with removing items from a list you created yourself using any of the two functions above.

Now we will go on to methods that gives you the ability to remove more than one item in a single operation.

Ways to remove more than one item from a list.

These methods can remove a single item or more than one item in a single operation. I have catalogued three ways that this can be done, using both functions and programming logic.

del object_name[index]:

The del operator can be used to remove any object in python. It can act on lists and other types in python. Unlike the earlier two functions, the del operator can be used to remove a single item from a list or a slice of items provided they exist consecutively. It does not return anything. When using del on a list, you must specify the list index of the item you want to remove or the slice you want to remove.

Here is some code illustrating the operation of the del operator.

Notice that when I completely deleted the fruits list and tried to reference it again, it gave a NameError. Fruits list does not exist any longer after applying del operator to the object. So, you can see that the del operator is a powerful tool. Use it at your discretion.

Using slicing logic to delete items:

We can use the concept of the slice operator, just as we used for the del operator above, to remove specific items from a list. I find this cumbersome sometimes but I need to add it here. A slice can be denoted as list_name[start:stop:step]. The stop and step are optional but if you don’t specify step, step defaults to 1, and if you don’t specify stop and step, what it gives you is a value, the item, and not a list, but we need to have a list back in this operation, so we will be specifying both the start and stop.

Here is some code.

Now, let’s move on to the last method for removing more than one item from a list. It involves using programming logic and list comprehension.

Programming logic method:

In this method, which I use often, you just move through the list and filter out the item you don’t need. That's all. I use list comprehension to do it, but you can also use for loop if you don’t understand list comprehension.

For those of you who don’t understand list comprehension, here is the same logic using a for loop. But list comprehension is more preferred because it looks more beautiful.

The for loop took four lines while list comprehension took only one line. Grrrh!

That’s all folks. I hope I have given you ideas on how you can remove items from your lists.

Happy pythoning.

Matched content