Methods और functions

Finance के लिए Python परिचय

Adina Howe

Professor

Methods बनाम Functions

Methods
  • सभी methods, functions ही होते हैं
  • List methods, Python के built-in functions का subset हैं
Functions
  • सभी functions, methods नहीं होते
  • किसी object पर लागू होते हैं
    • prices.sort()
  • किसी object का input जरूरी होता है
    • type(prices)
Finance के लिए Python परिचय

List Methods - sort

  • Lists में कई built-in methods होते हैं जो डेटा निकालने और बदलने में मदद करते हैं
  • Methods को list.method() की तरह एक्सेस करें

list.sort() list के elements को ascending order में sort करता है

prices = [238.11, 237.81, 238.91]

prices.sort()
print(prices)
[237.81, 238.11, 238.91]
Finance के लिए Python परिचय

append और extend से list में जोड़ना

list.append() list में एक element जोड़ता है

months = ['January', 'February', 'March']

months.append('April')
print(months)
['January', 'February', 'March', 'April']

list.extend() list में हर element जोड़ता है

months.extend(['May', 'June', 'July'])

print(months)
['January', 'February', 'March', 'April', 'May', 'June', 'July']
Finance के लिए Python परिचय

उपयोगी list methods - index

list.index(x) वह सबसे छोटा index देता है जहाँ element x आता है

months = ['January', 'February', 'March']
prices = [238.11, 237.81, 238.91]
months.index('February')
1
print(prices[1])
237.81
Finance के लिए Python परिचय

और functions ...

  • min(list): सबसे छोटा element देता है

  • max(list): सबसे बड़ा element देता है

Finance के लिए Python परिचय

सबसे कम CPI वाला महीना ढूँढ़ें

months = ['January', 'February', 'March']
prices = [238.11, 237.81, 238.91]
# Identify min price
min_price = min(prices)

# Identify min price index min_index = prices.index(min_price)
# Identify the month with min price min_month = months[min_index] print(min_month)
February
Finance के लिए Python परिचय

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

Finance के लिए Python परिचय

Preparing Video For Download...