Strings and Conditionals (Part Two)

I have a question about the use of “not” here, and how it interacts with the use of “in”.

For starters, if I try to put “not” after “in” as in:
if letter in not string_two and empty_list:

I get a syntax error. I would like to understand the functionality of “not” better so I know what it"s supposed to do, when I can use it, and when I can"t.

Thank you very much.

PS. I would also like to learn how to format my code in these posts. I would be grateful if you could point me to a guide or anything that could show me how to do that.

def common_letters(string_one, string_two):
empty_list =
for letter in string_one:
if letter in string_two and not empty_list:
return empty_list

Hey there @mega9414195894 :grinning:

To format your code simply press this button:

And then enter your code between the backticks:

The not operator is used to reverse the boolean value of a statement. For example:

a = True
b = not a 

# b will be equal to False since not was used on a

c = not b

# c will be equal to True since not was used on b

When testing a condition with an if, than not will reverse the value of the condition:

a = False

if not a:
  print("This runs because of the not")

When you are checking for an item in a list you can use the in keyword, and if you want to see if an element is not in a list you can reverse the value of the statement with a not:

lst = ['a', 'b', 'c']

#   This checks if the string a is in lst
#   /
if 'a' in lst:
  print('a is in lst')

#   This checks if the string d is not in lst
#   /
if 'd' not in lst:
  print('d in not in lst')

Your error is resulting from the fact that you are trying to reverse the value of a list, and then check if there is an item in it.

A list can have a boolean value based on if it is or isn’t empty:

a = ['item']
b = []

print(bool(a)) # prints True
print(bool(b)) # prints False

There for not list1 is equal to a boolean value, and you can’t check if an item is in a booleanvalue:

if letter in not string_two and empty_list:

Resulting in your SyntaxError.


Thank you! :slight_smile:

1 Like