Anagrams with Python

Step-by-step Python programming tutorial

Explore the fascinating world of Python and learn how to solve popular problems like anagrams. This is a must-watch for those interested in improving their coding skills or exploring a career in programming.

Key Insights:

  • The video is led by Art, a Python instructor at Noble Desktop, and focuses on solving the problem of anagrams using Python.
  • An anagram involves words that have the same count of letters. The challenge is to create a program that can identify them.
  • To solve the problem, the user inputs a word which is run through a for loop to create a dictionary where each letter is a key and the number of times it appears is the value.
  • The process involves increasing the value each time a letter appears in the dictionary. This creates a dictionary that counts how many times each letter appears in the word.
  • To compare two or more words, a function called 'word to dictionary' is created. This function takes a word as an argument and returns a dictionary.
  • The dictionaries of two words are then compared to determine if they are anagrams. If the dictionaries match, the words are anagrams. If they don't, the words are not anagrams.

Python Essentials

  • Data Types

    int, float, str, list, dict, set, tuple — the basic vocabulary.

  • Control Flow

    if/elif/else, for, while — the logic of any program.

  • Functions

    def, return, *args, **kwargs — reusable units of behavior.

  • Modules & Imports

    import statements, packages, virtual environments.

Play video

Video Transcription

My name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how to solve a very popular problem called anagrams. What are anagrams? Anagrams would be words that have the same count of letters. So, let me show you how you could solve this one.

First, let's create a variable name 'word' and ask a user for input. So, we would say something like, "Give me a word." When we get this word, we need to use a for loop for letters in the word and then initialize a dictionary with curly brackets. Each letter will be a key and the number of times we see that letter will be the value.

For example, if the letter is in the dictionary, then we'll do plus equal one to increase the value. If not, we'll just do dictionary letter one. To see what the dictionary looks like, run this and suppose we get apple. It creates a dictionary and counts how many times each letter appears in that word.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Now, since we want to compare two or more words, we need to wrap it as a function. Let's call this 'word to dictionary' and this function will take a word as an argument and return a dictionary. To compare these dictionaries, we need to pass a word by word and then compare them. If the dictionary from word one is the same as from the dictionary that we create from word two, then we could say, "Words are anagrams." Else, we want to print, "Words are not anagrams."

If we run it and do that, we get "Words are anagrams." Again, if you want to do something over and over again, wrap it as a function. What is a function? A function is a block of reusable code. Thank you.

Video Transcription:

Full Video Transcript

Introduction to Anagrams

I teach Python, and in this video I'm going to show you how to solve a very popular problem called anagrams. So first of all, what are anagrams? Anagrams would be words that have the same count of letters. Let me show you how you could solve this one.

Getting User Input

First, let's create a variable named word and ask a user for input. You know what input does, so enter something like "give me a word." Then when we get this word, we would need to use a for loop: for letter in word.

By the way, we didn't initialize the dictionary here. How would we initialize the dictionary? We need to use curly brackets. So now what we're going to do next is each letter will be a key, and the number of times we see that letter would be the value.

Building the Letter Count Dictionary

For example, if letter in dictionary, suppose we've already seen that letter, then we'll do plus equal one. So we'll increase the value. Else, we'll just do dictionary letter equals one, so then the value would be one.

If you want to see what the dictionary looks like now, I'm going to show it to you here. Just bear with me for a second, just want to make sure that you could see this. If I run this and suppose we get "apple," now you see what it does. It creates a dictionary and counts how many times each letter appears in that word.

Wrapping the Logic in a Function

Now since we want to compare two or more words, we need to wrap it as a function. How would we create this as a function? Let me zoom out a little bit so you could see. It's going to be def, and let's call this word_to_dictionary. This function will take a word as an argument. Make sure you've got indentation in the right place, and then we want to return the dictionary.

Since we need to compare two words as anagrams, that's going to be word one and word two. Then what I want to do next is call this function, which returns a dictionary, and pass word by word. Then we could compare these dictionaries.

Comparing Words for Anagrams

We could say something like this: if dictionary from word one would be the same as the dictionary that we could create from word two, then print "words are anagrams." Else, because there are just two options, then you want to print "not anagrams."

If I run it and I do "tad" and "add" because these two are anagrams... oops, we get an error "word two is not defined." You see I made a mistake, so this is supposed to be word two here. Let me run this: "add" and then "dad," and now you see "words are anagrams."

Key Takeaway on Functions

So again, if you want to do something over and over again, wrap it as a function. What is a function? A function is a block of reusable code. Thank you.

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram