If you want to use math, you can do following:
The implementation, of what I described:
Math is not used. I use the fact, that
iterate over a
+If you want to check, if a number is really an integer, use exceptions:
Floats do have the method
- Get the digits of the number with math.log10
- Convert the result of digits to integer
- In a for-loop with reversed(range(digits + 1))
- Each iteration give you a integer (large > 0), which should be the position of the digt.
- Integer division: big_number // 10**digits
- Use modulo, to get only one digit: result = result % 10
The implementation, of what I described:
def split_numbers(number): digits = int(math.log10(number)) for d in reversed(range(digits + 1)): digit = (number // 10 ** d) % 10 yield digitMuch easier is following code:
def split_digits(number): return [int(d) for d in str(number)]The number could be a
int
or a str
.Math is not used. I use the fact, that
str
is iterable. If youiterate over a
str
you'll get char by char.+If you want to check, if a number is really an integer, use exceptions:
try: big_number = int(big_number_as_str) except ValueError: print('Is not a int') else: print('Is a int') print(big_number)In Python a complex consists of two floats. One float for the real part, one float for the imaginary part.
Floats do have the method
is_integer()
, so you can test also, each part of the complex if it's an integer.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!