Python Fundamentals: Console Input, Boolean Logic, and Conditional Structures

Reading User Input

Basics of Input

The input() function captures keyboard entries. Store the returned value in a variable for later use. You can pass a string argument to input() to display a prompt message before the user types.

user_alias = input("Who are you? ")
print(f"I know you are {user_alias}")

Handling Numeric Input

The input() function always returns a string, regardless of what the user types. Convert the value to an integer or float using int() or float() if numeric operations are required.

user_age = input("What is your age? ")
print(f"Data type of input: {type(user_age)}")

Practical Example

Capture two distinct pieces of information from the user and display a formatted welcome message.

login_name = input("Enter username: ")
membership_grade = input("Enter membership grade: ")
print("%s, you are our valued %s member. Welcome!" % (login_name, membership_grade))
print(f"{login_name}, you are our valued {membership_grade} member. Welcome!")

Boolean Values and Comparison Operators

Boolean Basics

Boolean types represent binary states: True (often treated as 1) and False (often treated as 0). Comparision operators evaluate expressions and yield boolean results.

is_active = True
is_pending = False
print(f"is_active value: {is_active}, type: {type(is_active)}")
print(f"is_pending value: {is_pending}, type: {type(is_pending)}")

Comparison Operators

Use operators to compare values and determine logical outcomes.

val_a = 12
val_b = 12
# Equality check
print(f"val_a == val_b: {val_a == val_b}")

val_a = 12
val_b = 15
# Inequality check
print(f"val_a != val_b: {val_a != val_b}")
# Greater than
print(f"val_a > val_b: {val_a > val_b}")
# Less than
print(f"val_a < val_b: {val_a < val_b}")
# Greater than or equal to
print(f"val_a >= val_b: {val_a >= val_b}")
# Less than or equal to
print(f"val_a <= val_b: {val_a <= val_b}")

Basic if Statements

Syntax and Structure

Conditional execution relies on the if keyword. The condition must evaluate to a boolean. End the condition line with a colon and indent the executable block (usually 4 spaces).

current_age = 18
if current_age >= 18:
    print("You are an adult!")
    print("Time flies.")

Practice: Age Check

Convert input to an enteger to perform numeric comparison.

print("Welcome to the Kids Zone. Free for kids, tickets required for adults.")
age_input = int(input("Please enter your age: "))
if age_input > 18:
    print("You are an adult. Please pay 10 units for entry.")
print("Have a great time!")

if and else Combinations

Syntax

Use else to define a block of code that runs when the if condition is not met. else does not require a condition of its own.

submitted_age = int(input("Enter your age: "))
if submitted_age >= 18:
    print("You are an adult, please purchase a ticket.")
else:
    print("You qualify for free entry!")

Practice: Height Check

guest_height = int(input("Enter your height in cm: "))
if guest_height >= 120:
    print("Height exceeds 120cm. Ticket required.")
else:
    print("Height is under 120cm. Free entry granted.")

Multi-Way Branching with if, elif, and else

Syntax

Chain multiple conditions using elif. The interpreter checks conditions sequentially and executes the first one that is true.

guest_height = int(input("Enter height: "))
vip_status = int(input("Enter VIP level: "))
current_day = int(input("Enter the day of the month: "))

if guest_height <= 120:
    print("Free entry: Height requirement met.")
elif vip_status >= 3:
    print("Free entry: VIP status requirement met.")
elif current_day == 1:
    print("Free entry: First day of the month promotion.")
else:
    print("No qualifying conditions. Ticket purchase required.")

Practice: Number Guessing

target_number = 5
if int(input("Guess the number: ")) == target_number:
    print("Correct!")
elif int(input("Wrong. Try again: ")) == target_number:
    print("Correct!")
elif int(input("Last chance: ")) == target_number:
    print("Correct!")
else:
    print(f"Sorry, the number was {target_number}")

Nested Conditional Logic

Structure

Indent an if block inside another if block to create dependencies. The inner block executes only if the outer condition is satisfied.

print("Welcome to the Zoo")
if int(input("Enter your height: ")) > 120:
    print("Height condition met. No free entry yet.")
    print("Check VIP status for discount.")
    if int(input("Enter VIP level: ")) > 3:
        print("VIP level > 3. Free entry granted.")
    else:
        print("Condition not met. Ticket required.")
else:
    print("Free entry granted due to height.")

Practice: Corporate Gift Eligibility

guest_age = int(input("Enter your age: "))
if guest_age >= 18:
    print("Age requirement met.")
    if guest_age < 30:
        print("Age bracket confirmed.")
        years_employed = int(input("Enter years employed: "))
        job_level = int(input("Enter job level: "))
        if years_employed > 2:
            print("Eligible: Employed over 2 years.")
        elif job_level > 3:
            print("Eligible: Job level above 3.")
        else:
            print("Not eligible for gift.")
    else:
        print("Age bracket not met.")
else:
    print("Must be an adult to receive gift.")

Practice: Random Number Guessing Game

import random

secret_value = random.randint(1, 10)
attempt = int(input("Guess a number (1-10): "))

if attempt == secret_value:
    print("You got it right!")
else:
    if attempt > secret_value:
        print("Too high. Two tries left.")
    else:
        print("Too low. Two tries left.")

    attempt = int(input("Guess again: "))
    if attempt == secret_value:
        print("You got it right!")
    else:
        if attempt > secret_value:
            print("Too high. One try left.")
        else:
            print("Too low. One try left.")

        attempt = int(input("Final guess: "))
        if attempt == secret_value:
            print("You got it right!")
        else:
            print(f"Game over. The number was {secret_value}.")

Tags: python Input/Output Conditional Statements Boolean Logic Control Flow

Posted on Sat, 16 May 2026 08:54:57 +0000 by Sandip