Module 1 Ch 3 | Learn Python: Difference between Python 2.x और Python 3.x |

0
1029

 

Python 2.x और Python 3.x के बीच का फर्क

 

दोस्तों जब भी हम किसी programming language को सीखने जाते है तो हमारे दिमाग में यह सवाल ज़रूर आता है की कौन सा version सीखे? कौन सा आसान है उसके क्या फायदे है.

सबसे पहले तो हम यह बता देना चाहते हैं कि python दो version में उपलब्ध है:


Python2.x (mainly python 2.7) 

Python3.x(mainly python 3.3)


इन दोनों के ही अलग अलग फायदे और नुकसान है. जब आप किसी project पर काम कर रहे उसमे क्या ज़रूरते है उस basis पे आप अपना version चुन सकते हैं. python2.x python का base है python3.x उसी को modify कर बनाया गया है.

Python2.x एक legacy है, python3.x इस language का आज और कल है. पर वो कहते है न की “ अगर base मजबूत ना हो तो हम उसपे अपना कल नहीं बना सकते हैं ”. इसलिए हम यहाँ python2.7 सीखेंगे. साथ ही लर्निंग purpose से आपको python2.7  सीखने में ही advantage हैं. पर हमे यह जानना ज़रूरी है की python2.x और python3.x के बीच के फर्क क्या है. तो चलिए आज के chapter में हम इन दोनों के बीच के difference को जानते हैं.

वैसे देखा जाए तो, इन दोनों के बीच में बहुत ज्यादा फर्क नहीं है, कुछ function और module के syntax और नए function का difference है. नीचे दिए गए list में सभी को हम details में पढेंगे.

 

  • The__future__ module
  • The print function
  • Integer division
  • Unicode
  • xrange
  • Raising exceptions
  • Handling exceptions
  • The next() function और next() method
  • For-loop variables और the global namespace leak
  • Comparing unorderable types
  • Parsing user inputs via input()
  • Returning iterable objects instead of lists
  • Banker’s Rounding

 

 The _future_module:

 

अगर हम python3.x के function को python2.x में इस्तेमाल किया जाए तो हमे उसके लिए future module import करना होगा. जैसे python के integer division को हम python2.x में इस्तेमाल करना चाहे तो हमे उसे import करना होगा.

syntax: from __future__ import division

Picture1

देखा कितना आसान है. इसी तरह हम ऐसी ही और भी features को import कर सकते है और उसका python2.x में उपयोग कर सकते हैं.

Print statement:

 

अब तक है सबसे बड़ा बदलाव python2.x और python3.x का print statement है. python 3.x में print() introduce हुआ था. पर  python3.x में print statement को बदल कर print() print function कर दिया. अब बात सही भी है जब same काम ज्यादा आसानी से किया जा सकता है तो काहे paranthesis लगाया जाए. जब आप Python3.x में print statement बिना paranthesis लगाये करते हैं तो error दिखता है. हलांकि python २.x में print अगर () में इस्तेमाल किया जाए तो error नहीं दिखता.

Python2:

Picture2

Python3:

Picture3

Integer division:

 

integer division python2.x और python3.x में काफी फर्क है. और  अगर इसका difference पता न हो तो बहुत खतरनाक हो सकता है.

अगर हमे decimal point में output चाहिए  तो python2.x में float(5)/2. या 5/2.0 लिखा जाता है. उदहारण के तौर पर

Python2:

Picture4

पर अगर यही चीज आपको python 3.x में लिखा जाता है तो देखिये क्या चेंज होता है, देखिये

Python3:

Picture5

Picture6

 

Unicode:

 

Python 2 के पास  ASCII str() types है और अलग से  unicode(),  byte type के कोई type नहीं है. अब Python 3 में finally Unicode (UTF-8) str ings और  2 byte classes: byte and bytearrays introduce किया गया. चलिए आपको एक EXAMPLE से समझाने का try करते हैं.

Python2:

Picture7

Python3:

Picture8

xrange:

 

python2.x में xrange iterable object बनाने के लिए उपयोग करते थे जैसे for loop या list में.  इन शब्दों से घबराने की जरुरत नहीं है ये सब हम आगे पढने वाले ही हैं. खैर, हम इसे iterate करके infinetly run कर सकते हैं. python2.x में range() का उपयोग एक बार iterate करने का हो तो इसका उपयोग कर सकते हैं. Python3.x में range function को xrange function की तरह उपयोग करते हैं. xrange function namerror देगा. आइये आपको दिखाते हैं कैसे.

Picture9

python2:

Picture10

output:

 Picture11

Python3:

Picture12

output:

Picture13

xrange error

Picture14

Handling exceptions:

 

Handling expections python3 में कुछ कुछ  बदल गया है. Python3 में हम “as” keyword का उपयोग करते हैं. जैसे:

Picture15

Raising exceptions:

 

Raising exceptions के syntax python3 में अलग है. अगर हमे user को output में error दिखाना हो तो नीचे दिए गए syntax को उपयोग करना होगा:

raise IOError(“your error message”)

ये syntax python2 में भी काम करता है. लेकिन python2 में use किया जाने वाला ये syntax raise IOError, “your error message” python3 में उपयोग करने पर error देगा.

Picture16

List comprehension loop variables:

 

Python2 के version में जो variable list में iterate होता है अगर उसी नाम से global variable का भी नाम रखा जाए तो उसकी value में फेर-बदल होगा. लेकिन यह problem python3 में फिक्स कर दी गयी है. इसमें global variable की value नहीं बदलती है.

Picture17

 input function:

 

python3 में input function में string value store होती है. python2 में string value को user से लेने के लिए raw_input का उपयोग करते हैं.

Picture18

 Returning iterable objects list के बदले:

 

अगर हम range function का उपयोग python2 में करते हैं, तो ये list type में value स्टोर करता है. जब कि python3 में इसकी value range type में ही होती है. अगर इसे list में बदलना हो तो list() function का उपयोग करना होता है.

Picture19

 next function और .next method:

 

python2 में हम next function और next method दोनों का इस्तेमाल होता है जब की python3 में next function का तो वैसे ही इसतेमाल होता है पर .next method use करने पर “Attribute error” देता है.

Picture20

तो दोस्तों ये थे main difference python2 और python3 के बीच में. दोनों ही version अपनी ज़रूरते के हिसाब से सही हैं python2 में लाइब्रेरीज का ज्यादा support है तो वही python3 में inbuilt function का. तो पहला module यहाँ पर ख़त्म होता है. इसमें आपने introduction, इंस्टालेशन और python 2.x और 3.x में डिफरेंस सीखा. अगले module में हम python के fundamentals के बारे में पढेंगे. गुड बाय.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here