Conditional statements

Conditional statements are common among programming languages and they are used to perform actions or calculations based on whether a condition is evaluated as true or false.

If-then-else statements or conditional expressions are essential features of programming languages and they make programs more useful to users. In Python, conditional statements will end with a colon: and the section of code to be run will be indented further from the left of the screen that the conditional statement line.

If Statements

An if statement only runs a section of code if the condition in the statement results in a boolean True.

print("Program starts")

total_value = 99
minimum_value = 40

if total_value > minimum_value:
    print("Totals")
    print(total_value)

print("Program ends")

If we run this we will see a line saying Total and then the 99 store in total_value is printed beneath it.

If we change total_value to something less than or equal to 40 then the comparison is no longer True. Because it isn’t true the two indented print lines are no longer executed.


If Else Statements

An if else statement runs a section of code if the condition in the statement results in a boolean True but has a second section of code for when the condition is False.

print("Program starts")

total_value = 35
minimum_value = 40

if total_value > minimum_value:
  print("Totals")
  print(total_value)
else:
  print("Total still too small")

print("Program ends")

If ElseIf Else Statements

An if, elseif, else statement runs a section of code if the condition in the statement results in a boolean True but if it is False then it checks a subsequent if statement but it also has a section of code for when none of the if statements return True.

print("Program starts")

total_value = 40
minimum_value = 40

if total_value > minimum_value:
  print("Totals")
  print(total_value)
elif total_value == minimum_value:
  print("Just made it")
else:
  print("Total still too small")

print("Program ends")

elif stands for else if and must have a comparison that returns True or False after it.

You can use multiple elif statements to carry out multiple if statements in which you stop checking once a match is found.

print("Program starts")

total_value = 38
minimum_value = 40

if total_value > minimum_value:
  print("Totals")
  print(total_value)
elif total_value == minimum_value:
  print("Just made it")
elif total_value > (minimum_value - 5):
  print("Just too small")
else:
  print("Total still too small")

print("Program ends")

In this code we first check if the total is above the minimum value. If it isn’t we then check if it is equal to the minimum value. If it doesn’t match that then we check if it is greater than 5 below the minimum value. And finally, if nothing else has been matched we use the else statement to catch every value.


Syntax

Indentation matters!

if x > y:
  z = True
  if a > b:
    total = a
  else:
    z = False
print(z)

In most programming languages you will see curly brackets or braces {} used to surround sections of code. The whitespace and indentation from the left of the screen is just to make it more readable.

In Python, indentation is everything. The indentation controls what is considered a block of code. Every line beginning the same distance from the left of the screen is seen as a block of code.

Use brackets for long expressions:

if(really_long_variable_name > another_long_variable_name and really_long_variable_name < yet_another_long_variable_name):

If you have an expression that takes up a lot of space or wraps onto another line you can use brackets around it.

And remember to use your comments to explain not just what the file is doing, but throughout the code to explain what each is there for.


Pass

During development you sometimes need to enter some code into a code block but haven’t yet designed that part. Just entering a comment isn’t enough to count as a valid block of code. This is where you can use the pass keyword. It is a placeholder that you will replace later on in development.

if x >= y:
    # Add some code here in the future 
    pass
else:
    print("x is too small")
Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.