Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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