Saturday, 15 August 2015

python - How does "for" define a variable in a function? -



python - How does "for" define a variable in a function? -

i'm trying define function find average length of words in sentence. fine, have enquiry parts of function, namely, this:

random_sentence = str(input('enter sentence:')) def average(): 'takes average length of word in sentence inputed user.' words = random_sentence.split() averageword = sum(len(word) word in words)/len(words) homecoming averageword print(average()) averageword = sum(len(word) word in words)/len(words)

i understand sum , len do, however, how python know 'word' in "for word in words." predefined somewhere? when take phrase out function work, word not defined. appreciate clarification.

for is keyword in python.

when python executes program, transforms file series of tokens lexical analysis. afterwards, tokens parsed in order determine build belong to.

in case, expression token before for makes build generator expression.

python function variables sum

No comments:

Post a Comment