Part II – Use of the Conditional “if”

--Originally published at TC101 – Peaz Cooper

Well well well! Second of all! Ask yourself the following questions:

Do you know how to use the conditional “if” in your code? Do you have a knee pain? Maybe some sore throat? Together we will learn the answers to these awesome and mind-blowing questions!


Our life is made by decisions we take day after day, and our code is not an exception! A decision on our program is used when there are a set of actions that depend on our variable’s value and some of these need more information on order to be functional. Thankfully Python has the “if” statement that helps us to make a decision way simpler on our awesome code

n = input("Integer? ") #Pick an integer.  And remember, if raw_input is not supported by your OS, use input()
n = int(n) #Defines n as the integer you chose. (Alternatively, you can define n yourself)
if n < 0: )
    print ("The absolute value of",n,"is",-n)
else:
    print ("The absolute value of",n,"is",n) 


Here is the output from the two times that Mike Steinberg this program:

Integer? -34
The absolute value of -34 is 34

Integer? 1
The absolute value of 1 is 1

What does the computer do when it sees this piece of code? First, it prompts the user for a number with the statement “n = raw_input("Integer? ")“. Next it reads the line “if n < 0:“. If n is less than zero Python runs the line “print "The absolute value of",n,"is",-n“. Otherwise python runs the line “print "The absolute value of",n,"is",n“.

More formally, Python looks at whether the expression n <

is true or false. An if statement is followed by an indented block of statements that are run when the expression is true. After the if statement is an optional else statement and another indented block of statements. This 2nd block of statements is run if the expression is false. (wiki)


If you follow these steps you’ll be coding like a bawse in no time!!!

#PeazCooperOut #TC101