Introduction to if … else Statements with Python

Introduction to if … else Statements

 Basic Structure

The basic structure of conditional statements in Python is: 

if condition:
    # Block of code executed if the condition is true
else:
    # Block of code executed if the condition is false

  Explanation of the Structure

  • if: The if keyword introduces the condition. The condition must be an expression that evaluates to True or False.
  • condition: This is a boolean expression. Common conditions include comparisons (==, !=, >, <, >=, <=), arithmetic expressions, or function calls that return a boolean.
  • Block of code: The code indented under if will execute only if the condition is true.
  • else: The else keyword is optional and defines a block of code that runs when the condition is false.

Example: 

x = 8
if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")

 Values of Condition

In Python, any expression that returns a boolean (True or False) can be used as a condition. Here are some values considered False in Python:

  • None
  • False
  • 0 (zero, of any numeric type, like 0.0, 0j, etc.)
  • ” (empty string)
  • [] (empty list)
  • {} (empty dictionary or set)
  • () (empty tuple)

All other values are considered True.

Example: 

value = []
if value:
    print("The value is not empty")
else:
    print("The value is empty")

Comparison with Other Languages

Conditional structures in Python are similar to those in other programming languages, but with notable syntactic differences. For example:

  • Python uses indentation to delimit blocks of code.
  • C/C++/Java use curly braces {} to define blocks of code.

Python Example: 

if x > 10:
    print("x is greater than 10")
else:
    print("x is 10 or less")

Example: 

if (x > 10) {
    printf("x is greater than 10");
} else {
    printf("x is 10 or less");
}

 Common Errors

Incorrect Indentation: In Python, indentation is crucial. Incorrect indentation can lead to syntax errors or logical errors.

Example of an Error: 

x = 10
if x > 5:
print("x is greater than 5")  
# Error: code is not correctly indented

 Correction: 

x = 10
if x > 5:
    print("x is greater than 5")

Malformed Conditions: Ensure conditions are properly formed and return a boolean.

Example of an Error: 

x = 10
if x + 5:  # x + 5 is 15, so the condition is always True
    print("x + 5 is non-zero")

 Correction: 

x = 10
if x + 5 > 10:
    print("x + 5 is greater than 10")

 Using == Instead of =: == is for comparison, while = is for assignment.

Example of an Error: 

x = 10
if x = 10:  # Error: assignment instead of comparison
    print("x is 10")

 Correction: 

x = 10
if x == 10:
    print("x is 10")

Advanced Examples

Example 1: Checking User Input Validity 

age = int(input("Enter your age: "))
if age < 0:
    print("Age cannot be negative")
elif age < 18:
    print("You are a minor")
else:
    print("You are an adult")

 Example 2: Password Validation 

password = input("Enter your password: ")
if password == "secret123":
    print("Password is correct")
else:
    print("Password is incorrect")

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print