Python Data Types - int

          

To store numerical values, we have 3 data types in python out of which we are going to see int i.e., integer data type here.

 

Keyword: int

 

Numeric value without any fractional part in it can be consider as number with int data type.

For example: 0, 55, -32, 8694848, 100 these are numbers with int data type.

 

1.32, 5.3, 0.4 such numbers cannot be considered as int type.

 

To initialize a variable with int value we can just write variable name equal to sign (=) and then integer value.

e.g.

ball_count= 554

distance_travelled= 98

 

Note: Like other programming languages we don’t have to mention data type keywords before variable name in initialisation. And also, we do not have to declare a variable separately in python.

 

 

There are 4 forms in which we can represent an integer value in python.

1.     Decimal Form (base -10)

2.    Binary Form (base-2)

3.    Octal Form (base-8)

4.    Hexadecimal Form(base-16)

 

#Decimal Form(base-10)

 

          This is general form which we use in everyday life too to represent a number. Number which is represented using 0 to 9 is a decimal number.

e.g., 

          a=100

          b=47149

          c=-38

These all values are in decimal form.

 

#Binary Form(base-2)

 

          Binary numbers can only be represented using 0’s and 1’s.

e.g., 1011, 1111, 1010001111,10000001

These values are good examples of binary number.

 

We can convert a binary value into decimal value like shown below with example.

 

a=10111

 

 1                 0                  1                  1                  1

=   (2^4*1)   +  (2^3*0)  +  (2^2*1) + (2^1*1)  + (2^0*1)

=      16          +       0          +      4         +    2         +      1

=    23

 

So, 10111 of binary is 23 of decimal.

 

Like wise we can calculate binary to decimal.

 

We can write 10111 generally, but while in python 10111 will not make interpreter understand that this is the binary number. In python binary number must be prefixed with ‘0b’ or ‘0B’.

 

i.e., if we wanted to assign 10111 to a variable it will be done like,

 

Like this only we have to declare a binary value.

 


a=0b10111       

d=0B1100

 

 

#Octal Form(base-8)

         

          Octal Numbers can be represented only using 0 to 7 numbers. Like we had prefix for binary numbers(0b) for octal also we have prefix ‘0o’ (zero and small or capital ‘O’).

 

e.g.,

 

These are valid octal numbers

 

a=0o175

b=0o004   

 

Below are some examples of invalid octal numbers

 

e.g.,

 

a=0o814

b=0o191

 

These are not octal because octal number should only have 0 to 7 numerals. Octal numbers can be converted to decimal as below.

 

e.g.,

                    1                  7                  5

          =     (8^2*1)   + (8^1*7)  + (8^0*5)

          =        64      +        56      +        5

          =        125

175(base-8) =125(base-10)

 

#Hexadecimal form(base-16)

 

         We can represent hexadecimal numbers 0 to 9 and A to F (can be a to f as well)

 

Decimal numbers

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

Decimal numbers

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

 

Like binary & octal must have prefix ‘0X’ or ‘0x’. Some valid examples of hexadecimal numbers

a=0xA123


b=0XBAD

c=0x89FAB

 

Can be converted to decimal as below,

 

                  A                       1                    2                   3

= (16^3*10)   +    (16^2*1)  +  (16^1*2)  +  (16^0*3)

=    40960      +         256      +     32          +        3

=      41251


We have seen all 4 forms in which we can represent an integer value. When we declare a value in decimal, binary, octal & hexadecimal forms still while printing the values interpreter automatically converts it into decimal form which is standard form.







 
    However, we can print values in binary, octal & hexadecimal forms as well using some built in functions. Also, we can convert integer values from one form to another i.e., decimal to binary or binary to hexadecimal etc.

 int(value) : converts values from any other form to decimal value. And the value which the function returns it is in the class int type.

 **imp : other three functions oct(), bin(), and hex() converts to respective forms. Only difference is that these three returns values in class str type.









Comments

Popular posts from this blog

Python Data Types - float

Python Data Types - Complex