The example on the right follows a general convention…
our_target_object == comparison_object
In this case, our target object is denoted by the parameter.
No difference really, apart from generalized mindset. Certain norms are very well established in the community. We do well to learn and follow them so they become cemented in our thought process.
When trying to see if an item is contained in a list is it more efficient to loop through it as the example does in this lesson or is it more efficient to do an if statement like if var.count({item we’re looking for}) > 0 ?
If all we want is confirm membership, in is the most efficient, one could suppose. However, it is quite like an iterator, as is count() so as time is concerned they may be roughly the same speed, at least in terms of complexity, O(N). Either would be faster than a loop that examines each character.
Had the same exact question. Same thing happened to me. Looks like it only breaks after your dog_breed_I_want choice. But how do we make it only break at that choice and not have to cycle through the beginning and have that get printed. End print result makes zero sense…
I don’t quite follow your point of confusion. Perhaps you can elaborate on your reasoning.
dog_breeds_available_for_adoption is a list of strings.
The value assigned to the variable dog_breed_I_want is the string 'dalmatian'.
for dog_breed in dog_breeds_available_for_adoption:
print(dog_breed)
if dog_breed == dog_breed_I_want:
print("They have the dog I want!")
break
First iteration of loop. The first element of the list i.e. the string 'french_bulldog' is assigned to the loop variable dog_breed. The first statement of the loop i.e. print(dog_breed) prints this string. The if condition evaluates to False as 'french_bulldog' ≠ 'dalmatian'. The iteration finishes.
Second iteration of loop. The second element of the list i.e. the string 'dalmatian' is assigned to the loop variable dog_breed. The first statement of the loop i.e. print(dog_breed) prints this string. The if condition evaluates to True as 'dalmatian' == 'dalmatian'. The string "They have the dog I want!" is printed and then the break statement ends the loop.
Suppose the value assigned to the variable dog_breed_I_want was the string 'sheltie'. Then the for loop would print all the breeds in the list dog_breeds_available_for_adoption. Since 'sheltie' is not an element of this list, so the if condition never evaluates to True (Neither the string "They have the dog I want!" is printed Nor the break statement is reached). When the loop has iterated over all the strings in the list, the loop ends.
That was the whole reason I came to forums. I was like, If
dog_breed_I_want == “Dalmnation”, it should ONLY be printing Dalmnation.
Nevermind, I figured out why it’s printing the french bulldog.
dog_breeds_available_for_adoption = ["french_bulldog", "dalmatian", "shihtzu", "poodle", "collie"]
dog_breed_I_want = "dalmatian"
for dog_breed in dog_breeds_available_for_adoption:
print(dog_breed)
if dog_breed == dog_breed_I_want:
print("They have the dog I want!")
break
The reason it’s printing French Bulldog is because of the first print(dog_breed) statement. Its going to print out French_Bulldog, then continue with the rest of the script. In this scenario, stopping at Dalmation. Thus having both dogs printed on the console.