One of the built-in data types in Python is integer data type.Using this data type we can use many numbers varying from small to large range of integers in Python.In fact the largest number you can use in Python is limited only by the amount of memory you have.The integers you can in Python are positive integers,negative integers and zero(0).
As we known Python is dynamically typed language so to use an integer value simply write a name and assigned it any integer value.
>>> in=908 #integer type >>> in1=9203758 #integer type
Python support all the mathematical operator +,-,/,*,% square,cube.And to perform calculation it is not necesary that you give names to each and every integer you want to use.Calculation can be done directly using the numbers.Let us see each of the operations in detail below.
The addition operator is represented by +(plus) sign.
>>> 45+90 135 >>> 345+78+10 433 >>> 23+89+90+(910+10) 1122
You can also use object to perform addition.
>>> x=90 >>> y=10 >>> x+y 100 >>> print(x+y) #work fine 100
The subtraction operator is represented by minus sign(-).
>>> 45-90 -45 >>> 345+78-10 413 >>> x=10 >>> y=10 >>> x-y 0
To perform division we can use ‘/’ slash sign or ‘//’ double slash.The single slash ‘/’ will return the result a real number and the double slash will return result as integer.In other words using double slash will discard the fractional part if any is found.
>>> 90/45 #output is real number 2.0 #a real number >>> >>> 90//45 >>> 2 #integer 2 >>> x=10 >>> y=10 >>> x/y 1.0 >>> 34/3 11.333333333333334 #fractional part present >>> 34//3 11 #no fractional part
Another code example is given below.
>>> 45/8 5.625 >>> 45//8 5 >>> -45/8 -5.625 >>> -45//8 6
In -45/8 the output is -5.625 which is fine.However,in -45//8 the output cannot be a real number so the actual output which is -5.625 is rounded off towards negative infinity.Hence, -6 is the output.
Multiplication is performed using ‘*’ asterisk.
>>> 9*5 45 >>> (4+90)*2 188 >>> 4+90*2 >>> 184
The output of ‘(4+90)*2’ and ‘4+90*2’ do you know why?
In ‘(4+90)*2’ the addition is performed first and then the multiplication is performed.The addition is done first because the bracket force it to.
In ‘4+90*2’ there is no bracket so the standard mathematical rule is applied *(mulitplication) or /(division) first then +(addition),-(subtraction).
Another example is shown below.
>>> (23*56)/5 257.6 >>> (23*56)//5 257
Square or cube or 4th power,5th power …
The square,cube or 4th power or 5th power can be obtained by using the sign ‘**’ and use 2 for square ,3 for obtaining the cube ,4 for 4th power,5 for 5th power and so on.
>>> 2**2 #gives square 4 >>> 2**5 #gives 5th power 32 >>> 4**3 #gives cube of 4 64 >>> 3**500 36360291795869936842385267079543319118023385026001623040346035832580600191583895484198508262979388783308179702534403855752855931517013066142992430916562025780021771247847643450125342836565813209972590371590152578728008385990139795377610001
Python could still handle 3 to the power 500 and the result is enormous!!
To obtain a remainder means when the number is not exactly divisible by divisor what remains is the remainder.Say when 5 is divided by 2 we get 1 as the remainder.To obtain remainder in Python we use the operator ‘%’.
>>> 5%2 1 >>> 34%9 7
Truncating a number
If you do not want round off a number but instead truncate a real number to integer then you can use the built-in int() function.
>>> int(-12.34) -12 >>> int(90.99) 90
Note the truncation is always done toward the 0 in the number-line.