There are many data types in Python. One of them is NoneType.
It is a special Python object that has no value. It is used to indicate the absence of a value, or to signify that no data is available.
The following are ways in which we can convert a NoneType to an integer or string.
Method 1: Using the Boolean OR operator
First of all, let’s see some examples:
1 2 3 4 |
var1 = None converted_var1 = 99 or var1 print(converted_var1) |
Output:
99
1 2 3 4 |
var2 = None converted_var2 = "Fort Ternan" or var2 print(converted_var2) |
Output:
Fort-Ternan
In the above example, the variables var1 and var2 hold NoneTypes. Using OR operator, we can convert these NoneTypes to an integer or a string of our choosing.
But why does this work? There are two points to consider to answer this question.
Point 1: Understanding how OR boolean operator works
The general syntax for the OR operator is
expression1 or expression2
where
expression1 | expression2 | expression1 or expression2 |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Point 2: Python considers some values as false
These values include: 0, None, False, [ ] (empty list), ( ) (empty type), { } (empty dictionary), set() (empty set), and range(0) (empty range).
When the rules in the Table 1 above are used with Point 2, it is clear that “None or 99” evaluates to 99 because None is understood as a false value and 99 as true. Here are more examples.
1 2 3 4 5 6 7 8 9 10 11 |
val1 = None or 7.89 # false or true yields true (7.89) print("val1: ", val1) val2 = None or [] # false or false yields false print("val2: ", val2) val3 = 456 or -8.3 # true or true yields true print("val3: ", val3) val4 = 2.5 or None #true or false yields true print("val4: ", val4) |
Output:
val1: 7.89 val2: [ ] val3: 456 val4: 2.5
Note: “x or y” evaluates to x if x is true; otherwise, y is returned. In other words, if x is true, it is returned irrespective of the value of y, but if x is false, y is returned irrespective of its value.
A more interesting example
Suppose we want to convert all the elements in the following list into integers: [2.36, -1.87, None, 9.6]. The first way could be to loop through the list converting each item as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
list1 = [2.36, -1.87, None, 9.6] # initialize an empty list list2 = [] for item in list1: # if the value is false, then we convert 99 into int, which is still 99 # this captures the None value in list1. result = int(item or 99) # append the result to list2 list2.append(result) print(list2) |
Output:
[2, -1, 99, 9]
Alternatively, we can use list comprehension as shown below.
1 2 3 |
list1 = [2.36, -1.87, None, 9.6] list2 =[int(i or 99) for i in list1] print(list2) |
Output:
[2, -1, 99, 9]
As shown in the output, the None value is converted to 99 because None or 99 evaluates to 99.
Method 2: Using the if-condition
In this method, we use the if-statement to check if the variable holds a None value and if that is the case we reassign an integer or a string as shown in the coming examples.
1 2 3 4 5 |
var3 = None if var3 is None: var3 = 99 print(var3) |
Output:
99
If var3 is None (which it is) we reassign the variable var3 to a new value of 99, otherwise we set it to the original value of var3. We can write the same code as one-liners as shown in the following examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var4 = None var4 = 99 if var4 is None else var4 print("var4: ", var4) var5 = 12.6 var5 = 0 if var5 is None else var5 print("var5: ", var5) var6 = None var6 = "Missing" if var6 is None else var6 print("var6: ", var6) var7 = 3.142 # cast it into an integer if it is not NoneType var7 = 0 if var7 is None else int(var7) print("var7: ", var7) |
Output:
var4: 99 var5: 12.6 var6: Missing var7: 3
Here is how we can use the if-statement to convert each of the elements of the following list into an integer: [2.36, -1.87, None, 9.6] using one-liners.
1 2 3 |
list1 = [2.36, -1.87, None, 9.6] converted_list1 = [99 if i is None else int(i) for i in list1] print("converted_list1: ", converted_list1) |
Output:
converted_list1: [2, -1, 99, 9]
In the code, we loop through list1, and at each iteration check if the element is None if it is set it to 99 and if not cast it into an integer using the int() function.
Conclusion
A NoneType can be converted into an integer or a string in two ways that we discussed in this article – using the boolean OR operator or by if-statement. In either of the cases, you can use the int() function to cast the result into an integer if needed.