How does the variable in a for loop get defined?

def letter_check(word, letter):
  for character in word:
    if character == letter:
      return True
  return False

print(letter_check("strawberry", "a"))
print(letter_check("strawberry", "123"))

Even if we change letter, word and character’s spelling in code, I still get the output. So it’s not inbuilt keyword like str,len, def or something. So how does python understand what we meant by character equal to letter. And it is not explained anywhere in exercise.


def remove_middle(lst, start, end):
  return lst[:start] + lst[end+1:]

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 4))

Same with this code. I am having difficulty in understanding similar codes. I tried changing function arguments lst, start, end with other words and code still runs fine. My question is same, how python knows, which one is list and how end & start strings are used as indexes? it has anything to do with number of arguments we use while defining function like 3 arguments creates list… first for list, second for starting index and last for ending index? Can anyone explain syntax for above codes… it would be really helpful.


for char in word:

iterates over the word, one character at a time. character is the variable that refers to each letter, in turn. It is the character being iterated that is compared to the letter.

's' == 'a'  => False
't' == 'a'  => False
'r' == 'a'  => False
'a' == 'a'  => True

The call arguments are a list, an integer, and an integer. Their positional counterparts in the parameter are the names these objects are referred by inside the function.

lst   => [4, 8, 15, 16, 23, 42]
start => 1
end   => 4

As for explaining the syntax, that may be jumping the gun if you have not studied list slicing, yet.

1 Like