sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » Numbers Data Types

Numbers Data Types में numeric values; store की जाती है | Python में Integer(int), Floating-Point(float) और Complex(complex) ये तीन प्रकार के Numbers data types होते है | Number Objects; immutable होते है, मतलब जब Number Object को create किया जाता है तब उसकी value बदली नहीं जा सकती है |

Integer Number Data Type

Integer Data Type में Positive और Negative Numeric Values होती है लेकिन उनका अपूर्णांकित हिस्सा नहीं होता है | इस data type के range की कोई limit नहीं होती है |
Source Code :
a = 5
print(a, type(a))
b = -5
print(b, type(b))
Output :
5 <class 'int'>
-5 <class 'int'>


Floating-Point Number Data Type

Floating-Point Number Data Type में Positive और Negative Values होती है लेकिन उनका अपूर्णांकित हिस्सा होता है | अपूर्णांकित हिस्से की limit 15 तक होती है |
Source Code :
a = 5.123456789101217558585456
print(a, type(a))
b = -5.45
print(b, type(b))
c = 5.45
print(c, type(c))
Output :
5.123456789101217 <class 'float'>
-5.45 <class 'float'>
5.45 <class 'float'>


Complex Number Data Type

Complex Numbers में real और imaginary हिस्सा होता है जैसे कि, (a + bj) यहाँ पर a ये real Number और b ये imaginary हिस्सा होता है |
Source Code :
a = 5 + 2j

print(a, type(a))

Output :
(5+2j) <class 'complex'>


Number Type Conversion

Python में अलग-अलग data type को एक ही data type में convert किया जाता है | Python में कुछ functions ऐसे होते है कि जो data type convert करने में मजबूर करते है |

कुछ Type Convertor Functions :
  • int(a)
  • float(a)
  • complex(a, b)

int() Type Convertor

Syntax for int()

int(a)

Example पर float को integer में convert किया गया है | String में अगर characters होते है तो उसे Integer में convert नहीं किया जा सकता लेकिन String में अगर integer Numbers होते है तो उसे Integer में convert किया जा सकता है |
Source Code :
a = "123"
print(int(a))
b = 2.658
print(int(b))
Output :
123
2

float() Type Convertor

float(a)

Example पर integer को float में convert किया गया है | String में अगर characters होते है तो उसे Floating-Point Number में convert नहीं किया जा सकता लेकिन String में अगर integer या Floating-Point Numbers होते है तो उसे Floating-Point में convert किया जा सकता है |
Source Code :
a = "123"
print(float(a))
b = "123.564"
print(float(b))
c = 2
print(float(c))
Output :
123.0
123.564
2.0


complex() Type Convertor

complex(a, b)

Parameter :
a : यहाँ पर 'a' ये real Number होता है |
b : Optional. यहाँ पर 'b' ये imaginary हिस्सा होता है | अगर दिया नहीं जाता तो default value '0' होती है |

Source Code :
a = "123"
print(complex(a))
b = "123.564"
print(complex(b))
c = 2
print(complex(c), end="\n\n")

d = 123
e = 58
print(complex(d, e))

f = "123"
g = 58
print(complex(f, g))
Output :
(123+0j)
(123.564+0j)
(2+0j)

(123+58j)
    print(complex(f, g))
TypeError: complex() can't take second arg if first is a string

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply