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.

Categories
Python Answers

How to add keyboard input with timeout with Python?

Sometimes, we want to add keyboard input with timeout with Python.

In this article, we’ll look at how to add keyboard input with timeout with Python.

How to add keyboard input with timeout with Python?

To add keyboard input with timeout with Python, we can use the select.select method with sys.stdin.

For instance, we write:

import sys, select

print("You have 5 seconds to answer")

i, o, e = select.select([sys.stdin], [], [], 5)

if (i):
    print("You said", sys.stdin.readline().strip())
else:
    print("You said nothing")

We call select.select with [sys.stdin] and 5 to given users 5 seconds to enter some text.

If i is True, then the user entered something within the time limit and we can read the inputted value with sys.stdin.readline().strip().

Conclusion

To add keyboard input with timeout with Python, we can use the select.select method with sys.stdin.

Categories
Python Answers

How to find all occurrences of a substring with Python?

Sometimes, we want to find all occurrences of a substring with Python.

In this article, we’ll look at how to find all occurrences of a substring with Python.

How to find all occurrences of a substring with Python?

To find all occurrences of a substring with Python, we can use the re.finditer method.

For instance, we write:

import re

indexes = [m.start() for m in re.finditer('test', 'test test test test')]
print(indexes)

We call re.finditer method with the substring to search for and the string we’re searching for the substring in respectively.

Then we call m.start on each entry found to get the index of the start of each match.

Therefore, indexes is [0, 5, 10, 15].

Conclusion

To find all occurrences of a substring with Python, we can use the re.finditer method.