🚧Summaries For Lessons 24+ Are Still Work In Progress!

“Whoever is patient has great understanding…" Proverbs 14:29

Exercise: Most Frequent Letter

Description here...

Exercise: Most Frequent Letter

Description here...

Exercise: Most Frequent Letter

Description here...

- Practice Creating Functions
- Practice using Built-In Methods and Functions

The Most Frequent Letters

Practice time!

This time we'll focus on creating custom functions and using a few built-in methods and functions.

The Goal:
🔹Take a list of words
🔹Create a loop for words
🔹Calculate how many times each letter appears
🔹What is the most frequent letter(s) in the word

As always, pause here and try it on your own first. Then come back and let's do it together.

Ready? Set. Code🚀

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

💻Setup Your Script

Let's brainstorm and create a quick layout for the script. Feel free to comment additional logical steps if you want to.

🔹Define a list of words
🔹Create blank function
🔹Create a loop for words
🔹Execute function on each

#📦 Variables
#----------------------------------------------------------------------------------------------------
random_words = [
    "Lighthouse", "Cat", "Umbrella", "Giraffe"
    "Success", "Volcano", "Balloon",
    "Butterfly", "Mississippi", "Strawberry", "Tnt"
]

#⚙️ Functions
#----------------------------------------------------------------------------------------------------
def most_frequent_letters(word):
    pass # Placeholder

    # Count Letters

    # Find Max Count Letters

    # Report Values

#🎯 Main
#----------------------------------------------------------------------------------------------------
for word in random_words:
    results = most_frequent_letters(word)

#📦 Variables
#----------------------------------------------------------------------------------------------------
random_words = [
    "Lighthouse", "Cat", "Umbrella", "Giraffe"
    "Success", "Volcano", "Balloon",
    "Butterfly", "Mississippi", "Strawberry", "Tnt"
]

#⚙️ Functions
#----------------------------------------------------------------------------------------------------
def most_frequent_letters(word):
    pass # Placeholder

    # Count Letters

    # Find Max Count Letters

    # Report Values

#🎯 Main
#----------------------------------------------------------------------------------------------------
for word in random_words:
    results = most_frequent_letters(word)

#📦 Variables
#----------------------------------------------------------------------------------------------------
random_words = [
    "Lighthouse", "Cat", "Umbrella", "Giraffe"
    "Success", "Volcano", "Balloon",
    "Butterfly", "Mississippi", "Strawberry", "Tnt"
]

#⚙️ Functions
#----------------------------------------------------------------------------------------------------
def most_frequent_letters(word):
    pass # Placeholder

    # Count Letters

    # Find Max Count Letters

    # Report Values

#🎯 Main
#----------------------------------------------------------------------------------------------------
for word in random_words:
    results = most_frequent_letters(word)

For now if you execute nothing is going to happen because our function only has a pass keyword, which means DO NOTHING. It's used as a placeholder.

Now let's add some logic.

Count Letters

Let's start working on a function.

I'm going to add some print statements and then we'll focus on the first step - count letters

def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')

    # Count Letters
    for letter in word:
        count = word.count(letter)
        print(letter, count)
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')

    # Count Letters
    for letter in word:
        count = word.count(letter)
        print(letter, count)
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')

    # Count Letters
    for letter in word:
        count = word.count(letter)
        print(letter, count)

Now if you execute, you'll be able to see how many times each letter is used.

💡 Notice that Uppercase and Lowercase letters aren't counted as the same. So, we need to convert all letters into lower() or upper(). We can add this line on the top of function:

word = word.lower()          #Convert all letters into lower-case
word = word.lower()          #Convert all letters into lower-case
word = word.lower()          #Convert all letters into lower-case

💡 Also you can notice that letters are repeating, so we might need to store information to avoid duplicate print statements.

