

In the case below, the tasks executed print(“you can enter”) only occurs if the variable age is greater than 18 is a True case because this line of code has the indent. The tasks will end when the line of code does not contain the indent. The lines of code after the colon and with an indent will only be executed when the if statement is True. Start your tasks which need to be executed under this condition in a new line with an indent. The syntax is as simple as if condition statement :, which contains a word if, any condition statement, and a colon at the end. Use the condition statements learned before as the conditions need to be checked in the if statement. If they are 18 or younger than 18 they cannot enter the concert. If the individual is older than 18, they can enter the ACDC concert. It is helpful to think of an if statement as a locked room, if the statement is True we can enter the room and your program will run some predefined tasks, but if the statement is False the program will ignore the task.įor example, consider the blue rectangle representing an ACDC concert. Note: Upper Case Letters have different ASCII code than Lower Case Letters, which means the comparison between the letters in python is case-sensitive.īranching allows us to run different statements for different inputs. When there are multiple letters, the first letter takes precedence in ordering:

Similarly, the value for A is 101, and the value for B is 102 therefore: Therefore + is larger than ! as 43 is greater than 21. The decimal value shown in the following table represents the order of the character:įor example, the ASCII code for ! is 21, while the ASCII code for + is 43. Inequality operation is also used to compare the letters/words/symbols according to the ASCII value of letters. # Use Inequality sign to compare the strings If we use the inequality operator, the output is going to be True as the strings are not equal. # Use Equality sign to compare the strings As the strings are not equal, we get a False.

For example, use an equality operator on two different strings. We can apply the same methods on strings. When i equals 6 the inequality expression produces False. For example, the following condition will produce True as long as the value of i is not equal to 6: The inequality test uses an exclamation mark preceding the equal sign, if two operands are not equal then the condition becomes True. The statement is false as 2 is not greater than 5: If i is equal to 6, because 6 is larger than 5, the output is True. If the value of the left operand, in this case the variable i, is greater than the value of the right operand, in this case 5, then the statement is True. When comparing two values you can use these operators: Python Programming Fundamentals Conditions and BranchingĬomparison operations compare some value or operand and, based on a condition, they produce a Boolean.
