Categories
Python Answers

How to catch multiple exceptions in one line with Python?

Sometimes, we want to catch multiple exceptions in one line with Python.

In this article, we’ll look at how to catch multiple exceptions in one line with Python.

How to catch multiple exceptions in one line with Python?

To catch multiple exceptions in one line with Python, we can separate the exceptions we want to catch with commas.

For instance, we write:

try:
    raise ValueError('Represents a hidden bug, do not catch this')
    raise TypeError('This is the exception you expect to handle')
except (ValueError, TypeError) as e:
    print(repr(e))

We catch both ValueError and TypeError with one except clause.

And we get the exception content with e.

Conclusion

To catch multiple exceptions in one line with Python, we can separate the exceptions we want to catch with commas.

Categories
Python Answers

How to find and replace elements in a list with Python?

Sometimes, we want to find and replace elements in a list with Python.

In this article, we’ll look at how to find and replace elements in a list with Python.

How to find and replace elements in a list with Python?

To find and replace elements in a list with Python, we can use list comprehension.

For instance, we write:

a = [1, 2, 3, 1, 3, 2, 1, 1]
b = [100 if x == 1 else x for x in a]
print(b)

We have list a and we want to replace all the 1’s with 100.

To do this, we write [100 if x == 1 else x for x in a].

We check if x is 1 where x is each entry in a.

We put 100 in the returned array if x is 1. Otherwise, we put x in the array.

Then we assign the returned array to b.

Therefore b is [100, 2, 3, 100, 3, 2, 100, 100].

Conclusion

To find and replace elements in a list with Python, we can use list comprehension.

Categories
Python Answers

How to properly ignore exceptions in Python?

Sometimes, we want to properly ignore exceptions in Python.

In this article, we’ll look at how to properly ignore exceptions in Python.

How to properly ignore exceptions in Python?

To properly ignore exceptions in Python, we can use the pass keyword in the except clause.

For instance, we write:

try:
    raise Exception()
except:
    pass

to create an empty except clause with pass.

Conclusion

To properly ignore exceptions in Python, we can use the pass keyword in the except clause.

Categories
Python Answers

How to remove an element from a list by index with Python?

Sometimes, we want to remove an element from a list by index with Python.

In this article, we’ll look at how to remove an element from a list by index with Python.

How to remove an element from a list by index with Python?

To remove an element from a list by index with Python, we can use the del operator.

For instance, we write:

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del a[-1]
print(a)

We have the list a and we want to remove the last item.

To do that, we write del a[-1] to get the last item with index -1 and use the del operator to remove it.

Therefore, a is now [0, 1, 2, 3, 4, 5, 6, 7, 8] according to the print output.

Conclusion

To remove an element from a list by index with Python, we can use the del operator.

Categories
Python Answers

How to generate a random string with upper case letters and digits with Python?

Sometimes, we want to generate a random string with upper case letters and digits with Python.

In this article, we’ll look at how to generate a random string with upper case letters and digits with Python.

How to generate a random string with upper case letters and digits with Python?

To generate a random string with upper case letters and digits with Python, we can use the string join method with random.choices and string.ascii_uppercase and string.digits.

For instance, we write:

import string
import random

N = 10
s = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
print(s)

We call random.choices to generate a random string of characters with upper case and digits as specified by string.ascii_uppercase + string.digits.

k is the number of characters in the string which is N.

We call ''.join to combine the list of chosen characters into a string.

Therefore, s is 'LUG43QPVCR'.

Conclusion

To generate a random string with upper case letters and digits with Python, we can use the string join method with random.choices and string.ascii_uppercase and string.digits.