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

Boolean expression?

Not understanding this question?

Start by executing

s = 'quizzed'

Then write a Boolean expression that check whether

(a) The first character of string s is 'q' (b) The seventh character of s is q (c) The first two character of s is q

This is what I have for a

s[0]

6 Answers

A Boolean expression is a True or False statement.

(a) If s = 'quizzed' then the we can find the first character by using slicing like you suggest. a = s[0], but to get a Boolean expression you have to ask a true/false question about the value stored in a. You do that by a == 'q'. If you want to check it in one step you can ask if the slice is 'q' like this: s[0] == 'q'

(b) This one is similar to (a) above except we are looking for the seventh character and we have to remember that the index is zero based, so we would use the following Boolean expression: s[6] == 'q'

(c) For this question we are asking if two things are true at the same time. The only way to test if two things are true at the same time is using an and function like this: s[0] == 'q' and s[1] == 'q' . You can also ask this question another way by saying are the first two letters equal to 'qq' and you can do that using the following: s[0:2] == 'qq'

I hope that helps answer the question if you need clarification on any of the steps just let me know. I am glad to help.

The first two characters of s are q and a?

s[0] == 'q' and s[1] == 'a'

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Strings also have an awesome method called startswith() so you could do s.startswith('qa'). Or s[:2] == 'qa'.

One of the common principles of programming is called DRY, which stands for "Don't Repeat Yourself". Why would you store the same value in three different variables? You can just use the first variable in multiple locations. If you are instead asking about unpacking a list into multiple variables that can be done by the following: var1, var2, var3 = ['string1', 'string2', 'string3'] Which will result in var1 = 'string1' and var2 = 'string2' and var3 = 'string3' And Yes I do believe that is covered in one of the Python classes taught by Kenneth Love here at Treehouse.

Thanks! Starting to make sense. So does the word is mean == ?

Thanks that's a lot easier.

How would I assign 3 variables that has 3 strings that are the same? Any tutorial that shows this?

variables1 = ['string1', 'string2','string3'] variables2 = ['string1', 'string2','string3'] variables3 = ['string1', 'string2','string3']