def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')

    word       = word.lower() # Convert to lowercase
    dict_count = {}           # Empty Dictionary

    # Count Letters
    for letter in word:
        if letter not in dict_count: # Avoid Duplicates
            count = word.count(letter)
            dict_count[letter] = count

    # Display Results 
    for letter, count in dict_count.items():
        print(f'{letter} used {count} times.')
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')

    word       = word.lower() # Convert to lowercase
    dict_count = {}           # Empty Dictionary

    # Count Letters
    for letter in word:
        if letter not in dict_count: # Avoid Duplicates
            count = word.count(letter)
            dict_count[letter] = count

    # Display Results 
    for letter, count in dict_count.items():
        print(f'{letter} used {count} times.')
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')

    word       = word.lower() # Convert to lowercase
    dict_count = {}           # Empty Dictionary

    # Count Letters
    for letter in word:
        if letter not in dict_count: # Avoid Duplicates
            count = word.count(letter)
            dict_count[letter] = count

    # Display Results 
    for letter, count in dict_count.items():
        print(f'{letter} used {count} times.')

So we've made a few changes:

🔹Made all letters lowercase
🔹Create dict container
🔹Check if letter is already in a dict to avoid duplicates
🔹Add letter to dict with count value
🔹Print Dict

Here's how it looks now:

Find Max Count

Now to find max value we can get a list of all values() and put it inside max() function.

# Find Max Count Letters
max_count = max(dict_count.values()) # Find max value number
# Find Max Count Letters
max_count = max(dict_count.values()) # Find max value number
# Find Max Count Letters
max_count = max(dict_count.values()) # Find max value number

Now, let's iterate over characters in the dict and find all letters that have the most count. And keep in mind that multiple letters can have the same max value, so let's group them in a list.

# Find Max Count Letters
    max_count = max(dict_count.values())
    max_letters = []
    for k,v in dict_count.items():
        if v == max_count:
            max_letters.append(k)

    # Report Results
    print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
# Find Max Count Letters
    max_count = max(dict_count.values())
    max_letters = []
    for k,v in dict_count.items():
        if v == max_count:
            max_letters.append(k)

    # Report Results
    print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
# Find Max Count Letters
    max_count = max(dict_count.values())
    max_letters = []
    for k,v in dict_count.items():
        if v == max_count:
            max_letters.append(k)

    # Report Results
    print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')

And here's the final result:

Final Touches

Lastly, we can fix a few minor details in the code.

Firstly, let's return some values from our function in case we'd want to reuse them. Just scroll all the way down of the function and write

return max_letters, max_count
return max_letters, max_count
return max_letters, max_count

💡PS. Notice we're returning 2 values and they are going to be combined into a tuple.

So here's how we'd use these returning values in the main section:

for word in random_words:
    results = most_frequent_letters(word)
    letters = results[0]
    count   = results[1]
    print(letters, count)
for word in random_words:
    results = most_frequent_letters(word)
    letters = results[0]
    count   = results[1]
    print(letters, count)
for word in random_words:
    results = most_frequent_letters(word)
    letters = results[0]
    count   = results[1]
    print(letters, count)

And lastly, we can fix print statement so we display singular/plural ending for:
used 1 time / times . It's just a simple logical statement

    # Print Report
    for letter,count in dict_count.items():
        if count > 1:
            print(f'{letter} used {count} times.')
        else:
            print(f'{letter} used {count} time.')
    # Print Report
    for letter,count in dict_count.items():
        if count > 1:
            print(f'{letter} used {count} times.')
        else:
            print(f'{letter} used {count} time.')
    # Print Report
    for letter,count in dict_count.items():
        if count > 1:
            print(f'{letter} used {count} times.')
        else:
            print(f'{letter} used {count} time.')

💻 Final Code

Now let's put it all together into a single script:

#🔎 Find Most Frequent Letters in Words 🔠.

#📦 Variables
#----------------------------------------------------------------------------------------------------
random_words = [
    "Lighthouse", "Cat", "Umbrella", "Giraffe"
    "Success", "Volcano", "Balloon",
    "Butterfly", "Mississippi", "Strawberry", "Tnt"
]

