ट्रैक
/
Python
Python
/
अभ्यास
/
गाइडो की शानदार लज़ान्या
गाइडो की शानदार लज़ान्या

गाइडो की शानदार लज़ान्या

सीखने का अभ्यास

परिचय

Python एक डायनामिक और सख्त टाइप वाली प्रोग्रामिंग भाषा है। यह टाइप हिंट के ज़रिए डक टाइपिंग और ग्रेजुअल टाइपिंग, दोनों का इस्तेमाल करती है।

Python कई अलग-अलग प्रोग्रामिंग शैलियों का समर्थन करती है, लेकिन अंदर से Python में सब कुछ एक ऑब्जेक्ट है। इसमें संख्याएँ, स्ट्रिंग, ऐरे, और फंक्शन तक शामिल हैं।

जैसे-जैसे हम ट्रैक में आगे बढ़ेंगे, इन सबका मतलब और गहराई से समझेंगे।

यह पहला अभ्यास Python भाषा की 4 प्रमुख विशेषताओं से परिचित कराता है:

  1. नाम असाइनमेंट (वेरिएबल और कॉन्स्टेंट),
  2. फंक्शन (def कीवर्ड और return कीवर्ड),
  3. कमेंट, और
  4. डॉकस्ट्रिंग।

Note

सामान्य तौर पर, Python ट्रैक की सामग्री, टेस्ट और एनालाइज़र टूलिंग Python कोड की शैली के लिए PEP 8 और PEP 257 में दिए गए स्टाइल कन्वेंशन का पालन करती है, साथ ही एक अतिरिक्त (ज़ोरदार) सुझाव यह है कि एक अक्षर वाले वेरिएबल नाम या "_" नाम का वेरिएबल न हो।

Python ट्रैक पर, वेरिएबल हमेशा snake_case में लिखे जाते हैं, और कॉन्स्टेंट SCREAMING_SNAKE_CASE में।


नाम असाइनमेंट (वेरिएबल और कॉन्स्टेंट)

प्रोग्रामर असाइनमेंट = ऑपरेटर की मदद से नाम (जिन्हें वेरिएबल भी कहा जाता है) को किसी भी टाइप के ऑब्जेक्ट से बाँध सकते हैं: <name> = <value>. एक नाम को उसके जीवनकाल में अलग-अलग वैल्यू (अलग-अलग ऑब्जेक्ट टाइप) असाइन किया जा सकता है (या दोबारा बाँधा जा सकता है)।

>>> 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".

कॉन्स्टेंट

कॉन्स्टेंट ऐसे नाम होते हैं जिन्हें किसी प्रोग्राम में सिर्फ एक बार असाइन करना होता है। इन्हें मॉड्यूल (फाइल) लेवल पर परिभाषित करना चाहिए, और आम तौर पर ये प्रोग्राम के सभी फंक्शन और क्लास को दिखाई देते हैं। SCREAMING_SNAKE_CASE इस्तेमाल करने से यह संकेत मिलता है कि नाम को दोबारा असाइन नहीं करना चाहिए, और न ही उसकी वैल्यू बदलनी चाहिए।

फंक्शन

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 it 
# 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 mismatch 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.'


# Importing the math module
>>> import math

>>> math.pow(2,4)  # <--Calling the pow() function from the math module.
16.0

कमेंट

Python में कमेंट # से शुरू होते हैं, जो किसी स्ट्रिंग का हिस्सा नहीं होता, और लाइन खत्म होने पर समाप्त हो जाते हैं। कई दूसरी प्रोग्रामिंग भाषाओं के उलट, Python मल्टी-लाइन कमेंट मार्क सपोर्ट नहीं करता। कमेंट ब्लॉक की हर लाइन # अक्षर से शुरू होनी चाहिए।

डॉकस्ट्रिंग

