Day 3: Python's Conditional Statements & Logical Operators

Ayushi singlaAyushi singla
2 min read

Hello, Python Enthusiasts!

Welcome to Day 3 of my 100 Days of Python journey! Today, I explored conditional statements. These allow us to control the flow of our programs based on different conditions. I also explored logical operators and how to create more interactive and dynamic Python programs. These foundational concepts are essential for building dynamic and responsive code. Let’s dive in!


1. Conditional Statements in Python

Conditional statements allow your program to make decisions based on specific conditions. Python offers three primary constructs:

  • if: Executes a block of code if the condition is true.

  • elif: Specifies additional conditions to check if the previous ones are false.

  • else: Executes a block of code if none of the conditions are true.

Example:

# Example of conditional statements
age = int(input("Enter your age: "))
if age < 18:
    print("You're a minor.")
elif 18 <= age < 65:
    print("You're an adult.")
else:
    print("You're a senior citizen.")

In this example, the program checks the user’s age and prints the corresponding message. The flexibility of combining if, elif, and else ensures the code is both clear and functional.


2. Logical Operators in Action

Logical operators like and, or, and not allow you to combine multiple conditions and create complex logic:

Example:

# Logical operators in action
name = input("Enter your name: ")
age = int(input("Enter your age: "))

if name == "Ayushi" and age == 19:
    print("Hello, Ayushi! You’re 19 years old.")
elif name != "Ayushi" or age != 19:
    print("You’re not Ayushi or your age isn't 19.")
else:
    print("Welcome, Ayushi!")

Here, the program combines conditions using logical operators to create a more interactive and dynamic response.


3. Building Interactive Programs

Combining input(), conditional statements and logical operators enables us to build simple interactive applications.

Example: A Simple Login System

# A simple login system
username = input("Enter your username: ")
password = input("Enter your password: ")

if username == "Ayushi" and password == "python123":
    print("Login successful! Welcome, Ayushi.")
else:
    print("Invalid credentials. Please try again.")

Join Me!

Have you tried creating a program using conditional statements? Share your ideas or ask questions in the comments below. Let’s learn and grow together.

Until tomorrow, happy coding! 😊


0
Subscribe to my newsletter

Read articles from Ayushi singla directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayushi singla
Ayushi singla