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 trial

Python

How the code runs!

I am not getting the point how this below code runs and getting output. Please explain.

for letter in "You got this!":
    if letter in "oh":
        print(letter)

Output: o o h

1 Answer

Python follows the following steps:

  • Iterate (loop) through every letter in the string "You got this!"
  • On each iteration, check if the letter is either 'o' or 'h'
  • If it indeed is either 'o' or 'h', print out the letter

There's two Os and one H, so it prints o o h

Awesome. I got the concept. Thanks Alexander