Https://www.codecademy.com/courses/learn-python-3/lessons/introduction-to-strings/exercises/strings-in-conditionals-ii

#I am unable to understand the below code , can someone explain me please?
#I am not able to understand the lines
#for letter in string_one:
#if (letter in string_two) and not (letter in common):

def contains(big_string, little_string): return little_string in big_string def common_letters(string_one, string_two): common = [] for letter in string_one: if (letter in string_two) and not (letter in common): common.append(letter) return common print(common_letters("banana", "cream"))

Hi,

The code essentially returns all the characters that two strings have in common.
Here’s a bit more on how it works:

‘common_letters’ takes in two input strings. It loops through the first string, looking at each letter in the string individually (using the for loop).
It looks at each letter and if it is in string two, it appends it to the ‘common’ array (as long as it isn’t already in the array, which is what the not(letter in common) does).
After it has looked through the whole first string, it returns all the letters in common.

Hope that helps!

Thank you for the explanation :slight_smile:

Please correct me if i understood wrong.

so the for loop looks for each letter that is “banana”

Then in if condition it checks whether the letter b is present in the second string (“cream”) or not.
in the same way it checks each and every word from banana if its present in cream or not and if its present and common then it adds to the common list.

but i don’t understand why not is used.

sorry for asking such silly questions but i am completely new to coding.
Just started learning in codecademy.

The function contains() takes two parameters, big_string, and little_string, and will return True or False depending on if little_string in big_string. For example:


def contains(big_string, little_string):
  return little_string in big_string


print(contains("Hello', 'el'))  # True

This returns True because little_string (‘el’) is in big_string(‘Hello’).

The second function common_letters() takes two parameters as well, string_one, string_two

def common_letters(string_one, string_two):
  common = []
  for letter in string_one:
    if (letter in string_two) and not (letter in common):
      common.append(letter)
  return common

print(common_letters("banana", "cream"))

The for loop is specifying, for ever letter in string_one, IF that letter is in string_two AND NOT in empty list common then append that letter to the list (i.e. common), then return that list.

So as the code runs, every letter in “banana” gets iterated over.

  1. Is the letter “B” in string_two and Not in common? No
  2. is the letter “A” in string_two and Not in common? Yes, then append it to common.
  3. is the letter “N” in string_two and Not in common? No
  4. is the letter “A” in string_two and Not in common? No (it’s in both strings but it is in the list already)
  5. is the letter “N” in string_two and Not in common? No
  6. is the letter “A” in string_two and Not in common? No (it’s in both strings but it is in the list already)

That is why the code returns [‘a’]

I hope this helps!

The ‘not’ is used to prevent a letter from being added more than once. For example ‘a’ is in banana 3 times but the not is used to check whether ‘a’ is already in common. If it isn’t, it adds it to common. If it is, it just moves on.

This prevents duplicates in the common array.

Hope this helps!

Got it!

Thank you :slight_smile:

Thank you @seraph776

This is what i was looking for.

I understand the complete process now.

:handshake:

I am guessing

in the below lines it should be string_two not string_one , am i correct?
because as you mentioned " for ever letter in string_one , IF that letter is in string_two AND NOT in empty list common then append that letter to the list (i.e. common ), then return that list."

  1. Is the letter “B” in string_one and Not in common ? No
  2. is the letter “A” in string_one and Not in common ? Yes, then append it to common.
  3. is the letter “N” in string_one and Not in common ? No
  4. is the letter “A” in string_one and Not in common ? No (it’s in both strings but it is in the list already)
  5. is the letter “N” in string_one and Not in common ? No
  6. is the letter “A” in string_one and Not in common ? No (it’s in both strings but it is in the list already)

Yes, you are correct. It compares it against string_two.
It is looping through string_one so each letter is in string_one. It should be:

  1. Is the letter “B” in string_two and Not in common ? No
  2. is the letter “A” in string_two and Not in common ? Yes, then append it to common.
  3. is the letter “N” in string_two and Not in common ? No
  4. is the letter “A” in string_two and Not in common ? No (it’s in both strings but it is in the list already)
  5. is the letter “N” in string_two and Not in common ? No
  6. is the letter “A” in string_two and Not in common ? No (it’s in both strings but it is in the list already)

Thank you !! @bcoding21

Good catch! You are absolutely correct! I edited my original post to reflect the correction!

Finally my confusion is gone.

Thank you both @seraph776 and @bcoding21

2 Likes