How can I check if a string has a numeric value in it in Python? [duplicate]

Use the .isdigit() method: >>> ‘123’.isdigit() True >>> ‘1a23′.isdigit() False Quoting the documentation: Return true if all characters in the string are digits and there is at least one character, false otherwise. For unicode strings or Python 3 strings, you’ll need to use a more precise definition and use the unicode.isdecimal() / str.isdecimal() instead; not … Read more

Round a floating-point number down to the nearest integer?

int(x) Conversion to integer will truncate (towards 0.0), like math.trunc. For non-negative numbers, this is downward. If your number can be negative, this will round the magnitude downward, unlike math.floor which rounds towards -Infinity, making a lower value. (Less positive or more negative). Python integers are arbitrary precision, so even very large floats can be … Read more

Can I hint the optimizer by giving the range of an integer?

Yes, it is possible. For example, for gcc you can use __builtin_unreachable to tell the compiler about impossible conditions, like so: if (value < 0 || value > 36) __builtin_unreachable(); We can wrap the condition above in a macro: #define assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0) And use it like so: assume(x … Read more