Categories
Python Answers

How to check if string input is a number with Python?

Sometimes, we want to check if string input is a number with Python.

In this article, we’ll look at how to check if string input is a number with Python.

How to check if string input is a number with Python?

To check if string input is a number with Python, we can use the string’s isnumeric method.

For instance, we write:

a = input()
isnumeric = a.isnumeric()
print('is numeric' if isnumeric else 'not numeric')

We call input to get user input and assign the input value to a.

Then we call isnumeric on a to check if it’s numeric.

If it is, True is returned. Otherwise, it returns False.

Therefore, if we enter numbers, we see 'is numeric'. Otherwise, 'not numeric' is printed.

Conclusion

To check if string input is a number with Python, we can use the string’s isnumeric method.

Categories
Python Answers

How to download image using Python requests library?

Sometimes, we want to download image using Python requests library.

In this article, we’ll look at how to download image using Python requests library.

How to download image using Python requests library?

To download image using Python requests library, we can use the requests.get method to make a GET request.

Then we call shutil.copyfileobj to save the file to disk.

For instance, we write:

import requests
import shutil

url = 'https://i.picsum.photos/id/926/200/300.jpg?hmac=jlGQWyYJAmrBGxcsX5Uwr_J1N3bMHU46d3660T6emao'
path = 'photo.jpg'

r = requests.get(url, stream=True)
if r.status_code == 200:
    with open(path, 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)

We define the url to get the image from and the path to save the image to.

Then we call requests.get with the url and stream set to True to make the request.

Then if r.status_code is 200, then we call open to open the file path with 'wb' permission to write the file to the path whether it exists or not.

Then we set r.raw.decode_content to True to decode the file content.

And finally, we call shutil.copyfileobj with r.raw to save the content to file f.

Now we should see the photo displayed when we open photo.jpg.

Conclusion

To download image using Python requests library, we can use the requests.get method to make a GET request.

Then we call shutil.copyfileobj to save the file to disk.

Categories
Python Answers

How to return multiple values from a function with Python?

Sometimes, we want to return multiple values from a function with Python.

In this article, we’ll look at how to return multiple values from a function with Python.

How to return multiple values from a function with Python?

To return multiple values from a function with Python, we can use the return statement with values separated by commas.

For instance, we write:

def foo():
    return True, False


x, y = foo()
print(x)
print(y)

to define the foo function that returns True and False.

And then we call foo and assign the returned values to x and y.

Therefore, we see:

True
False

printed.

Conclusion

To return multiple values from a function with Python, we can use the return statement with values separated by commas.

Categories
Python Answers

How to create an ordered set with Python?

Sometimes, we want to create an ordered set with Python.

In this article, we’ll look at how to create an ordered set with Python.

How to create an ordered set with Python?

To create an ordered set with Python, we can use the ordered-set package.

To install it, we run:

pip install ordered-set

Then we write:

from ordered_set import OrderedSet

letters = OrderedSet('abracadabra')
print(letters)

We use the OrderedSet class from the module to create an ordered set and assign that to letters.

Therefore, letters is OrderedSet(['a', 'b', 'r', 'c', 'd']).

Conclusion

To create an ordered set with Python, we can use the ordered-set package.

Categories
Python Answers

How to build a basic Python iterator?

Sometimes, we want to build a basic Python iterator.

In this article, we’ll look at how to build a basic Python iterator.

How to build a basic Python iterator?

To build a basic Python iterator, we can define a class with the __iter__ and __next__ methods.

For instance, we write:

class Counter:
    def __init__(self, low, high):
        self.current = low - 1
        self.high = high

    def __iter__(self):
        return self

    def __next__(self):
        self.current += 1
        if self.current < self.high:
            return self.current
        raise StopIteration


for c in Counter(3, 9):
    print(c)

We define the Counter iterator class.

The __iter__ method which returns self.

And the __next__ method returns the the next value to return with the iterator.

We increment self.current by 1 and return that if self.current is less than self.high.

Otherwise, we raise the StopIteration error to stop the iterator.

Finally, we use the iterator with the for loop to print the value between 3 and 9 exclusive.

Therefore, we see:

3
4
5
6
7
8

printed.

Conclusion

To build a basic Python iterator, we can define a class with the __iter__ and __next__ methods.