-5
listFrench = ['un', 'deux', 'trois', 'quatre', 'cinq']
listEnglish = ['one', 'two', 'three', 'four', 'five']
for wF, wE in zip(listFrench, listEnglish):
    print(f"The French word for {wE} is {wF}")

I don't understand the usage of wE and aF , and the f used in the print statement

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 1
    Are you asking why they **chose** those particular variable names? Presumably `wE` is an abbreviation of "word english", and `wF` is an abbreviation for "word french". – John Gordon Aug 04 '23 at 03:38
  • Experiment! Temporarily change that to `for x in zip(listFrench, listEnglish): print(x)`. You'll see that `zip` is returning `tuples` with 2 elements. The `wF, wE` is "tuple unpacking" - python will automatically expand the tuple into those two named variables. – tdelaney Aug 04 '23 at 03:40
  • 1
    The `f` is an [f-string](https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals) – John Gordon Aug 04 '23 at 03:41

1 Answers1

-2

Crack open the python shell and experiment. First, there is what is being returned by zip:

>>> listFrench = ['un', 'deux', 'trois', 'quatre', 'cinq']
>>> listEnglish = ['one', 'two', 'three', 'four', 'five']
>>> for x in zip(listFrench, listEnglish):
...     print(x)
... 
('un', 'one')
('deux', 'two')
('trois', 'three')
('quatre', 'four')
('cinq', 'five')

zip is returning tuples, each with 2 values in it. In fact, its yielding items from each input list. It can take any number of input lists (anything iterable, really) and returns tuples from each item in the lists in turn.

Now, lets see what happens with that first tuple

>>> wF, wE = ('un', 'one')
>>> wF
'un'
>>> wE
'one'

That's tuple unpacking. We have two variables on the left hand side and a tuple with two elements on the right. So, python assigned the variable.

Finally, that "f" is for Augmented String Interpolation. It lets you format a string from data already defined in your program.

>>> f"The French word for {wE} is {wF}"
'The French word for one is un'
tdelaney
  • 73,364
  • 6
  • 83
  • 116