Python if else Statement
Python if…else Statement : I’m going to talk about conditional structures, which will allow you to do tests and go further in programming. Conditions allow you to execute one or more instructions in one case, other instructions in another case.
Conditions are an essential concept in programming. They will allow you to make a precise action if, for example, a variable is positive, another action if this variable is negative, or a third action if the variable is null.
Python IF statement
Here is the basic structure of a IF statement in python :
if EXPRESSION: STATEMENT
Example 1 :
a = 5 if a > 0: # If a is greater than 0 print("a is greater than 0.")
Output : a is greater than 0.
The print function is executed if a is greater than 0.
number = int(input('Enter a number: ')) if number > 100: print('The number entered is greater than 100!')
Save the code in a file and run it from the command line:
Output: The number entered is greater than 100!
The python script will ask you to enter a number. For example, if you enter 500, the conditional expression will be evaluated to True (500 is greater than 100), and the print function will be executed.
There are two important concepts to consider regarding conditions:
- The first is the instruction block. An instruction block is a series of instructions that are executed in a specific case (by condition, as we have just seen, by repetition). Here, our block consists of only one instruction (the line that calls the print function). But nothing prevents you from putting several instructions in this block.
- The second important notion is that of indentation. Indentation is understood to mean a certain shift to the right, obtained by one (or more) spaces or tabs. Indentations are essential for Python. It is not, as in other languages such as C++ or Java, a reading comfort but a way for the interpreter to know where the beginning and the end of a block are.
Python IF ELSE Statement
The first form of condition that we have just seen is practical but rather incomplete if we want to do several tests.
The statement ELSE allows us to define a first form of complement to our IF statement.
if EXPRESSION1: STATEMENT1 else: STATEMENT3
age = 21 if age >= 18: print("You're an adult.") else: print("You're a minor.")
Output: You're an adult.
The only subtlety is to realize that Python executes either one or the other, and never both. Note that this else statement must be at the same indentation level as the if statement it completes. In addition, it also ends with a colon since it is a condition.
Python IF ELIF ELSE Statement
The elif statement is a contraction of “else if”, which can be translated very literally as “otherwise if”. The Python
if..elif..else statement takes the following form:
if EXPRESSION1: STATEMENT1 elif: EXPRESSION2: STATEMENT2 else: STATEMENT3
The conditions are evaluated sequentially. Once a condition returns TRUE, the remaining conditions are not performed, and the program control moves to the end of the
a=-10 if a > 0: print("a is positive.") elif a < 0: # Négatif print("a is négative.") else: print("a is anull.")
Output a is négative.
Unlike most of the programming languages, Python doesn’t have switch or case statements. A sequence of multiple elif statements can be used as a substitute for the switch or case statements.
The conditions must necessarily introduce new operators, known as comparison operators :
|<||Strictly less than|
|>||Strictly superior to|
|<=||Less than or equal to|
|>=||Greater than or equal to|
|!=||Not Equal to|
Note : The equality of two values is compared with the operator "==" and not "=". The latter is the assignment operator and should not be used in a condition.
The keywords AND,OR and NOT
It often happens that our conditions have to test several predicates, for example when we want to check if any variable, of integer type, is in a precise interval :
a=15 if a>=10 and a<=20: print("a is in the interval.") else: print("a is not in the interval.")
Output: a is in the interval.
The purpose of the exercise is to determine whether a year entered by the user is leap year.
A year is leap year if it is a multiple of 4, unless it is a multiple of 100; however, it is considered leap year if it is a multiple of 400.
I invite you to go to the wikipedia page if you want more information.
I hope you solved it yourself! For those who have problems to solve this exercise, here is the solution ( It’s a solution among many others, you may very well have found something different but that works just as well 🙂 )
year=input("Enter a year : ") year=int(year) leap=False if year % 400 == 0: leap=True elif year % 100 == 0: leap=False elif year % 4 == 0: leap=True else: leap=False if leap: print("The year entered is leap year.") else: print("The year entered is not leap year.")
- Conditions allow certain instructions to be executed in some cases, other instructions in another case.
- Conditions are marked with the keywords IF ,ELIF and ELSE
- The keywords IF and ELIF must be followed by a test
- Booleans are either True or False data.