Default और keyword arguments

इंटरमीडिएट Python फॉर डेवलपर्स

Jasmin Ludolf

Senior Data Science Content Developer

Average

# Create a custom function
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)

    # Round the results
    rounded_average = round(average_value, 2)

    # Return rounded_average as an output
    return rounded_average
  • values = आर्ग्युमेंट
इंटरमीडिएट Python फॉर डेवलपर्स

Arguments

  • फंक्शन या मेथड को दिए गए मान
    • Positional
    • Keyword
इंटरमीडिएट Python फॉर डेवलपर्स

Positional arguments

  • आर्ग्युमेंट क्रम में दें, कॉमा से अलग करें
# Round pi to 2 digits
print(round(3.1415926535, 2))
3.14
इंटरमीडिएट Python फॉर डेवलपर्स

Keyword arguments

  • keywords को मान असाइन कर के आर्ग्युमेंट दें

  • व्याख्या और ट्रैकिंग में मददगार

# Round pi to 2 digits 
print(round(number=3.1415926535
इंटरमीडिएट Python फॉर डेवलपर्स

Keyword arguments

  • keywords को मान असाइन कर के आर्ग्युमेंट दें

  • व्याख्या और ट्रैकिंग में मददगार

# Round pi to 2 digits 
print(round(number=3.1415926535, ndigits=2))
3.14
इंटरमीडिएट Python फॉर डेवलपर्स

Keyword arguments पहचानना

# Get more information about the help function
print(help(round))
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
इंटरमीडिएट Python फॉर डेवलपर्स

Keyword arguments

Help on built-in function round in module builtins:

round(number, ndigits=None)
  Round a number to a given precision in decimal digits.

  The return value is an integer if ndigits is omitted or None. Otherwise 
  the return value has the same type as the number. ndigits may be negative.

$$

  • पहला आर्ग्युमेंट: number
  • दूसरा आर्ग्युमेंट: ndigits
इंटरमीडिएट Python फॉर डेवलपर्स

Default arguments

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
  • None = कोई मान नहीं / खाली

  • Default argument: किसी argument के लिए default मान तय करने का तरीका

  • हम None को 2 से ओवरराइट करते हैं

  • अक्सर उपयोग होने वाला मान - इसे default argument से सेट करें

इंटरमीडिएट Python फॉर डेवलपर्स

एक आर्ग्युमेंट जोड़ना

# Create a custom function
def average(values):
    average_value = sum(values) / len(values)
    rounded_average = round(average_value, 2)
    return rounded_average
इंटरमीडिएट Python फॉर डेवलपर्स

एक आर्ग्युमेंट जोड़ना

# Create a custom function
def average(values, rounded=False):


इंटरमीडिएट Python फॉर डेवलपर्स

एक आर्ग्युमेंट जोड़ना

# Create a custom function
def average(values, rounded=False):

# Round average to two decimal places if rounded is True if rounded == True:
average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
इंटरमीडिएट Python फॉर डेवलपर्स

एक आर्ग्युमेंट जोड़ना

# Create a custom function
def average(values, rounded=False):

# Round average to two decimal places if rounded is True if rounded == True: average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
# Otherwise, don't round else: average_value = sum(values) / len(values) return average_value
इंटरमीडिएट Python फॉर डेवलपर्स

संशोधित average() फंक्शन का उपयोग

# List of preparation times (minutes)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]
इंटरमीडिएट Python फॉर डेवलपर्स

संशोधित average() फंक्शन का उपयोग

# Get the average without rounding
print(average(preparation_times, False))
28.4585714
# Get the average without rounding
print(average(preparation_times))
28.4585714
इंटरमीडिएट Python फॉर डेवलपर्स

संशोधित average() फंक्शन का उपयोग

# Get the rounded average
print(average(values=preparation_times, rounded=True))
28.46
इंटरमीडिएट Python फॉर डेवलपर्स

अभ्यास करते हैं!

इंटरमीडिएट Python फॉर डेवलपर्स

Preparing Video For Download...