ट्रैक
/
Python
Python
/
अभ्यास
/
छोटी बहन की शब्दावली
छोटी बहन की शब्दावली

छोटी बहन की शब्दावली

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

परिचय

Python में str अपरिवर्तनीय अनुक्रम होता है, जो यूनिकोड कोड पॉइंट से बना होता है। इनमें अक्षर, विशेषक चिह्न, स्थिति बताने वाले अक्षर, संख्याएँ, मुद्रा चिह्न, इमोजी, विराम चिह्न, स्पेस और लाइन ब्रेक अक्षर, और भी बहुत कुछ शामिल हो सकता है। चूँकि यह अपरिवर्तनीय है, मेमोरी में str ऑब्जेक्ट की वैल्यू नहीं बदलती; जो मेथड स्ट्रिंग में बदलाव करते दिखते हैं, वे उस str ऑब्जेक्ट की एक नई कॉपी या नया इंस्टेंस लौटाते हैं।

str लिटरल को सिंगल ' या डबल " कोट्स के ज़रिए घोषित किया जा सकता है। ज़रूरत पड़ने पर एस्केप \ अक्षर भी इस्तेमाल किया जा सकता है।


>>> single_quoted = 'These allow "double quoting" without "escape" characters.'

>>> double_quoted = "These allow embedded 'single quoting', so you don't have to use an 'escape' character."

>>> escapes = 'If needed, a \'slash\' can be used as an escape character within a string when switching quote styles won\'t work.'

