python


Number Types in Python


int

Used for storing integer numbers without any fractional part.
DIFFERENT WAYS OF REPRESENTING int IN PYTHON:
1) As decimal number( base 10): This is the default way of representing integers. The term base 10 means, 10 digits from 0 to 9 are allowed. Example: a=25.
2) As binary number( base 2): The term base 2 means, only 2 digits from 0 and 1 are allowed. But we need to prefix the number with 0b or 0B , otherwise Python will take it to be a decimal number. Example: a=0b101 is binary representation for decimal number 5. Although we can assign binary value to the variable but when we display it we always get output in decimal number system form. We cannot provide any other digit except 0 and 1 while giving binary value , otherwise Python will generate syntax error. We can provide negative values in binary number system also by prefixing 0b with - .Example: -0b101 (decimal value: -5)
3)As octal number(base 8): The term base 8 means , only 8 digits from 0 to 7 are allowed. But we need to prefix the number with zero followed by small o or capital O i.e. either 0o or 0O , otherwise Python will take it to be a decimal number. Example: a=0o101 (decimal value: 65). We cannot provide any other digit except 0 , 1 , 2 , 3 , 4 , 5 , 6 and 7 while giving octal value , otherwise Python will generate syntax error. Just like binary number system , we can provide negative values in octal number system also by prefixing 0O with -. Example: a=-0o101 (decimal value: -65).
4)As hexadecimal number( base 16): The term base 16 means , only 16 digits from 0 to 9 , a to f and A to F are allowed. But we need to prefix the number with zero followed by small x or capital X i.e. either 0x or 0X , otherwise Python will take it to be a decimal number.Example: a=0x101 (decimal value: 257).We cannot provide any other value except the digits and characters from A to F while giving hexadecimal value , otherwise Python will generate syntax error.Just like other number systems , we can provide negative values in hexadecimal number system also by prefixing 0x with -.

We know that Python allows us to represent integer values in 4 different forms like int , binary , octal and hexadecimal Moreover it also allows us to convert one base type to another base type with the help of certain functions. These functions are:
1) bin(): The bin() function converts and returns the binary equivalent of a given integer.
Syntax : bin(a).
Parameters : a : an integer to convert . This value can be of type decimal , octal or hexadecimal.
Return Value : A string representing binary value.
2) oct(): The oct() function converts and returns the octal equivalent of a given integer.
Syntax : oct(a).
Parameters : a : an integer to convert . This value can be of type decimal , binary or hexadecimal
Return Value : A string representing octal value.
3) hex(): The hex() function converts and returns the hexadecimal equivalent of a given integer.
Syntax : hex(a)
Parameters : a : an integer to convert . This value can be of type decimal , octal or bin.
Return Value : A string representing hexadecimal value.
Examples:


>>> bin(25)
0b11001
>>> oct(25)
0o31
>>>hex(0o401)
0x101


float

Float values are specified with a decimal point. So 2.5 , 3.14 , 6.9 etc are all examples of float data type. Just like double data type of other languages like Java/C , float in Python has a precision of 16 digits.For float, we can only assign values in decimal number system and not in binary , octal or hexadecimal number system.Float values can also be represented as exponential values. Exponential notation is a scientific notation which is represented using e or E followed by an integer and it means to the power of 10.


>>> a=2.5
>>> print(a)
2.5
>>> 10/3
3.3333333333333335
>>> a=3.5e4
>>> a
35000.0


complex

Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part. For example: 4+3j , 12+1j etc. The letter j is called unit imaginary number.It denotes the value of √-1 , i.e j² denotes -1.For representing the unit imaginary number we are only allowed to use the letter j (both upper and lower case are allowed).Any other letter if used will generate error.he letter j , should only appear in suffix , not in prefix. The real and imaginary parts are allowed to be integers as well as floats. The real part can be specified in any int form i.e. decimal , binary , octal or hexadecimal but the imaginary part should only be in decimal form. We can display real and imaginary part separately by using the attributes of complex types called “real” and “imag”. Don’t think real and imag are functions , rather they are attributes/properties of complex data type


>>> a=2.5
>>> print(a)
2.5
>>> 10/3
3.3333333333333335
>>> a=3.5e4
>>> a
35000.0