Welcome to CCP
Hello you !
Lets get started,
So we talked about tuples in last part,
Now we will be looking forward to Sets and take a look at this:
I think all should be clear by having a look at this image,
sets are like the sets we in mathematics, a collection of items and they don't have repeated elements, here also the same property applies,
and the most important thing is that they don't support indexing,
but they are iterable like this:
Sounds interesting ?
Yeah It is !
Lets do an exercise:
Exercise Problem:
You have a sentence/paragraph, you have to count all the words in it and organize them in a dictionary
Exercise Solution: (Do the exercise first, if you get a problem, you can refer here)
I will use Visual Studio for this:
paragraph = """
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
def remove_newline_and_strip(para):
para = para.strip().replace("\n", " ").replace(".", "").replace(",", "").replace("-", "")
return para # returns a clean_para that means it has no punctuation marks in it like . or ,
def count_words(clean_para):
words = clean_para.split() # split() function splits a string into a list of words when seperated by spaces
word_count = {} # contains words as keys and number of occureneces of those words as values
for word in words:
if not word in word_count:
word_count[word] = 0
word_count[word] += 1
print(word_count)
def main():
clean_para = remove_newline_and_strip(paragraph)
count_words(clean_para)
main()
"""
Outputs:
{'The': 1, 'Zen': 1, 'of': 3, 'Python': 1, 'by': 1, 'Tim': 1, 'Peters': 1, 'Beautiful': 1, 'is': 10, 'better': 8, 'than': 8, 'ugly': 1, 'Explicit': 1, 'implicit': 1, 'Simple': 1, 'complex': 1, 'Complex': 1, 'complicated': 1,
'Flat': 1, 'nested': 1, 'Sparse': 1, 'dense': 1, 'Readability': 1, 'counts': 1, 'Special': 1, 'cases': 1, "aren't": 1, 'special': 1, 'enough': 1, 'to': 5, 'break': 1, 'the': 5, 'rules': 1, 'Although': 3, 'practicality': 1,
'beats': 1, 'purity': 1, 'Errors': 1, 'should': 2, 'never': 3, 'pass': 1, 'silently': 1, 'Unless': 1, 'explicitly': 1, 'silenced': 1, 'In': 1, 'face': 1, 'ambiguity': 1, 'refuse': 1, 'temptation': 1, 'guess': 1, 'There': 1,
'be': 3, 'one': 3, 'and': 1, 'preferably': 1, 'only': 1, 'obvious': 2, 'way': 2, 'do': 2, 'it': 2, 'that': 1, 'may': 2, 'not': 1, 'at': 1, 'first': 1, 'unless': 1, "you're": 1, 'Dutch': 1, 'Now': 1, 'often': 1, '*right*': 1,
'now': 1, 'If': 2, 'implementation': 2, 'hard': 1, 'explain': 2, "it's": 1, 'a': 2, 'bad': 1, 'idea': 3, 'easy': 1, 'good': 1, 'Namespaces': 1, 'are': 1, 'honking': 1, 'great': 1, "let's": 1, 'more': 1, 'those!': 1}
"""
have fun coding,
.
.
.
.
.
.
.
.
.
.
.
AND DO NOT EVER TRY TO JUST CHEAT !
SEE YOU LATER !
Comments
Post a Comment