Let’s talk about the integer type in Python. We’ve just taken a variable x, and assigned it a value of 5, using the type() function. We could see that this type is int. Python 3 no longer has a long type. Basically, the long type was renamed int, and it is now containing all the integer and long values in Python 3. In Python 2, you had both an int type, and a long type. There is no distinction any longer in Python 3. If you use a 0x prefix, you can then indicate a hexadecimal value. Although it will be stored as an integer in that variable y. Likewise, you can use a 0o prefix, if you want to use a octal value. In Python 2, they just used a zero prefix in front of a number to indicate an octal value.
x = 5 type(x) The output is displayed:The window titled int_demo.py includes the code: x = 5 y = 10 y = 0xA # hexadecimal y = 0o12 # octal (Python 2 uses 012) y = 0b1010 # binary print('x =', x, ',', 'y =', y) #Typical comparisons can be made print('x == y =', x == y) print('x != y=', x != y) print('x >= y =', x >= y) print('x > y =', x > y) print('x<= y =', x <= y) print('x < y =', x < y) # The usual operators can be used: print('x + y =', x + y) print('x - y =', x - y) print('x * y =', x* y) print('x / y =', x / y) # In Python 2, x / y uses floor division like: print('x // y =', x // y) print('x % y =', x % y) print('x ** y=', x ** y) # There are several useful builtin functions: print('divmod(x, y) =', divmod(x, y)) print('pow(x, y) =', pow(x,y)) print('abs(-x) =', abs(-x)) print('int(5.2) =', int(5.2)) print('int("0xff",16) =', int("0xff", 16)) print('float(x) =', float(x))
0b is used for binary. In all of these cases, these values are the same as the decimal 10. And we can see if we go run this program, the first thing that is output is that x = 5, and that y = 10, where that binary value is actually represented as a decimal value internally by Python. You can also see that you can use typical comparison operators in Python, like == for testing for equality; != for inequality. Of course, we have >=, >, <=, or <. In each of these cases, you can see that there is a Boolean, or true or false value that's returned. You can also use your typical operators as you would expect; plus is used for addition, minus for subtraction, asterisk for multiplication, and forward slash for division.
Something that's different about Python 3 is that division will yield a floating point number, rather than using floor division, which Python 2 would do. You can force floor division in Python 3, or Python 2, by using the double forward slash, which will always give you a integer divisor. You can use the percent operator as the modulus, or the remainder after division. Double asterisk is used for a power; so x raised to the power of y. There are several built-in functions that you'll find useful, like divmod, which will return a tuple with a divisor and the remainder. You also have a power function, which raises the x to the y value; an absolute value function, which will always yield a magnitude or a positive value; the integer function, which will convert something like a floating point number into an integer number. It'll even do that with different basis, like base 16. If you want to convert an integer into a floating point number, then you can use the float() function.
x / y = 0.5 x // y = 0 x % y = 5 x ** y = 97655625 int_demo.py: print('x / y =', x / y) # In Python 2, x/ y uses floor division like: print('x // y =', x // y) print('x % y =',x % y) print('x ** y =', x ** y) # There are several useful builtin functions: print('divmod(x, y) =', divmod(x, y)) print('pow(x, y) =', pow(x,y)) print('abs(-x) =', abs(-x)) print('int(5.2) =', int(5.2)) print('int("0xff",16) =', int("0xff", 16)) print('float(x) =', float(x)) The output: divmod(x, y) = (0, 5) pow(x, y)=9765625 abs(-5) = 5 int(5.2) = 5 int("0xff, 16) = 255 float(x) = 0.5
As far as inline operations, it doesn't support ++, or --, and things like that. But you can do +=, to do the equivalent of something like x = x + y. You can also do the -=, *=, and /=, for inline subtraction, multiplication, or division. And you can see those results there. If you want to assign multiple variables to multiple values, you can do that like this, where x, y = 4, 2. As we can see, this turned out that x=4, y=2. Finally, we might mention that bitwise operators can be used on integers. So if you want to do a bitwise or, exclusive or, bitwise and, a left shift of bits, or a right shift of bits, or even inversion, we have the special symbols or operators that allow you to perform those operations.