Categories
Python Answers

How to check if something is (not) in a list in Python?

Sometimes, we want to check if something is (not) in a list in Python.

In this article, we’ll look at how to check if something is (not) in a list in Python.

How to check if something is (not) in a list in Python?

To check if something is (not) in a list in Python, we can use the not in operators.

For instance, we write

3 not in [2, 3, 4]

to check if 3 isn’t in [2, 3, 4].

It should return False since 3 is in [2, 3, 4].

Conclusion

To check if something is (not) in a list in Python, we can use the not in operators.

Categories
Python Answers

How to take the first N items from a generator or list with Python?

Sometimes, we want to take the first N items from a generator or list with Python.

In this article, we’ll look at how to take the first N items from a generator or list with Python.

How to take the first N items from a generator or list with Python?

To take the first N items from a generator or list with Python, we can use the slice syntax for lists and itertools.islice method for generators.

For instance, we write

top_5 = array[:5]

to return the first 5 items from the array list.

And we get the first items from a generator with

import itertools

top_5 = itertools.islice(my_list, 5) 

We call islice with the my_list generator and 5 to return an iterator with the first 5 items.

Conclusion

To take the first N items from a generator or list with Python, we can use the slice syntax for lists and itertools.islice method for generators.

Categories
Python Answers

How to specify and save a figure with exact size in pixels with Python Matplotlib?

Sometimes, we want to specify and save a figure with exact size in pixels with Python Matplotlib.

In this article, we’ll look at how to specify and save a figure with exact size in pixels with Python Matplotlib.

How to specify and save a figure with exact size in pixels with Python Matplotlib?

To specify and save a figure with exact size in pixels with Python Matplotlib, we set the figsize and dpi when calling figure.

For instance, we write

plt.figure(figsize=(800 / my_dpi, 800 / my_dpi), dpi=my_dpi)

to call figure with the figsize calculated by the width and height in pixels divided by my_dpi.

And we set dpi to my_dpi to set the dpi.

This will set the size to the width and height we want in pixels.

Conclusion

To specify and save a figure with exact size in pixels with Python Matplotlib, we set the figsize and dpi when calling figure.

Categories
Python Answers

How to fix SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ with Python?

Sometimes, we want to fix SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ with Python.

in this article, we’ll look at how to fix SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ with Python.

How to fix SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ with Python?

To fix SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ with Python, should tell the Python interpreter that our string is Unicode.

To do this, we write

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

def func():
    return '£'

to specify that the strings in the code is Unicode with

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

And then we can return '£' in func without errors.

Conclusion

To fix SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ with Python, should tell the Python interpreter that our string is Unicode.

Categories
Python Answers

How to run multi-line statements in the one-line command-line with Python?

Sometimes, we want to run multi-line statements in the one-line command-line with Python.

In this article, we’ll look at how to run multi-line statements in the one-line command-line with Python.

How to run multi-line statements in the one-line command-line with Python?

To run multi-line statements in the one-line command-line with Python, we can use pipe.

For instance, we write

echo -e "import sys\nfor r in range(10): print('rob')" | python

to add code into the double quotes with the \n character to add new lines between lines.

And then we echo the string with echo -e to backslash escape the string and pipe the result to python to run it.

Conclusion

To run multi-line statements in the one-line command-line with Python, we can use pipe.