Python एक डायनामिक और स्ट्रॉन्गली टाइप्ड प्रोग्रामिंग भाषा है। यह डक टाइपिंग और ग्रेजुअल टाइपिंग दोनों का उपयोग करता है, टाइप हिंट्स के ज़रिए। इम्परेटिव, डिक्लेरेटिव (जैसे कि फंक्शनल) और ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग की शैलियाँ सभी समर्थित हैं, लेकिन भीतर से Python में सब कुछ एक ऑब्जेक्ट है।
Python कोड की पठनीयता पर बहुत ज़ोर देता है और (Haskell की तरह) फंक्शन, मेथड और क्लास की परिभाषाएँ दर्शाने के लिए सिग्निफिकेंट इंडेंटेशन का उपयोग करता है।
Python को Guido van Rossum ने बनाया और यह पहली बार 1991 में जारी हुआ। Python Software Foundation Python और CPython के विकास के लिए संसाधनों का प्रबंधन और निर्देशन करती है। यह समुदाय के सदस्यों से Python Enhancement Proposals या PEPs के ज़रिए भाषा में बदलाव के प्रस्ताव प्राप्त करती है।
मौजूदा रिलीज़ का पूरा दस्तावेज़ीकरण docs.python.org पर मिल सकता है।
यह पहला कॉन्सेप्ट Python भाषा की 4 प्रमुख विशेषताओं से परिचित कराता है:
def कीवर्ड और return कीवर्ड),आम तौर पर, Python ट्रैक के कॉन्टेंट, टेस्ट और एनालाइज़र टूलिंग Python कोड स्टाइल के लिए PEP 8 और PEP 257 में दिए गए स्टाइल नियमों का पालन करते हैं। इसके अलावा यह अतिरिक्त (ज़ोरदार) सुझाव भी है कि एक अक्षर वाले वेरिएबल नाम न हों।
zen of Python (PEP 20) और What is Pythonic? कुछ और सिद्धांत बताते हैं।
Python ट्रैक पर वेरिएबलो हमेशा snake_case में लिखे जाते हैं, और कॉन्स्टेंट SCREAMING_SNAKE_CASE में
Python में वेरिएबल या कॉन्स्टेंट बनाने के लिए कोई कीवर्ड इस्तेमाल नहीं होता।
इसके बजाय, प्रोग्रामर असाइनमेंट = ऑपरेटर का उपयोग करके किसी भी तरह के ऑब्जेक्ट से नाम (जिन्हें वेरिएबल भी कहा जाता है) बाँध सकते हैं: <name> = <value>।
किसी नाम को उसके जीवनकाल में अलग-अलग वैल्यू (अलग-अलग ऑब्जेक्ट टाइप) पर फिर से असाइन (या फिर से बाँध) किया जा सकता है।
उदाहरण के लिए, my_first_variable को = का उपयोग करके कई बार फिर से असाइन किया जा सकता है, और हर बार असाइन करने पर यह अलग-अलग ऑब्जेक्ट टाइप को दर्शा सकता है:
>>> my_first_variable = 1 # my_first_variable bound to an integer object of value one.
>>> my_first_variable = 2 # my_first_variable re-assigned to integer value 2.
>>> print(type(my_first_variable))
<class 'int'>
>>> print(my_first_variable)
2
>>> my_first_variable = "Now, I'm a string." # <--You may re-bind a name to a different object type and value.
>>> print(type(my_first_variable))
<class 'str'>
>>> my_first_variable = 'You can call me "str".' # <--Strings can be declared using single or double quote marks.
>>> print(my_first_variable)
You can call me "str".
>>> import collections
>>> my_first_variable = collections.Counter([1,1,2,3,3,3,4,5,6,7]) # <--Now my_first_variable has been re-bound to a Counter object.
>>> print(type(my_first_variable))
<class 'collections.Counter'>
>>> print(my_first_variable)
Counter({3: 3, 1: 2, 2: 1, 4: 1, 5: 1, 6: 1, 7: 1})
कॉन्स्टेंट ऐसे नाम हैं जिन्हें किसी प्रोग्राम में सिर्फ एक बार असाइन करना होता है।
इन्हें मॉड्यूल (फाइल) लेवल पर परिभाषित करना चाहिए, और आम तौर पर ये प्रोग्राम के सभी फंक्शन और क्लास को दिखाई देते हैं।
SCREAMING_SNAKE_CASE का उपयोग यह संकेत देता है कि नाम को फिर से असाइन नहीं करना चाहिए, और न ही उसकी वैल्यू बदलनी चाहिए।
# All caps signal that this is intended as a constant.
MY_FIRST_CONSTANT = 16
# Re-assignment will be allowed by the compiler & interpreter,
# but this is VERY strongly discouraged.
# Please don't do this, it could create problems in your program!
MY_FIRST_CONSTANT = "Some other value"
Python में काम की इकाइयाँ फंक्शन में समाहित होती हैं, जो खुद ऑब्जेक्ट होते हैं (यह turtles all the way down है)।
फंक्शन अकेले चलाए जा सकते हैं, दूसरे फंक्शन को आर्गुमेंट के तौर पर दिए जा सकते हैं, नेस्टेड किए जा सकते हैं, या किसी क्लास से बाँधे जा सकते हैं। जब फंक्शन किसी क्लास नाम से बँधे होते हैं, तो उन्हें मेथड कहा जाता है। एक-दूसरे से जुड़े फंक्शन और क्लास (उनके मेथड के साथ) एक ही फाइल या मॉड्यूल में एक साथ रखे जा सकते हैं, और दूसरे प्रोग्राम में उपयोग के लिए पूरे या कुछ हिस्से के तौर पर इम्पोर्ट किए जा सकते हैं।
def कीवर्ड एक फंक्शन की परिभाषा शुरू करता है।
हर फंक्शन में () ब्रैकेट के अंदर शून्य या उससे अधिक औपचारिक पैरामीटर हो सकते हैं, जिनके बाद एक : कोलन आता है।
फंक्शन की बॉडी के स्टेटमेंट def के बाद वाली लाइन से शुरू होते हैं और एक ब्लॉक में इंडेंट होने चाहिए:
# The body of a function is indented by 2 spaces & prints the sum of the numbers.
def add_two_numbers(number_one, number_two):
total = number_one + number_two
print(total)
>>> add_two_numbers(3, 4)
7
# Inconsistent indentation in your code blocks will raise an error.
>>> def add_three_numbers_misformatted(number_one, number_two, number_three):
... result = number_one + number_two + number_three # This was indented by 4 spaces.
... print(result) # <--This was only indented by 3 spaces.
...
...
File "<stdin>", line 3
print(result)
^
IndentationError: unindent does not match any outer indentation level
फंक्शन return कीवर्ड के ज़रिए स्पष्ट रूप से कोई वैल्यू या ऑब्जेक्ट लौटाते हैं:
# Function definition on first line, explicit return used on final line.
def add_two_numbers(number_one, number_two):
return number_one + number_two
# Calling the function in the Python shell returns the sum of the numbers.
>>> add_two_numbers(3, 4)
7
# Assigning the function call to a variable and printing
# the variable will also return the value.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
11
जिन फंक्शन के return के बाद कोई स्पष्ट एक्सप्रेशन नहीं होता, वे निहित रूप से None ऑब्जेक्ट लौटाते हैं।
None के विवरण आगे के किसी कॉन्सेप्ट में समझाए जाएँगे।
इस अभ्यास और इस व्याख्या के उद्देश्य से, None एक प्लेसहोल्डर है जो कुछ भी न होने, यानी null को दर्शाता है:
# This function will return `None`
def square_a_number(number):
square = number * number
return # <-- note that this return is not followed by an expression
# Calling the function in the Python shell appears
# to not return anything at all.
>>> square_a_number(2)
>>>
# Using print() with the function call shows that
# the function is actually returning the **None** object.
>>> print(square_a_number(2))
None
जो फंक्शन return छोड़ देते हैं, वे भी निहित रूप से None ऑब्जेक्ट लौटाते हैं।
इसका मतलब है कि अगर आप किसी फंक्शन में return का उपयोग नहीं करते, तो Python आपके लिए None ऑब्जेक्ट लौटा देगा।
# This function omits a return keyword altogether
def add_two_numbers(number_one, number_two):
result = number_one + number_two
>>> add_two_numbers(5, 7)
>>> print(add_two_numbers(5, 7))
None
# Assigning the function call to a variable and printing
# the variable will also show None.
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None
फंक्शन को उनके नाम के बाद () लगाकर कॉल या इन्वोक किया जाता है।
किसी क्लास या मॉड्यूल के अंदर बनाए गए फंक्शन को कॉल करने के लिए डॉट (.) नोटेशन का उपयोग किया जाता है।
>>> def raise_to_power(number, power):
... return number ** power
...
>>> raise_to_power(3,3) # Invoking the function with the arguments 3 and 3.
27
# A mis-match between the number of parameters and the number of arguments will raise an error.
>>> raise_to_power(4,)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: raise_to_power() missing 1 required positional argument: 'power'
# Calling methods or functions in classes and modules.
>>> start_text = "my silly sentence for examples."
>>> str.upper(start_text) # <--Calling the upper() method from the built-in str class on start_text.
'MY SILLY SENTENCE FOR EXAMPLES.'
# Because a string is an instance of the str class, methods can also be called on them "directly".
>>> start_text = "my silly sentence for examples."
>>> start_text.upper() # <--Calling the upper() method on start_text directly.
'MY SILLY SENTENCE FOR EXAMPLES.'
# Alternatively, we can skip the variable assignment (although this gets messy quick).
>>> "my silly sentence for examples.".upper()
'MY SILLY SENTENCE FOR EXAMPLES.'
# Importing the math module
>>> import math
>>> math.pow(2,4) # <--Calling the pow() function from the math module.
16.0
Python में कमेंट # से शुरू होते हैं, जो किसी स्ट्रिंग का हिस्सा नहीं होता, और लाइन खत्म होने पर समाप्त हो जाते हैं।
कई दूसरी प्रोग्रामिंग भाषाओं के उलट, Python मल्टी-लाइन कमेंट चिह्नों का समर्थन नहीं करता।
कमेंट ब्लॉक की हर लाइन # अक्षर से शुरू होनी चाहिए।
इंटरप्रेटर कमेंट को अनदेखा कर देता है:
# This is a single line comment.
x = "foo" # This is an in-line comment.
# This is a multi-line
# comment block over multiple lines --
# these should be used sparingly.
किसी फंक्शन बॉडी का पहला स्टेटमेंट वैकल्पिक रूप से एक डॉकस्ट्रिंग हो सकता है, जो फंक्शन या ऑब्जेक्ट के उद्देश्य को संक्षेप में बताता है। डॉकस्ट्रिंग तीन डबल कोट्स (""") का उपयोग करके घोषित किए जाते हैं, जो कोड ब्लॉक के समान स्तर पर इंडेंट होते हैं:
# An example from PEP257 of a multi-line docstring
# reformatted to use Google style non-type hinted docstrings.
# Some additional details can be found in the Sphinx documentation:
# https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#getting-started
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword Arguments:
real (float): The real part of the number (default 0.0)
imag (float): The imaginary part of the number (default 0.0)
"""
if imag == 0.0 and real == 0.0:
return complex_zero
डॉकस्ट्रिंग को स्वचालित दस्तावेज़ीकरण टूल पढ़ते हैं, और फंक्शन, मेथड या क्लास के नाम पर खास एट्रिब्यूट .__doc__ को कॉल करने पर ये लौटाए जाते हैं।
जहाँ भी दस्तावेज़ीकरण की ज़रूरत हो, वहाँ किसी भी आकार के प्रोग्राम के लिए इन्हें इस्तेमाल करने की सलाह दी जाती है। इनके नियम PEP257 में दिए गए हैं।
डॉकस्ट्रिंग हल्के यूनिट टेस्ट का काम भी कर सकते हैं, जिन्हें PyTest पढ़ और चला सकता है, या doctest मॉड्यूल इम्पोर्ट करके चलाया जा सकता है।
टेस्टिंग और doctest को आगे के किसी कॉन्सेप्ट में समझाया जाएगा।
# An example on a user-defined function using a Google style docstring.
>>> def raise_to_power(number, power):
"""Raise a number to an arbitrary power.
Parameters:
number (int): The base number.
power (int): The power to raise the base number to.
Returns:
int: The number raised to the specified power.
Takes a number and raises it to the specified power, returning the result.
"""
return number ** power
...
# Calling the .__doc__ attribute of the function and printing the result.
>>> print(raise_to_power.__doc__)
Raise a number to an arbitrary power.
Parameters:
number (int): The base number.
power (int): The power to raise the base number to.
Returns:
int: The number raised to the specified power.
Takes a number and raises it to the specified power, returning the result.
...
# Printing the __doc__ attribute of the built-in type: str.
>>> print(str.__doc__)
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to 'utf-8'.
errors defaults to 'strict'.