Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjohn larson
16,594 PointsI see this syntax popping up here and there, though it hasn't been covered yet. Is it something that will be taught? or
absorbed through osmosis or something.
# this line
dic[word] = dic[word] + 1 if word in dic else 1
# in this code
def word_count(string):
words = string.split(' ')
dic = dict('')
for word in words:
dic[word] = dic[word] + 1 if word in dic else 1
return dic
1 Answer
James J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsAre you unsure as to the line as a whole, or elements within it?
Perhaps the line written as follows makes it clearer
for word in words:
if word in dic:
dic[word] = dic[word] + 1
else:
dic[word] = 1
You will be taught control flow as you move through the lessons, using keywords to loop over things, terminate loops, and create conditional branches through code.
happy to explain the individual parts of the code if you need it
James
john larson
16,594 Pointsjohn larson
16,594 Pointsright, that's how it has been taught so far. but sometimes i see it the other way. i recognize it all on one line pretty much, it's just unfamiliar