DEV Community

Cover image for Python Programming for Beginners – Day 5
augustineowino357-design
augustineowino357-design

Posted on

Python Programming for Beginners – Day 5

Conditional Statements in Python

In the previous lesson, we learned about Python Operators and how they are used to perform calculations, comparisons, and logical operations. Today, we will learn about Conditional Statements, which allow programs to make decisions based on certain conditions.

Conditional Statements are very important because they help programs respond differently depending on user input or specific situations.


What are Conditional Statements?

Conditional Statements are used to execute different blocks of code depending on whether a condition is "True" or "False".

Python mainly uses:

  • "if"
  • "if...else"
  • "if...elif...else" -_ Nested_ "if"

The if Statement

The "if" statement executes a block of code only if the condition is "True".

Syntax

if condition:
    # code to execute
Enter fullscreen mode Exit fullscreen mode

Example

age = 18

if age >= 18:
    print("You are eligible to vote")
Enter fullscreen mode Exit fullscreen mode

Output

You are eligible to vote
Enter fullscreen mode Exit fullscreen mode

The if...else Statement

The "else" block executes when the condition is "False".

Syntax

if condition:
    # code if condition is true
else:
    # code if condition is false
Enter fullscreen mode Exit fullscreen mode

Example

number = 5

if number % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")`

Enter fullscreen mode Exit fullscreen mode

Output

Odd Number

Enter fullscreen mode Exit fullscreen mode

The if...elif...else Statement

The "elif" statement allows checking multiple conditions.

Syntax

if condition1:
    # code
elif condition2:
    # code
else:
    # code
Enter fullscreen mode Exit fullscreen mode

Example

marks = 75

if marks >= 80:
    print("Grade A")
elif marks >= 70:
    print("Grade B")
elif marks >= 60:
    print("Grade C")
else:
    print("Fail")
Enter fullscreen mode Exit fullscreen mode

Output

Grade B
Enter fullscreen mode Exit fullscreen mode

Nested if Statements

A Nested "if" means placing an "if" statement inside another "if" statement.

Example

age = 20
citizen = True

if age >= 18:
    if citizen:
        print("Eligible to vote")
Enter fullscreen mode Exit fullscreen mode

Output

Eligible to vote
Enter fullscreen mode Exit fullscreen mode

Indentation in Python

Python uses indentation (spaces) to define blocks of code.

Example

age = 18

if age >= 18:
    print("Adult")

Enter fullscreen mode Exit fullscreen mode

Incorrect indentation causes errors.

Incorrect Example

age = 18

if age >= 18:
print("Adult")
Enter fullscreen mode Exit fullscreen mode

This will produce an "IndentationError".


Comparison Operators in Conditions

Conditional Statements commonly use Comparison Operators.

Operator| Meaning
"=="| Equal to
"!="| Not equal to
">"| Greater than
"<"| Less than
">="| Greater than or equal to
"<="| Less than or equal to

Example

x = 10

if x > 5:
    print("x is greater than 5")
Enter fullscreen mode Exit fullscreen mode

Logical Operators in Conditions

Logical Operators help combine multiple conditions.

Operator| Meaning

"and"| Both conditions must be True
"or"| At least one condition must be True
"not"| Reverses the condition

Example

age = 22
has_id = True

if age >= 18 and has_id:
    print("Access Granted")
Enter fullscreen mode Exit fullscreen mode

Output

Access Granted
Enter fullscreen mode Exit fullscreen mode

Simple Login Program

Example

username = input("Enter username: ")
password = input("Enter password: ")

if username == "admin" and password == "1234":
    print("Login Successful")
else:
    print("Invalid Credentials")
Enter fullscreen mode Exit fullscreen mode

Why Conditional Statements are Important

Conditional Statements help programs:

  • Make decisions
  • Validate user input
  • Control program flow
  • Build login systems
  • Create intelligent applications
  • Respond to different situations

Without conditions, programs would always behave the same way.


Conclusion

Conditional Statements are essential in Python programming because they allow programs to make decisions and respond based on conditions.

Understanding "if", "else", and "elif" statements is important for building interactive and dynamic applications.

Practice writing programs using different conditions and logical operators to strengthen your understanding.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)