Conditional Statement
Learn the conditional statement in Python.
The most commonly used conditional statement in Python is the if
statement. It contains a conditional expression, a true branch, and it may also contain a false branch. The true branch is executed when the conditional expression is true. The false branch, if present, is executed when the conditional expression is false. The following program demonstrates the use of the conditional statement.
We want to write a program that determines the eligibility for a driver’s license based on the age
input by the user. The eligible age should be or above.
age = int (input ("Enter your age:")) # Taking age input if age >= 18: # If age is greater than or equal to 18 print("You are eligible for a driver's license.") else: print("You are not eligible for a driver's license.")
The conditional statement used in the program above starts with if
, followed by a conditional expression and a colon (:
).
-
Line 3: It contains the code to be executed if the condition is true. Note the indentation (extra space) at the start of line 3.
-
line 4: The word,
else
, ...