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

When to use AND (and OR)

His so I had this code

def first_4(item):
    list(item) and return item[0:4]

It didn't work unless i took out the and and put the return item[0:4] on the next line. THIS DIDN'T WORK IN THE CHALLENGE... But it worked in workspaces... anyone know why? Regardless could you answer the following question below?

I thought it would work. Why didn't it work and in what situations should I use the and? Please tell me the same for or as I may run into the same problem in using or

2 Answers

If you are doing multiple things within a function you need to put them on seperate lines. the AND and OR statements are conditional statements that go into if/while as conditions for making the loop run, for example if i > 5 and a < 3: DO SOMETHING

you would never use and/or within a function to do multiple things.

Susannah Klanecek By if/while loops do you mean if/else statements or for/while loops lol? Also, if what you are saying is correct... Why did it work in workspaces??

sorry I meant if statements and while loops, aside from the example I gave you could also do

while Something or Other_thing: do something

as to why it worked in workspaces, I don't know but it shouldn't have, workspaces sometimes have weird behavior