In the previous chapter, we learned about various datatypes. From this chapter onward, we will learn in detail about each data type.
Numbers in Python
This datatype stores the numeric values and is immutable. For the beginners, it is sufficient to understand that immutable objects are those which can not be changed once created (Though this subject needs a deeper and detailed post). It can be further divided into three types:-
- int - Often called integers, these are positive or negative whole numbers with no decimal point. Prior to Python 3, Python 2 had int and long int types but the long int has been discontinued in Python 3 and now the int can be of unlimited size. e.g. 1, 2, -3, 99 etc.
- float - They have decimal point separating the integer from the fraction e.g. 22.0 , 5.0, -22.0 etc.
- complex - They are of the form a + bj. ‘a’ is the real part, ‘b’ is the imaginary part and j is the square root of -1. Also, both ‘a’ and ‘b’ are floats.
Python has a built-in function type() which tells you the data-type.
>>> type(2)
<class 'int'>
>>> type(2.0)
<class 'float'>
>>> type(3+4j)
<class 'complex'>
Coercion in Python
In Python, one type of number can be easily converted into another type and it is called ‘coercion’.
Any mathematical operation will coerce an integer to a float if one of the operands is a float. Also, the result of the division (/) will always be a float irrespective of whether the operands were integers or floats.
>>> 1 + 2.0
3.0
>>> 1*5.0
5.0
>>> 5/1
5.0
>>> a = 10
>>> b = 2
>>> c = a/b
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
>>> type(c)
<class 'float'>
>>>
Alternatively, you can convert the numbers as under:-
- int(x) will convert x to a plain integer. Also, it will convert the float into the lower integer. i.e int (2.9) will give 2.
- float(x) will convert x to a floating-point number.
- complex(x) will convert x to a complex number with imaginary part ‘0’.
- complex(x,y) will result into x + yj.
>>> int(1.0)
1
>>> int (2.9)
2
>>> float (25)
25.0
>>> complex(2)
(2+0j)
>>> complex(2,3)
(2+3j)
Operators in Python
Before getting into operators, we must note that in all the below mentioned operators except the division (/) operator, the result will be float only if one or both of the operands are floats. If both of them are integers, then the result will be an integer.
- Addition (+) - e.g. 2 + 1 = 3.
- Subtraction(-)- e.g. 3 - 1 = 2.
- Multiplication(*) - e.g. 5 * 2 = 10.
- Division(/) - e.g. 4/2. In this case the result will be a float i.e. 2.0 even when both the operands were integers.
- Truncation/Floor Division(//). It will truncate the fractional part if any and result will be an integer. e.g. 5//2 = 2. In mathematical terms, it will give you the quotient.
- Modulo (%)- It will give the remainder as an answer. e.g. 5 % 3 = 2.
- Exponentiation - e.g. 5 ** 2 = 25.
>>> 2 + 1
3
>>> 2.0 + 1
3.0
>>> 3 - 1
2
>>> 3 - 1.0
2.0
>>> 5 * 2
10
>>> 5.0 * 2
10.0
>>> 4/2
2.0
>>> 5//2
2
>>> 5//2.0
2.0
>>> 5%3
2
>>> 5.0%3
2.0
>>> 5**2
25
Table of Contents
Chapter 6 - Data Types & Variables
पिछले अध्याय में हमने विभिन्न प्रकार के data-types के बारे में सीखा। इस अध्याय से आगे, हम प्रत्येक data-type के बारे में विस्तार से जानेंगे।
Numbers
यह data type numeric values को संग्रहीत करता है ओर यह immutable होते है। शुरुआती लोगों के लिए, यह समझना पर्याप्त है कि immutable objects वे हैं जिन्हें एक बार निर्मित होने के बाद नहीं बदला जा सकता (हालांकि इस विषय को एक गहन और विस्तृत पोस्ट की आवश्यकता है)। इसे आगे तीन प्रकारों में विभाजित किया जा सकता है:
- int - इन्हें अक्सर integers भी कहा जाता है। यह decimal के बिना positive या negative whole numbers होते है। Python 3 से पहले यानि के Python 2 में int व long int type होती थी, लेकिन Python 3 में long int को discontinue कर दिया गया है ओर इसमें int किसी भी length का हो सकता है। जैसे के 1, 2, -3, 99 इत्यादि।
- float – इनमें decimal point होता है जो के integer ओर fraction को अलग करता है। जैसे के 22.0 , 5.0, -22.0 इत्यादि।
- complex – यह a + bj के रूप में होते है। इसमें ‘a’ real part, ‘b’ imaginary part व j -1 का square root होता है। ‘a’ ओर ‘b’ दोनो ही floats होते है।
Python में type () नामक built-in फ़ंक्शन होता है। जो के आपको data-type के बारें में बताता है।
>>> type(2)
<class 'int'>
>>> type(2.0)
<class 'float'>
>>> type(3+4j)
<class 'complex'>
Conversions
Python में आसानी से एक प्रकार के number को दूसरे प्रकार में बदला जा सकता है जिसे ‘coercion’ कहते है।
यदि कोई ऑपरेंड में से कोई एक फ्लोट है, तो कोई भी गणितीय कार्रवाई(mathematical operation) एक integer को एक फ्लोट में बदल देगी । इसके अलावा विभाजन (/) का परिणाम हमेशा एक फ़्लोट होगा चाहे वह ऑपरेंड integers या फ़्लोट्स हो।
>>> 1 + 2.0
3.0
>>> 1*5.0
5.0
>>> 5/1
5.0
>>> a = 10
>>> b = 2
>>> c = a/b
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
>>> type(c)
<class 'float'>
>>>
इसके इलावा, आप निमनलिखित तरीक़े से numbers के प्रकार को बदल सकते है:-
- int(x) x को एक integer में बदल देगा। इसके इलावा, यह एक फ़्लोट को lower integer में बदलेगा अर्थात int (2.9) हमें 2 में बदल देगा।
- float(x) x को floating-point number में बदल देगा।
- complex(x) x को complex number में बदल देगा जिसका के imaginary part ‘0’ होगा।
- complex(x,y) का उत्तर x + yj होगा।
>>> int(1.0)
1
>>> int (2.9)
2
>>> float (25)
25.0
>>> complex(2)
(2+0j)
>>> complex(2,3)
(2+3j)
Operators
ऑपरेटरों के बारें में पढ़ने से पहले, हमें ध्यान देना चाहिए कि डिवीजन (/) ऑपरेटर को छोड़कर सभी नीचे दिए गए ऑपरेटरों में, परिणाम एक फ़्लोट केवल तभी होगा जब एक या दोनों ऑपरेंड फ्लोट होंगे। यदि वे दोनों integer हैं, तो परिणाम भी integer ही होगा ।
- जोड़ (+) – जैसे के . 2 + 1 = 3.
- घटाव (-)- जैसे के . 3 - 1 = 2.
- गुणन(*) – जैसे के . 5 * 2 = 10.
-
डिवीजन(/) - e.g. 4/2. इस स्थिति में परिणाम एक फ्लोट होगा अर्थात् 2.0 जब के दोनों ऑपरेंड integer थे।
-
ट्रंकेशन / फ्लोर डिवीजन (//) अगर परिणाम में fractional पार्ट हो तो यह उसे उड़ा देगा ओर आपको सिर्फ़ integer परिणाम में देगा .जैसे के 5//2 = 2. गणितीय शब्दों में, यह आपको भागफल देगा।
- Modulo (%)- यह शेष(remainder) को उत्तर के रूप में देगा। जैसे 5% 3 = 2
- घातांक – जैसे 5 ** 2 = 25.
>>> 2 + 1
3
>>> 2.0 + 1
3.0
>>> 3 - 1
2
>>> 3 - 1.0
2.0
>>> 5 * 2
10
>>> 5.0 * 2
10.0
>>> 4/2
2.0
>>> 5//2
2
>>> 5//2.0
2.0
>>> 5%3
2
>>> 5.0%3
2.0
>>> 5**2
25
Table of Contents
Chapter 6 - Data Types & Variables