Skip to main content

Python Numbers

Number is a values that are capable to perform all type of mathematical operation using operators. In python program numbers are deal integer and floating point value. for example 7 and 7.10 is numeric value. 7 are is an integer value and 7.10 is floating point number.

Integer values are related to whole number, negative as well,positive values, that have a no portion of fraction part. and floating point are capable to store of fraction part. for example.

#Display value type
print(type(7)) 
print(type(7.10))
print(type(-7))
print(type('7'))
print(type("7.10"))
print(type("""7"""))
print(type('''7'''))
Output
<type 'int'>
<type 'float'>
<type 'int'>
<type 'str'>
<type 'str'>
<type 'str'>
<type 'str'>

In given program when put a number within the quotation marks like that (') single quotation and single triple quotation ('''), and similar to double quotation marks (" "). that interpreter are change the behaviour of number to string. so when defined a number then not use quotation marks. In other situation we can also convert (type cast) of string value to an integers or floating point. for example.

#Case 1 
#Type cast string to float point value
number=float("7.10") #7.10
print(number)

#Case 2 
#Type cast string to float point value
number=float("7") #7.0
print(number)


#case 3
#Type cast floating point to integer value
number=int(7.10) # 7
print(number)

#case 4
#Type cast string value to integer value
number=int("7")
print(number) # 7
Output
7.1
7.0
7
7

In this program there are given 4 cases. case 1 are used to convert floating point string to floating point number. float() function are used in a this situation, given a valid floating point string and this function are return the result of floating point(7.10).

Case 2 is interesting that are explaining the working of how to convert string integer value to floating point. in this situation float() function are provide a valid integer value string. this function are returning the floating point result (7.0). note that this are adding of fraction part.

Case 3 is conversion of floating point to integer value. in this case use int() function and given a floating point parameter to this function. this function are remove fraction portion and returning the simple integer value(7).

Case 4 are used to simply convert valid string integer to integer.

Other number types

There are also two more numeric type data are exist in a python language. this is such as a long number and complex number. for example.

#long number example
print(type(143786611L))
print(type(3456789012l))

#complex number example
print(type(7.123j))
print(type(123J))
Output
<type 'long'>
<type 'long'>
<type 'complex'>
<type 'complex'>

Append the end of integer is (L or l) is indicates the long data type. and similar append the end of integers and float value includes of (j and J) are indicates complex number.

long type is not work of floating point value. see this example

#long number example
print(type(123.45L))
Output
  File "test.py", line 2
    print(type(123.45L))
                     ^
SyntaxError: invalid syntax

Type Conversion (casting) of complex and long number as follows

#case 1 integer to long type
number=long(1234)
print(type(number),number)

#case 2 string integer value to long type
number=long("5678")
print(type(number),number)

#case 3  string long value to long type
number=long("5678L")
print(type(number),number)


#case 4  string integer value to complex type
number=complex("9876")
print(type(number),number)


#case 5  string float value to complex type
number=complex("123.45")
print(type(number),number)

#case 6  string complex value to complex type
number=complex("123.45j")
print(type(number),number)
Output
(<type 'long'>, 1234L)
(<type 'long'>, 5678L)
(<type 'long'>, 5678L)
(<type 'complex'>, (9876+0j))
(<type 'complex'>, (123.45+0j))
(<type 'complex'>, 123.45j)

complex number is combination of real number and imaginary part. (j or J) are used to indicates that is imaginary part.

Python number operation

Number data are defined by python classes like int, float, long and complex. python are dynamic type check so this reason there are no need to provide variable data type.

Number system : This is provide a special behaviors of number such as number may be binary, hexadecimal and octal. normally number is support decimal system but appending of few characters in perfix of number. then that are possible to change its behaviour.

Prefixes Meaning Example
0b or 0B Binary 0b0101 (binary) = (int 5)
0x or 0x Hexadecimal 0xB (hexa) = (int 11)
0o or 0o Octal 0o16 (Octal) = (int 14)

For example

number=0b0101 #binary literal 
print(type(number),number) # int 5

number=0xB # Hexadecimal literal
print(type(number),number) # int 11

number=0o16  # Octal literal
print(type(number),number) # int 14
Output
(<type 'int'>, 5)
(<type 'int'>, 11)
(<type 'int'>, 14)

Note that in this example are append prefixes are used to change given value to special number. let take one more example.

number=0b0101*2 #binary literal 
print(type(number),number) # int 10

number=0xB-3 # Hexadecimal literal
print(type(number),number) # int 8

number=0o16/4  # Octal literal
print(type(number),number) # int 3
(<type 'int'>, 10)
(<type 'int'>, 8)
(<type 'int'>, 3)




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment