This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| Bevrage = namedtuple('Bevrage',['name','color','type']) | |
| # defining a class in which named tuple acts as a constructor or #__init__() method | |
| class BeverageContainer(Bevrage): | |
| def container_type(self): | |
| if self.type == 'hot': | |
| return "Use a Mug" | |
| else: | |
| return "A mug is not required" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # consider a shopping list which contains various objects with quantity and cost and this items are stored in a list as tuples with their description and we want to find the total cost with discount | |
| from collections import namedtuple | |
| grocery = namedtuple('grocery',['item_name','item_weight','item_cost', | |
| 'item_quantity','item_dicsount']) | |
| shopping_list_tuples = [('apples',2.3,128,7,13.6),('chicken',1,228,2,110), | |
| ('milk',0.6,18,2,0),('lemons',0.3,45,20,50)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from urllib.request import urlopen | |
| def read_data(name: str): | |
| if name.startswith(('http:','https:','ftp:')): | |
| return urlopen(name).read() | |
| else: | |
| with open(name) is f: | |
| return f.read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| animal = namedtuple('animal',['species_name','legs_no','color']) | |
| dog = animal('canine',4,'white') | |
| dog.legs_no = 78 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| animal = namedtuple('animal',['species_name','legs_no','color']) | |
| dog = animal('canine',4,'white') | |
| dog = dog._replace(legs_no = 6) | |
| print(dog.legs_no) | |
| # Output: 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| text = "HELLO WORLD, hello world, Hello World". | |
| re.sub("hello",'good morning',text,flags=re.IGNORECASE) | |
| # Output: 'good morning WORLD, good morning world, good morning World' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def matchcase(word): | |
| def replace(m): | |
| text = m.group() | |
| if text.isupper(): | |
| return word.upper() | |
| elif text.islower(): | |
| return word.lower() | |
| elif text[0].isupper(): | |
| return word.capitalize() | |
| else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| s1 = 'Jalape\u00f1o' # composed form | |
| s2 = 'Jalapen\u0303o' # decomposed form | |
| print(s1,s2) | |
| # Output: Jalapeño Jalapeño | |
| print(s1 == s2) | |
| # Output: False | |
| from unicodedata import normalize | |
| v1 = normalize('NFC',s1) | |
| v2 = normalize('NFC',s2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unicodedata | |
| s3 = "tête-à-tête" | |
| s4 = unicodedata.normalize('NFD',s3) | |
| print(len(s3) == len(s4)) | |
| # Output: False | |
| [val for val in s4 if unicodedata.combining(val)] | |
| # Output: ['̂', '̀', '̂'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| text_to_translate = "īīŏŏūāāīīĕĕĕŏōūў ūūĭĭŏŏŭŭēīūūўĕŏūūȳēēīū ēēīīŏŏōōōō" | |
| import unicodedata | |
| import sys | |
| dictonary_of_accents = dict.fromkeys(val for val in range(sys.maxunicode) if | |
| unicodedata.combining(chr(val))) | |
| # here we are using dict.fromkeys(keys,value) method of the dict class which returns a dictonary. | |
| #with specified keys and values in this case the values is not specified which means that the value of the keys is none | |
| normalized_text = unicodedata.normalize('NFD',text_to_translate) |