﻿ Python Int - CodeBlessU
 ⚛ CodeBlessU learn code with examples

# Python Int

## Introduction

``````# Example, Python int data type
int_a = 5
int_b = -2

type(int_a)``````
Integers are whole numbers, which includes
• positive (1,2, ...)
• negative (-1, -2, ...)
• zero (0)

In python, integer numbers are written as below.

Syntax:
`variable = put number here`

• class 'int'

## Int operations

Below table specifies int operations in python.

Operation Description Result data type
when x and y
both int at least one float
x + y sum / addition of x and y int float
x - y subtract y from x int float
x * y multiply x and y int float
x / y x divided by y float float
x // y x integer division y int float
x % y x modulo y, remainder of x / y int float
-x negation of x int float
+x x int float
abs(x) absolute of x int float
divmod(x, y) pair of (x // y, x % y) tuple of int tuple of float
pow(x, y) x raised to power y int float
x ** y x raised to power y int float

### Integer division

It returns floored quotient of x / y. It is also called Floor division. For positive values, it is like removing decimal point and fractional part (digits after decimal point). For negative values, it is like removing decimal point, removing fractional part (digits after decimal point) and shifting result one step away from 0.

Result is always floored in integer division.

``````# Example, integer division
a = 20
b = 3
c = a // b # assign 6

a = 20
b = -3
c = a // b # assign -7``````

### Int divided by int

It returns float.
``````# Example, int divided by int
a = 20
b = 3
type(a/b)``````

### Points to consider for int divided by int and converting result to int

Say, you need int divided by int output as int.

• Now consider 81/10 and 89/10. Are you ok with result value 8 for both?
• Suppose number is negative, -81/10 and -89/10, Are you ok with result value -9 for both?
• Suppose its -91/10, Are you looking for -9 or -10?

You are discussing round (8.1 to 8, 8.9 to 9), rounding for exact middle point (8.5 to 8 or 9?), floor, ceil, etc.

``````# Example, int divided by int
print(81/10)
print(81//10)
print(89/10)
print(89//10)
print(-81/10)
print(-81//10)
print(-89/10)
print(-89//10)
print(-91/10)
print(-91//10)
``````

### Int divided by float

It returns float.

### Float divided by int

It returns float.

### Int operations with float

If operation involves int and float (i.e. mix), it returns float.

### Increment ++ operator, Decrement -- operator

Python does not have ++ increment or -- decrement operator. Use += or -= (i.e short from of assignment with addition / subtraction).
``````# Example, int increment
a1 = 2
a1 += 1 # 3
a1 = a1 + 1 # 4

a1 -= 1 # 3
a1 = a1 - 1 # 2
``````

## Int bases

Below are known bases for integer numbers:
• Binary / bin / Base 2 - it contains only 0, 1.
• Octal / oct / Base 8 - it contains 0 to 7.
• Decimal / dec / Base 10 - it contains 0 to 9.
• Hexadecimal / hex / Base 16 - it contains 0 to 9, A to F / a to f.

When you create integer as binary / octal / decimal or hex, its type remain same (int). Computer will be storing data in binary only, However, when we/our program interact with computer, we generally prefer decimal number.

So, we need a facility to specify that we are passing binary / octal / hex number and not decimal. Key point to note is, with facility available to specify binary / octal / hex (0b/0B/0o/0O/0x/0X), when you try to print without specifying anything, it is generally visible as decimal.

So, we need one more facility to specify that, while printing / fetching, pls provide it in specific base. This conversion to binary / octal / hex returns as string type in python.

### Binary literal

Use prefix 0b or 0B with number to create variable with binary value.

``````# Example, create variable with binary value of decimal 255.
int_a = 0b11111111
int_b = 0B11111111
type(int_a) # returns class 'int'
print(int_a) # prints 255
``````

### Octal literal

Use prefix 0o or 0O with number to create variable with octal value.

``````# Example, create variable with octal value of decimal 255.
int_a = 0o377
int_b = 0O377
type(int_a) # returns class 'int'
print(int_a) # prints 255
``````

### Decimal literal

No prefix required to create variable with decimal value.

``````# Example, create variable with decimal 255.
int_a = 255
type(int_a) # returns class 'int'
print(int_a) # prints 255
``````

### Hex literal

Use prefix 0x or 0X with number to create variable with hex value. Uppercase (A-F) and lowercase (a-f) both are allowed.

``````# Example, create variable with hex value of decimal 255.
int_a = 0xFF
int_b = 0XFF
int_c = 0xff
int_d = 0xFf
type(int_a) # returns class 'int'
print(int_a) # prints 255
``````

## Int limit, biggest, smallest, max, min

In Python 3.x, there is no defined limit for int number. So, biggest and smallest number (i.e. boundary) can not be derived.

## Short, Long, Unsigned, Signed

In Python 3.x, there is no separate short int, long int, signed int or unsigned int.