Project 10: Conditional If-Statement

--Originally published at TEC GDL 2016

Within Python it will be useful very quickly to begin adding some degrees of logic to your program. One of the tools you have to add logic is the conditional If-assignment.

The basic logic behind the If statement is the following:

  • The program checks if something is the case
  • if it is the case: do something
  • – otherwise: continue, don’t do anything

Use of assignment operators within If:

x = 5
y = 8
if x > y:
        print(‘x is greater than y’)

If you run this program nothing will happen. Why?
Well, as I have just explained, if the condition within the If statement is not fulfilled (see here: 5 is not greater than 8) Python continues the program and chooses to ignore the If condition. When we now change the numbers to x=8 and y=5, the code is going to print ‘x is greater than y‘.

Just as the usual < and > signs, we are also able to add other assignment operators that work just as well (e.g. <= ; >=). One of the only exceptions would be the = sign where we have to use ‘==’ because the usual ‘=’ we know from mathematical calculations is already used within Python when using a variable definition.

Thanks for visiting my blog once again. Feel free to ask any questions within the comment section or hit me up at Twitter @tecjames.

For a list of all operators to be used within Python visit the following website:
https://www.tutorialspoint.com/python/python_basic_operators.htm

Best,

James