int and long were "unified" a few versions back. Before that it was possible to overflow an int through math ops.
3.x has further advanced this by eliminating long altogether and only having int.
- Python 2:
sys.maxintcontains the maximum value a Python int can hold.- On a 64-bit Python 2.7, the size is 24 bytes. Check with
sys.getsizeof().
- On a 64-bit Python 2.7, the size is 24 bytes. Check with
- Python 3:
sys.maxsizecontains the maximum size in bytes a Python int can be.- This will be gigabytes in 32 bits, and exabytes in 64 bits.
- Such a large int would have a value similar to 8 to the power of
sys.maxsize.
integer - Can you explain how int function(int()) works in Python? - Stack Overflow
integer - How does Python manage int and long? - Stack Overflow
How much memory do int's take up in python?
What does Int() mean?
int and long were "unified" a few versions back. Before that it was possible to overflow an int through math ops.
3.x has further advanced this by eliminating long altogether and only having int.
- Python 2:
sys.maxintcontains the maximum value a Python int can hold.- On a 64-bit Python 2.7, the size is 24 bytes. Check with
sys.getsizeof().
- On a 64-bit Python 2.7, the size is 24 bytes. Check with
- Python 3:
sys.maxsizecontains the maximum size in bytes a Python int can be.- This will be gigabytes in 32 bits, and exabytes in 64 bits.
- Such a large int would have a value similar to 8 to the power of
sys.maxsize.
This PEP should help.
Bottom line is that you really shouldn't have to worry about it in python versions > 2.4
Going through the docs I didn't find a definite answer
If I do
x = 234234 sys.getsizeof(x)
It returns 28, so that's 224 bits ?
Apparently getsizeof() returns the size of an object in bytes, is this size consistent across all int's in python though?
I know that there are long ints, and that they are converted automatically to long ints at a certain value in python, but I'm just wondering about ints for now.
In Java the size that I got was 32 bit, so 4 bytes.
Is this correct, that :
python int = 224 bits java int = 32 bits
And is this because the python int has more methods etc?
Basically what I was trying to state was that across programming languages the data type sizes were fairly conventional and that an int in C# would be the same size as an int in Python / Java. I think this might be wrong though.
Thanks!
From what I have gathered it means that the code is going to be looking for an intger but I dont want to continue assuming that this is correct if I am wrong. Thanks guys!
good day everyone,
would anyone be able to help me figure out how to make a function to calculate a size for a circle and increase it's size by a multiple of 2,3,4 and so on with retaining it's original quotient value?
i would have a default ellipse right in the middle of the canvas with a size of 10,10
i need help to make a function to increase that size by pressing key 2 and multiply it and so on with key 3, key 4.....
i just couldnt get around how to use int() and str() properly
It does exactly what it says - converts a string to integer in a given numeric base. As per the documentation, int() can convert strings in any base from 2 up to 36. On the low end, base 2 is the lowest useful system; base 1 would only have "0" as a symbol, which is pretty useless for counting. On the high end, 36 is chosen arbitrarily because we use symbols from "0123456789abcdefghijklmnopqrstuvwxyz" (10 digits + 26 characters) - you could continue with more symbols, but it is not really clear what to use after z.
"Normal" math is base-10 (uses symbols "0123456789"):
int("123", 10) # == 1*(10**2) + 2*(10**1) + 3*(10**0) == 123
Binary is base-2 (uses symbols "01"):
int("101", 2) # == 1*(2**2) + 0*(2**1) + 1*(2**0) == 5
"3" makes no sense in base 2; it only uses symbols "0" and "1", "3" is an invalid symbol (it's kind of like trying to book an appointment for the 34th of January).
int("333", 4) # == 3*(4**2) + 3*(4**1) + 3*(4**0)
# == 3*16 + 3*4 + 3*1
# == 48 + 12 + 3
# == 63
The base value tells python to interpret the given string to be a value of a different base.
For example, the 1011 in base 2 is 11. Thus, int('1011', 2) returns 11.
On the other hand, 1011 in base 3 is 31. Thus, int('1011', 3) returns 31.
Decimals are in base 10, which is why the default value of base is 10.
A fun side-effect of choosing a numeric base, is that there does not exist a digit in that system that is higher than (or equal to) the base itself. This is why we do not have a digit for ten in the decimal system, while the hexadecimal system (base 16) uses the digit A for ten. This is why you were getting errors for asking a number with the digit 5 to be interpreted in base 4.
As the other answers have mentioned, the int operation will crash if the string input is not convertible to an int (such as a float or characters). What you can do is use a little helper method to try and interpret the string for you:
def interpret_string(s):
if not isinstance(s, basestring):
return str(s)
if s.isdigit():
return int(s)
try:
return float(s)
except ValueError:
return s
So it will take a string and try to convert it to int, then float, and otherwise return string. This is more just a general example of looking at the convertible types. It would be an error for your value to come back out of that function still being a string, which you would then want to report to the user and ask for new input.
Maybe a variation that returns None if its neither float nor int:
def interpret_string(s):
if not isinstance(s, basestring):
return None
if s.isdigit():
return int(s)
try:
return float(s)
except ValueError:
return None
val=raw_input("> ")
how_much=interpret_string(val)
if how_much is None:
# ask for more input? Error?
int() only works for strings that look like integers; it will fail for strings that look like floats. Use float() instead.