Skip to content

Instantly share code, notes, and snippets.

@watxaut
Created October 22, 2019 18:39
Show Gist options
  • Select an option

  • Save watxaut/a7f7f45ec414641ef8f0f92f736fa182 to your computer and use it in GitHub Desktop.

Select an option

Save watxaut/a7f7f45ec414641ef8f0f92f736fa182 to your computer and use it in GitHub Desktop.
Python Training: conditionals
# here we have one boolean
some_true_condition = True
# if the boolean is True, it will print, if not, it won't do anything
if some_true_condition == True:
print("Hey this is True")
if some_true_condition: # This is the same, but more Pythonic
print("Hey this still is True")
# this will not get printed, as it checks for the condition to be False
if not some_true_condition:
print("I will not get any attention :(")
# In here the condition is something more likely to happen inside the code,
# when we evaluate if a number is greater or less than another
another_condition = 2 < 3
if another_condition:
print("True indeed")
else:
print("Nah, it's False")
# with more than one condition and the condition inside the conditional
num = 3
if num < 3:
print("The number is less than 3")
elif num == 3:
print("It's equal!")
else:
print("Nah, it's greater than 3")
# with more than one condition and strings used in conditions.
# Operators "and", "or" and "not"
s_sentence = "Lion Alone"
# checks BOTH conditions to be true (and they are)
if "Lion" in s_sentence and "Alone" in s_sentence:
# will enter here, print "Hey ho!" and skip all other conditions
# because of the if-elif-else structure
print("Hey ho!")
# checks ONE condition to be true (neither of them are)
elif "not" in s_sentence or "and" in s_sentence:
print("<--Input something silly here-->")
# This is true, but it will not enter here as the first conditional matches
elif "lioness" not in s_sentence:
print("I'm out of test sentences")
# if no case is matched
else:
print("I am really out of test sentences and this is the else statement")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment