[Tutor] Use case of assignment expression
Manprit Singh
manpritsinghece at gmail.com
Tue Apr 27 23:12:40 EDT 2021
Dear sir ,
Consider an example to find the words from a given sentence having given
word as prefix, program should return -1 if no such words found in the
sentence.
The code is written below :
def word_found(string, wd):
if word := [ele for ele in string.split() if ele.startswith(wd)]:
return word
else:
return -1
st = "Anuj is a good boy"
wd = "goo"
ans = word_found(st, wd)
print(ans) returns ['good'] that is correct
st = "Anuj is a bad boy"
wd = "goo"
ans = word_found(st, wd)
print(ans) returns -1 that is also correct.
My question is regarding assignment expressions used in function
definition. Is this the appropriate use of assignment expression?
Need your guidance.
Regards
Manprit Singh
More information about the Tutor
mailing list