Why does my print statement not need to be indented?

Hello everyone!

I have a question regarding indent. Why do we have no indent in the line of print(“They have the dog I want!”)?
And what will happen if we insert indent so that the line aligns with the if statement above it?
Thank you very much!

Because it is not part of the the if block nor part of the for block.

4 Likes
items_on_sale = ["blue_shirt", "striped_socks", "knit_dress", "red_headband", "dinosaur_onesie"]

print("Checking the sale list!")
for item in items_on_sale:
  print(item)
  if item == "knit_dress":
    break
print("End of search!")

I dont understand this: since the programm executes orders from above to below, when theres no command to say stop at this point before it start printing, why didnt the programm just print all the items in the list?I mean why not be like this? The flow makes more sense:

print("Checking the sale list!")
for item in items_on_sale:
  if item == "knit_dress":
    break
    print(items)
print("End of search!")

We cannot see your indentation. I’m going to edit your post to see if any issues become visible…


We can see print after break which is not possible. Anything after break is unreachable.

4 Likes

We are given the dog breed list and we have a variable for a breed we want.

Inside the for loop, every breed gets printed, following the 1st line and then after an if condition, we again print “They have the dog i want!”

My question is, if there wasn’t any IF statement, “They have the dog I want!” would have anyway been printed. Why isn’t the print statement indented in if, when that string should only be printed if the IF condition holds.

(We can also store in dog_breed_I_want a breed that is not present in the dog_breeds, then this loop will generate the wrong result.)

The key is to choose where and what to print. It’s up to us where to print.

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
b = 'g'
for x in a:
    if x == b:
        print ('Found match in list')
        break
else:
    print ('Match not found in list')

Note that the else is on the for not the if. When a match is found, the print statement inside the loop executes. When no match is found, the print statement in the else clause is executed. Change the line to read,

b = 'h'

and run it again to see the last line print.

If this is new to you, then read up on for..else to gain a deeper understanding of its usage.

1 Like

I can see where the confusion comes in here.
I’m new to programming so you elites correct me if I’m wrong but this is what the gist of the concept being implemented.

Here is an example:

number_sequence = range(6)
for number in number_sequence:
  print(number)
  if number == 3:
    break
print("We reached to the number 3 and no need to keep counting up to 5")
  1. The “for” loop is just an iteration of a sequence of elements you give it. ie range(6)…0, 1, 2, 3, 4, 5.
    Inside the “for” loop it looks at each element one at a time.

  2. The “if” condition inside the “for” loop is just a condition you want the “for” loop to do something if the condition is met.to reach too and do something else.
    Here we have an “if” condition that states while the “for” loop iterates, checks each element one at a time, check to see if the “if” condition is met. If the “if” condition is not met, continue with the “for” loop.

  3. So each time the “for” loop iterates the “print(number)” function is executed.

  4. Once the “if” condition is met, the “for” loop terminates, breaks, does not execute any more.

  5. Finally once the “for” loops terminates, breaks, does not execute any more, the last “print(“We reached to the number 3 and no need to keep counting up to 5”)” executes.

I see what you are saying why didn’t every element be printed first then execute the “if” condition.
Here it would not do that because you formated the code to check the “if” condition after each “for” loop iteration was made.

If you wanted the it to print out each iteration first then check for the “if” condition is met, you would have to re-format your code like this…

number_sequence = range(6)

for number in number_sequence:
  print(number)
for number in number_sequence:
  if number == 3:
   print("We reached to the number 3 and no need to keep counting up to 5")
3 Likes

What will happen if the item we want is at the end of a list with 1000 items? Python will print out all of these items before reached break statement? Is there any solution to solve it?

if you don’t indent your “print(“They have the dog I want!”)” statement it will execute it even if your “if” condition is not satisfied and if you want in to execute only when your “if” condition is satisfied then you have to use indentation.

If we have a sorted list, there are algorithms you can use to search very quickly, such as binary search.
With an unsorted list though, our choices are to potentially search through every item or take the time to sort the list and then search.

1 Like