#⚙️ Functions
#----------------------------------------------------------------------------------------------------
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')
    word       = word.lower()

    # Count Letters
    dict_count = {}

    for letter in word:
        if letter not in dict_count:
            count = word.count(letter)
            dict_count[letter] = count

    # Print Report
    for letter,count in dict_count.items():
        if count > 1:
            print(f'{letter} used {count} times.')
        else:
            print(f'{letter} used {count} time.')

    # Find Max Count Letters
    max_count = max(dict_count.values())
    max_letters = []
    for k,v in dict_count.items():
        if v == max_count:
            max_letters.append(k)

    # Report Results
    print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')

    return max_letters, max_count


#🎯 Main
#----------------------------------------------------------------------------------------------------

for word in random_words:
    results = most_frequent_letters(word)
    # letters = results[0]
    # count   = results[1]
    # print(letters, count)
#🔎 Find Most Frequent Letters in Words 🔠.

#📦 Variables
#----------------------------------------------------------------------------------------------------
random_words = [
    "Lighthouse", "Cat", "Umbrella", "Giraffe"
    "Success", "Volcano", "Balloon",
    "Butterfly", "Mississippi", "Strawberry", "Tnt"
]

#⚙️ Functions
#----------------------------------------------------------------------------------------------------
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')
    word       = word.lower()

    # Count Letters
    dict_count = {}

    for letter in word:
        if letter not in dict_count:
            count = word.count(letter)
            dict_count[letter] = count

    # Print Report
    for letter,count in dict_count.items():
        if count > 1:
            print(f'{letter} used {count} times.')
        else:
            print(f'{letter} used {count} time.')

    # Find Max Count Letters
    max_count = max(dict_count.values())
    max_letters = []
    for k,v in dict_count.items():
        if v == max_count:
            max_letters.append(k)

    # Report Results
    print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')

    return max_letters, max_count


#🎯 Main
#----------------------------------------------------------------------------------------------------

for word in random_words:
    results = most_frequent_letters(word)
    # letters = results[0]
    # count   = results[1]
    # print(letters, count)
#🔎 Find Most Frequent Letters in Words 🔠.

#📦 Variables
#----------------------------------------------------------------------------------------------------
random_words = [
    "Lighthouse", "Cat", "Umbrella", "Giraffe"
    "Success", "Volcano", "Balloon",
    "Butterfly", "Mississippi", "Strawberry", "Tnt"
]

#⚙️ Functions
#----------------------------------------------------------------------------------------------------
def most_frequent_letters(word):
    print('-'*50)
    print(f'🔎 Checking word: {word}')
    word       = word.lower()

    # Count Letters
    dict_count = {}

    for letter in word:
        if letter not in dict_count:
            count = word.count(letter)
            dict_count[letter] = count

    # Print Report
    for letter,count in dict_count.items():
        if count > 1:
            print(f'{letter} used {count} times.')
        else:
            print(f'{letter} used {count} time.')

    # Find Max Count Letters
    max_count = max(dict_count.values())
    max_letters = []
    for k,v in dict_count.items():
        if v == max_count:
            max_letters.append(k)

    # Report Results
    print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')

    return max_letters, max_count


#🎯 Main
#----------------------------------------------------------------------------------------------------

for word in random_words:
    results = most_frequent_letters(word)
    # letters = results[0]
    # count   = results[1]
    # print(letters, count)

Summary

Great Job! During this lesson:

  • You created a reusable function.

  • You practiced loops, dictionaries, and string methods.

  • You learned how to return multiple values from a function.

  • And you cleaned up the output for readability.

💡Remember, that there are always many ways to solve the same problem. If your solution looks different that's totally fine as long as it works. Focus on getting from A to B and there might be many journeys to get there.

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Follow along the lesson and create this quick tool.

If you feeling like adding extra features, then sort letters based on the count value or alphabetically if multiple letters share the same count value.

⌨️ Happy Coding!

⌨️ Happy Coding!

⌨️ Happy Coding!

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

Have a Questiopn?

Have a Questiopn?

Have a Questiopn?

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

Sposored by LearnRevitAPI.com