program-concept

Programming Concepts Explained with Code Examples

Variable Assignments

Variables are named locations used to store data in memory. An assignment statement assigns a value to a variable. In the script, coffee_price and budget are examples of variable assignments.

# Variable Assignments
coffee_price = 5  # Assigning the price of one coffee
budget = 0  # Initializing budget to 0

Functions, Procedures, Calls

Functions encapsulate a block of code that performs a specific task and can be called from other parts of the program. The get_number_of_coffees function calculates how many coffees can be bought within a given budget.

def get_number_of_coffees(budget, price_per_coffee):
    # Function body

Logic Decisions and Branching

Conditional statements (if, else) allow the program to execute different blocks of code based on certain conditions. This concept is demonstrated when deciding if the user’s budget is sufficient to buy any coffee.

if budget < price_per_coffee:
    # Condition met
else:
    # Condition not met

Loops

Loops repeat a block of code multiple times. The while loop in get_number_of_coffees keeps buying coffee until the budget is depleted.

while budget >= price_per_coffee:
    budget -= price_per_coffee
    number_of_coffees += 1

Debugging Techniques

Debugging is the process of finding and fixing errors in code. The script uses logging for debugging, which is a more advanced and flexible technique compared to simple print statements.

logging.debug(f"Debug: Budget entered is {budget}")

Importing and Configuring Logging

The script starts by importing the logging module and setting up the basic configuration. This demonstrates how to incorporate external libraries and configure them for use in a program.

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

Error Handling with Logging

Using try-except blocks for error handling and logging errors when they occur is crucial for robust programs. This technique allows the program to gracefully handle unexpected inputs or situations.

try:
    budget = float(input("Enter your budget for coffee: "))
except ValueError:
    logging.error("Invalid input for budget. Please enter a numeric value.")

Full Python Script

import logging

# Variable Assignments
coffee_price = 2.50
num_cups = 5

# Logic Decisions and Branching
if coffee_price < 3.00:
    print("I'll take two cups!")
else:
    print("Too expensive for me.")

# Functions, Procedures, Calls
def brew_coffee():
    print("Brewing coffee...")

brew_coffee()

# Loops
for cup in range(3):
    print("Brewing cup", cup+1)

# Debugging Techniques
logging.basicConfig(level=logging.DEBUG)

def brew_coffee(cups):
    logging.debug("Brewing %d cups of coffee...", cups)

brew_coffee("two")

# Simple and Compound Conditions within a Programming
if num_cups >= 3 and coffee_price < 3.00:
    print("Let's have a coffee break!")