किसी फंक्शन बॉडी का पहला स्टेटमेंट चाहें तो एक डॉकस्ट्रिंग हो सकता है, जो फंक्शन या ऑब्जेक्ट के उद्देश्य को संक्षेप में बताता है। डॉकस्ट्रिंग ट्रिपल डबल कोट्स (""") से घोषित की जाती हैं और कोड ब्लॉक के समान लेवल पर इंडेंट होती हैं:


# 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

डॉकस्ट्रिंग को Sphinx जैसे ऑटोमेटेड डॉक्युमेंटेशन टूल पढ़ते हैं, और फंक्शन, मेथड या क्लास के नाम पर खास एट्रिब्यूट .__doc__ कॉल करके इन्हें पाया जा सकता है। डॉकस्ट्रिंग के सामान्य कन्वेंशन PEP257 में दिए गए हैं, लेकिन सटीक फॉर्मैट प्रोजेक्ट और टीम के हिसाब से बदलते हैं। Exercism के कॉन्सेप्ट अभ्यास बिना टाइप हिंट वाले कोड के लिए Google स्टाइल अपनाने की कोशिश करते हैं।

डॉकस्ट्रिंग हल्के यूनिट टेस्ट की तरह भी काम कर सकती हैं, जिन्हें आगे के किसी अभ्यास में समझाया जाएगा।

# 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.

निर्देश

आप अपनी पसंदीदा कुकबुक से एक शानदार लसानिया बनाने में मदद के लिए कुछ कोड लिखेंगे।

आपके पास पाँच काम हैं, और सभी आपकी रेसिपी बनाने से जुड़े हैं।


Note

स्टब फाइल में हमने आपके लिए पहले फंक्शन की परिभाषा लिखनी शुरू कर दी है, लेकिन बाकी फंक्शन की परिभाषाएँ आपको खुद लिखनी होंगी। इसके अलावा, सभी कॉन्स्टेंट भी आपको खुद बनाने होंगे। स्टब फाइल में लिखी #TODO कमेंट लाइनों को ध्यान से पढ़िए। किसी काम को पूरा करने के बाद उस TODO कमेंट को हटा दीजिए।


1. अपेक्षित बेक समय को मिनटों में एक कॉन्स्टेंट के रूप में बनाइए

EXPECTED_BAKE_TIME कॉन्स्टेंट बनाइए, जो बताता है कि लसानिया को ओवन में कितने मिनट बेक होना चाहिए। आपकी कुकबुक के अनुसार, लसानिया को ओवन में 40 मिनट तक रहना चाहिए:

>>> print(EXPECTED_BAKE_TIME)
40

2. बेक होने का बाकी समय मिनटों में निकालिए

bake_time_remaining() फंक्शन को पूरा कीजिए। यह फंक्शन आर्गुमेंट के रूप में वे असली मिनट लेता है जितनी देर लसानिया ओवन में रही है, और EXPECTED_BAKE_TIME कॉन्स्टेंट के आधार पर लौटाता है कि लसानिया को अभी कितने मिनट और बेक होना है।

>>> bake_time_remaining(30)
10

3. तैयारी का समय मिनटों में निकालिए

preparation_time_in_minutes() फंक्शन बनाइए। यह आर्गुमेंट के रूप में वह number_of_layers लेता है जितनी परतें आप लसानिया में जोड़ना चाहते हैं, और लौटाता है कि उन्हें बनाने में आपको कितने मिनट लगेंगे। मान लीजिए कि हर परत को तैयार करने में 2 मिनट लगते हैं।

>>> def preparation_time_in_minutes(number_of_layers):
        ...
        ...
        
>>> preparation_time_in_minutes(2)
4

4. कुल बीता समय (तैयारी + बेकिंग) मिनटों में निकालिए

elapsed_time_in_minutes() फंक्शन बनाइए, जो आर्गुमेंट के रूप में दो पैरामीटर लेता है:

  • number_of_layers (लसानिया में जोड़ी गई परतों की संख्या)
  • elapsed_bake_time (वे मिनट जितनी देर लसानिया पहले ही ओवन में बेक हो चुकी है)।

यह फंक्शन लौटाता है कि आपने रसोई में खाना बनाने में कुल कितने मिनट बिताए: परतें लगाने में लगा आपका समय, और वह समय जो लसानिया ने ओवन में बेक होने में बिताया।

>>> def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
        ...
        ...
        
>>> elapsed_time_in_minutes(3, 20)
26

5. रेसिपी को नोट्स के साथ अपडेट कीजिए

रेसिपी को दोबारा देखिए और फंक्शन डॉकस्ट्रिंग के रूप में "नोट्स" जोड़िए।

def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
    """Calculate the elapsed cooking time.
    
    Parameters:
        number_of_layers (int): The number of layers in the lasagna.
        elapsed_bake_time (int): Time the lasagna has been baking in the oven.
    
    Returns:
        int: The total time elapsed (in minutes) preparing and baking.

    This function takes two integers representing the number of lasagna 
    layers and the time already spent baking the lasagna. It calculates 
    the total elapsed minutes spent cooking (preparing + baking).
    
    """
GitHub के ज़रिए संपादित करें यह लिंक एक नई विंडो या टैब में खुलता है
Python Exercism

गाइडो की शानदार लज़ान्या शुरू करने के लिए तैयार हैं?

Exercism पर साइन अप कीजिए और Python को 17 कॉन्सेप्ट146 अभ्यास तथा असली इंसानों से मिलने वाली मेंटरिंग के साथ सीखिए और उसमें महारत हासिल कीजिए, वह भी बिल्कुल मुफ्त।