मल्टी-लाइन स्ट्रिंग को ''' या """ से घोषित किया जाता है।

>>> triple_quoted =  '''Three single quotes or "double quotes" in a row allow for multi-line string literals.
  Line break characters, tabs and other whitespace are fully supported.

  You\'ll most often encounter these as "doc strings" or "doc tests" written just below the first line of a function or class definition.
    They\'re often used with auto documentation ✍ tools.
    '''

स्ट्रिंग को + ऑपरेटर से जोड़ा जा सकता है। इस तरीके का इस्तेमाल कम ही करना चाहिए, क्योंकि यह बहुत कुशल नहीं है और इसे बनाए रखना भी आसान नहीं है।

language = "Ukrainian"
number = "nine"
word = "дев'ять"

sentence = word + " " + "means" + " " + number + " in " + language + "."

>>> print(sentence)
...
"дев'ять means nine in Ukrainian."

अगर अलग-अलग स्ट्रिंग के किसी list, tuple, set या किसी अन्य कलेक्शन को एक ही str में जोड़ना हो, तो <str>.join(<iterable>) बेहतर विकल्प है:

# str.join() makes a new string from the iterables elements.
>>> chickens = ["hen", "egg", "rooster"] # Lists are iterable.
>>> ' '.join(chickens)
'hen egg rooster'

# Any string can be used as the joining element.
>>> ' :: '.join(chickens)
'hen :: egg :: rooster'

>>> ' 🌿 '.join(chickens)
'hen 🌿 egg 🌿 rooster'


# Any iterable can be used as input.
>>> flowers = ("rose", "daisy", "carnation")  # Tuples are iterable.
>>> '*-*'.join(flowers)
'rose*-*daisy*-*carnation'

>>> flowers = {"rose", "daisy", "carnation"}  # Sets are iterable, but output order is not guaranteed.
>>> '*-*'.join(flowers)
'rose*-*carnation*-*daisy'

>>> phrase = "This is my string"  # Strings are iterable, but be careful!
>>> '..'.join(phrase)
'T..h..i..s.. ..i..s.. ..m..y.. ..s..t..r..i..n..g'


# Separators are inserted **between** elements, but can be any string (including spaces).
# This can be exploited for interesting effects.
>>> under_words = ['under', 'current', 'sea', 'pin', 'dog', 'lay']
>>> separator = ' ⤴️ under'
>>> separator.join(under_words)
'under ⤴️ undercurrent ⤴️ undersea ⤴️ underpin ⤴️ underdog ⤴️ underlay'

# The separator can be composed different ways, as long as the result is a string.
>>> upper_words = ['upper', 'crust', 'case', 'classmen', 'most', 'cut']
>>> separator = ' 🌟 ' + upper_words[0]
>>> separator.join(upper_words)
 'upper 🌟 uppercrust 🌟 uppercase 🌟 upperclassmen 🌟 uppermost 🌟 uppercut'

किसी str के भीतर के कोड पॉइंट्स को बाईं ओर से 0-based index संख्या से रेफर किया जा सकता है:

creative = '창의적인'

>>> creative[0]
'창'

>>> creative[2]
'적'

>>> creative[3]
'인'

दाईं ओर से भी इंडेक्स लगाया जा सकता है, और वह -1-based index से शुरू होता है:

creative = '창의적인'

>>> creative[-4]
'창'

>>> creative[-2]
'적'

>>> creative[-1]
'인'

Python में “character” या "rune" नाम का कोई अलग टाइप नहीं है, इसलिए स्ट्रिंग पर इंडेक्स लगाने से लंबाई 1 की नई str बनती है:


>>> website = "exercism"
>>> type(website[0])
<class 'str'>

>>> len(website[0])
1

>>> website[0] == website[0:1] == 'e'
True

सबस्ट्रिंग को स्लाइस नोटेशन के ज़रिए चुना जा सकता है, जिसमें <str>[<start>:stop:<step>] से एक नई स्ट्रिंग बनती है। परिणाम में stop इंडेक्स शामिल नहीं होता। अगर start नहीं दिया गया है, तो शुरुआती इंडेक्स 0 होगा। अगर stop नहीं दिया गया है, तो stop इंडेक्स स्ट्रिंग के अंत में होगा।

moon_and_stars = '🌟🌟🌙🌟🌟⭐'
sun_and_moon = '🌞🌙🌞🌙🌞🌙🌞🌙🌞'

>>> moon_and_stars[1:4]
'🌟🌙🌟'

>>> moon_and_stars[:3]
'🌟🌟🌙'

>>> moon_and_stars[3:]
'🌟🌟⭐'

>>> moon_and_stars[:-1]
'🌟🌟🌙🌟🌟'

>>> moon_and_stars[:-3]
'🌟🌟🌙'

>>> sun_and_moon[::2]
'🌞🌞🌞🌞🌞'

>>> sun_and_moon[:-2:2]
'🌞🌞🌞🌞'

>>> sun_and_moon[1:-1:2]
'🌙🌙🌙🌙'

स्ट्रिंग को <str>.split(<separator>) के ज़रिए छोटी स्ट्रिंग में भी तोड़ा जा सकता है, जो सबस्ट्रिंग की एक list लौटाता है। ज़रूरत पड़ने पर उस ऐरे पर आगे इंडेक्स लगाया जा सकता है या उसे फिर से स्प्लिट किया जा सकता है। <str>.split() को बिना किसी आर्गुमेंट के इस्तेमाल करने पर स्ट्रिंग व्हाइटस्पेस पर टूट जाती है।

>>> cat_ipsum = "Destroy house in 5 seconds mock the hooman."
>>> cat_ipsum.split()
...
['Destroy', 'house', 'in', '5', 'seconds', 'mock', 'the', 'hooman.']


>>> cat_ipsum.split()[-1]
'hooman.'


>>> cat_words = "feline, four-footed, ferocious, furry"
>>> cat_words.split(', ')
...
['feline', 'four-footed', 'ferocious', 'furry']

<str>.split() के सेपरेटर एक से ज़्यादा अक्षर के भी हो सकते हैं। स्प्लिट का मिलान पूरी स्ट्रिंग से किया जाता है।


>>> colors = """red,
orange,
green,
purple,
yellow"""

>>> colors.split(',\n')
['red', 'orange', 'green', 'purple', 'yellow']

स्ट्रिंग सभी common sequence operations का समर्थन करती है। हर कोड पॉइंट पर for item in <str> लिखकर लूप में एक-एक करके चला जा सकता है। इंडेक्स के साथ आइटम पर for index, item in enumerate(<str>) लिखकर भी लूप में एक-एक करके चला जा सकता है।


>>> exercise = 'လေ့ကျင့်'

# Note that there are more code points than perceived glyphs or characters
>>> for code_point in exercise:
...    print(code_point)
...
လ
ေ
့
က
ျ
င
်
့

# Using enumerate will give both the value and index position of each element.
>>> for index, code_point in enumerate(exercise):
...    print(index, ": ", code_point)
...
0 :  လ
1 :  ေ
2 :  ့
3 :  က
4 :  ျ
5 :  င
6 :  ်
7 :  ့

निर्देश

आप अपनी छोटी बहन को उसका अंग्रेज़ी शब्दावली का होमवर्क करने में मदद कर रहे हैं, जो उसे बहुत उबाऊ लगता है। उसकी कक्षा शब्दों के आगे उपसर्ग और अंत में प्रत्यय जोड़कर नए शब्द बनाना सीख रही है। शब्दों का एक समूह देखकर शिक्षक ऐसे शब्द चाहते हैं जो सही ढंग से बदले गए हों और जिनकी वर्तनी भी सही हो, यानी ऐसे शब्द जिनके शुरू में उपसर्ग या अंत में प्रत्यय जोड़ा गया हो।

इस होमवर्क में चार गतिविधियाँ हैं, और हर गतिविधि में काम करने के लिए कुछ टेक्स्ट या शब्द दिए गए हैं।

1. किसी शब्द में उपसर्ग जोड़िए

अंग्रेज़ी के सबसे आम उपसर्गों में से एक है un, जिसका अर्थ है "नहीं"। इस गतिविधि में आपकी बहन को शब्दों के आगे un जोड़कर नकारात्मक यानी "नहीं" वाले शब्द बनाने हैं।

add_prefix_un(<word>) फंक्शन बनाइए जो word को पैरामीटर के रूप में लेता है और un उपसर्ग लगा हुआ एक नया शब्द लौटाता है:

>>> add_prefix_un("happy")
'unhappy'

>>> add_prefix_un("manageable")
'unmanageable'

2. शब्द-समूहों में उपसर्ग जोड़िए

आपकी बहन की कक्षा चार और आम उपसर्ग सीख रही है: en (यानी 'अंदर रखना' या 'ढक देना'), pre (यानी 'पहले' या 'आगे'), auto (यानी 'स्वयं' या 'समान'), और inter (यानी 'बीच में' या 'के बीच').

इस अभ्यास में कक्षा इन उपसर्गों की मदद से शब्दावली शब्दों के समूह बना रही है, ताकि उन्हें एक साथ पढ़ा जा सके। हर उपसर्ग एक सूची के साथ दिया गया है, जिसमें उसके साथ आमतौर पर आने वाले शब्द हैं। विद्यार्थियों को उपसर्ग लगाना है और ऐसी स्ट्रिंग बनानी है जिसमें हर शब्द पर वह उपसर्ग लगा हुआ दिखे।

make_word_groups(<vocab_words>) फंक्शन बनाइए जो इस रूप का vocab_words पैरामीटर लेता है: [<prefix>, <word_1>, <word_2> .... <word_n>], और ऐसी स्ट्रिंग लौटाता है जिसमें हर शब्द पर उपसर्ग लगा हो और जो इस तरह दिखे: '<prefix> :: <prefix><word_1> :: <prefix><word_2> :: <prefix><word_n>'.

यहाँ इनपुट को संभालने के लिए for या while लूप बनाने की ज़रूरत नहीं है। ध्यान से सोचिए कि इसके बजाय आप कौन-कौन से स्ट्रिंग मेथड (और डिलिमिटर) इस्तेमाल कर सकते हैं।

>>> make_word_groups(['en', 'close', 'joy', 'lighten'])
'en :: enclose :: enjoy :: enlighten'

>>> make_word_groups(['pre', 'serve', 'dispose', 'position'])
'pre :: preserve :: predispose :: preposition'

>> make_word_groups(['auto', 'didactic', 'graph', 'mate'])
'auto :: autodidactic :: autograph :: automate'

>>> make_word_groups(['inter', 'twine', 'connected', 'dependent'])
'inter :: intertwine :: interconnected :: interdependent'

3. किसी शब्द से प्रत्यय हटाइए

ness एक आम प्रत्यय है जिसका अर्थ है 'होने की अवस्था'। इस गतिविधि में आपकी बहन को ness प्रत्यय हटाकर मूल शब्द ढूँढना है। लेकिन वर्तनी के कुछ झंझट भरे नियम भी हैं: अगर मूल शब्द के अंत में कोई व्यंजन और उसके बाद 'y' आता था, तो उस 'y' को 'i' में बदल दिया जाता था। ऐसे मूल शब्दों से 'ness' हटाते समय 'y' को वापस लाना पड़ता है। जैसे happiness --> happi --> happy.

remove_suffix_ness(<word>) फंक्शन बनाइए जो एक word लेता है और ness प्रत्यय हटाकर मूल शब्द लौटाता है।

>>> remove_suffix_ness("heaviness")
'heavy'

>>> remove_suffix_ness("sadness")
'sad'

4. किसी शब्द को निकालिए और उसका रूप बदलिए

प्रत्यय अक्सर किसी शब्द का शब्द-भेद बदलने के लिए इस्तेमाल होते हैं। अंग्रेज़ी में एक आम चलन है "verbing" या "verbifying", जिसमें en प्रत्यय जोड़कर कोई विशेषण क्रिया बन जाता है।

इस काम में आपकी बहन वाक्य से एक विशेषण निकालकर उसे क्रिया में बदलकर शब्दों की "verbing" का अभ्यास करेगी। अच्छी बात यह है कि यहाँ जिन शब्दों को बदलना है वे सब नियमित हैं, उनमें प्रत्यय जोड़ने के लिए वर्तनी बदलने की ज़रूरत नहीं पड़ती।

adjective_to_verb(<sentence>, <index>) फंक्शन बनाइए जो दो पैरामीटर लेता है। पहला एक sentence है, जिसमें वह शब्दावली शब्द इस्तेमाल हुआ हो, और दूसरा उस शब्द का index है, जब उस वाक्य को अलग-अलग हिस्सों में तोड़ा जाए। यह फंक्शन निकाले गए विशेषण को क्रिया के रूप में लौटाना चाहिए।

>>> adjective_to_verb('I need to make that bright.', -1 )
'brighten'

>>> adjective_to_verb('It got dark as the sun set.', 2)
'darken'
GitHub के ज़रिए संपादित करें यह लिंक एक नई विंडो या टैब में खुलता है
Python Exercism

छोटी बहन की शब्दावली शुरू करने के लिए तैयार हैं?

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