Table of Contents

Constants are variables that are not meant to change during the execution of a program. While Python doesn’t enforce immutability like some other languages, it’s a best practice to use constants for values that should remain fixed - such as tax rates, maximum limits, configuration flags, or program-wide labels. Constants help make your code more readable, more reliable, and easier to maintain over time. In Python, constants are typically defined using uppercase letters by convention.

This practice section is focused on understanding how and when to use constants. You’ll define them, use them in calculations, and compare them with user input. These beginner-friendly exercises help form a habit of separating fixed values from dynamic ones - a critical step toward writing clean and maintainable code.

What you should know before starting:

Beginner Level Practice - Constants in Python

Working with constants at the beginner level helps you understand the difference between fixed values and dynamic user inputs. In the following tasks, you’ll define one or more constants and then build small programs that use them in calculations or comparisons. This will show you how to separate logic from configuration, a practice used in almost every real-world Python project. The goal is to form the habit of using constants to make your programs easier to update and more robust against unintended changes.

1. Speed Limit Checker

Create a program that stores the speed limit for a road in a constant called SPEED_LIMIT. Then ask the user to enter their current speed. If their speed is greater than the limit, print “You are over the speed limit!”; otherwise, print “You are driving within the limit.”

What this Task Teaches

This task teaches how to define constants using all-uppercase naming, how to use those constants in conditional logic, and how to compare user input with a fixed reference value. It builds good habits around using constants for configuration-like values.

Hints and Tips
  • Define SPEED_LIMIT = 60 or any value at the top of the script.
  • Use input() to get the user’s speed and convert it to an integer.
  • Compare the current speed to the constant using an if statement.
  • Use clear messages for both outcomes.
Common Mistakes
  1. Using lowercase for constants: Constants should be written in uppercase to clearly indicate they shouldn’t be changed.
  2. Hardcoding the limit instead of using a constant: Always use the constant in your condition - don’t type 60 again.
  3. Forgetting to convert input: Without using int(), comparing a string to a number will cause errors.
  4. Reassigning the constant: Don’t modify a constant’s value later in the program - treat it as locked.
  5. Unclear output: Messages should clearly inform the user whether they’re over the limit or not.
Step-by-Step Thinking

This is a simple check using a fixed reference value. Think of it like a speed radar.

  1. Define a constant SPEED_LIMIT and assign it a number (e.g., 60)
  2. Ask the user to input their current speed
  3. Convert the input to an integer
  4. Use if to compare the speed to the constant
  5. Print the appropriate message based on the comparison
How to Make it Harder
  • Include how many km/h over the limit the driver is.
  • Add a warning if the user exceeds the limit by more than 20 km/h.
  • Let the user choose between different zones with different speed limits (use multiple constants).

2. Daily Calorie Goal

Define a constant called DAILY_CALORIE_LIMIT with a value like 2000. Then ask the user how many calories they have consumed so far today. If they are under the limit, show how many calories they have left. If they are over the limit, show how many extra calories they’ve eaten.

What this Task Teaches

This task shows how to use constants in real-world health or fitness-related logic. You’ll practice subtraction, conditionals, and clean variable naming - all using a fixed constant for comparison.

Hints and Tips
  • Define DAILY_CALORIE_LIMIT = 2000 at the top of the script.
  • Get user input and convert it to a number using int().
  • Use if/else to check whether they’re over or under the limit.
  • Calculate and display the difference clearly.
Common Mistakes
  1. Incorrect subtraction: Be clear about whether you’re calculating “calories left” or “extra calories”.
  2. Hardcoding the limit again: Use the constant in every calculation - don’t retype the number.
  3. Unclear or vague messages: Make sure the user knows exactly how much they’ve gone over or how much room they have left.
  4. Reusing constant names as variables: Never reassign a constant - always treat it as fixed.
  5. Ignoring formatting: Don’t print raw numbers without context. Always say “calories left” or “calories over”.
Step-by-Step Thinking

This is a budget tracker for calories. Think of it as checking your nutritional spending.

  1. Define the constant DAILY_CALORIE_LIMIT
  2. Ask the user to input how many calories they’ve consumed
  3. Convert the input to an integer
  4. Compare the input to the calorie limit
  5. Calculate the difference depending on whether they’re over or under
  6. Display a message with the result
How to Make it Harder
  • Let the user enter calories from multiple meals and sum them up.
  • Add constants for protein, fat, and carb limits and track them too.
  • Store the remaining/over amount in a variable and use it in other messages.

Intermediate Level Practice - Constants in Python

At the intermediate level, constants become especially useful when you're working on programs that involve repeated logic, calculations, or configuration values. Instead of duplicating the same number or string in multiple places, constants make your code cleaner and easier to maintain. These exercises will help you use constants alongside conditionals, arithmetic, and user interaction to simulate realistic scenarios. You’ll also learn to write logic that adapts based on constant values, improving flexibility and clarity in your code. By the end of this section, you’ll be more comfortable separating fixed program settings from dynamic behavior.

1. Ticket Price Calculator

Define a constant called BASE_TICKET_PRICE with a value like 12. Ask the user how many tickets they want to buy and calculate the total cost. Then print a message like “You owe $36”. If the user buys more than 5 tickets, apply a discount by defining another constant called DISCOUNT_RATE (e.g., 0.1 for 10% off).

What this Task Teaches

This task shows how constants can be used in both base calculations and discount logic. You’ll also practice multiplication, conditional discounting, and formatting output clearly using fixed values.

Hints and Tips
  • Define BASE_TICKET_PRICE = 12 and DISCOUNT_RATE = 0.1 at the top.
  • Use int() to get the number of tickets from the user.
  • Multiply to get the total price, and apply the discount if needed.
  • Use round() to clean up the final price.
Common Mistakes
  1. Not using the constants in all places: Always use BASE_TICKET_PRICE and DISCOUNT_RATE - don’t hardcode 12 or 0.1 again later.
  2. Forgetting to apply the discount: Check the ticket count before applying any calculation logic.
  3. Wrong discount formula: You must subtract the discount from the total, not just multiply by the discount rate.
  4. Messy output: Make sure your total is rounded and clearly labeled with the dollar sign.
  5. Reassigning constants: Never change the values of BASE_TICKET_PRICE or DISCOUNT_RATE mid-program.
Step-by-Step Thinking

This is a classic ticket checkout simulation with optional discounts. Think of it like a small ticket machine.

  1. Define BASE_TICKET_PRICE and DISCOUNT_RATE as constants
  2. Ask the user how many tickets they want
  3. Multiply ticket count by base price to get the total
  4. Check if the user bought more than 5 tickets
  5. If so, apply discount: total = total - total × DISCOUNT_RATE
  6. Display the final amount owed, rounded to 2 decimal places
How to Make it Harder
  • Let the user choose between adult and child tickets using separate constants.
  • Use different discounts based on ticket quantity (e.g., 10% for 5+, 20% for 10+).
  • Ask if the user has a promo code and apply an extra discount.

2. Currency Converter

Define a constant called USD_TO_EUR_RATE (e.g., 0.91). Ask the user how many U.S. dollars they want to convert. Use the constant to calculate and display the equivalent amount in euros, like “$50 is 45.5 euros”.

What this Task Teaches

This task demonstrates how constants are used for exchange rates and fixed references. You’ll also practice multiplication, numeric formatting, and string composition using monetary values.

Hints and Tips
  • Use float() to allow decimal input.
  • Multiply the dollar amount by the constant to get the result.
  • Use round() or f-strings to display the result with 2 decimal places.
Common Mistakes
  1. Forgetting type conversion: User input must be converted to float or int before using in calculations.
  2. Reversing the formula: Make sure you're multiplying USD by the exchange rate to get EUR - not the other way around.
  3. Displaying too many decimals: Round the result to 2 digits for currency accuracy.
  4. Hardcoding the rate: Use the constant USD_TO_EUR_RATE for all calculations - don’t retype the number.
  5. Missing symbols in output: Add $ and symbols in your message for clarity.
Step-by-Step Thinking

This is a single-step conversion based on a fixed rate. Think of it like a digital money exchange.

  1. Define the constant USD_TO_EUR_RATE (e.g., 0.91)
  2. Ask the user how many USD they want to convert
  3. Convert the input to a float
  4. Multiply the amount by the constant
  5. Round the result to 2 decimal places
  6. Display a message showing both amounts and their units
How to Make it Harder
  • Let the user choose which direction to convert (USD → EUR or EUR → USD).
  • Add constants for multiple currencies and allow selection.
  • Apply a transaction fee stored in another constant.

Advanced Level Practice - Constants in Python

At the advanced level, constants become a tool for clarity, reusability, and maintainability. In these tasks, you’ll use constants in programs that involve multiple conditions, branching logic, or rule-based processing. Constants will help you avoid repetition, centralize important values, and create programs that are easy to scale or update. These exercises also challenge you to structure your code cleanly and think about how fixed values impact program logic. This is how professionals write code - not just that works, but that’s built to last.

1. Shipping Cost Estimator

Write a program that calculates the shipping cost based on weight. Define three constants: BASE_COST = 5.0 (for packages under 1kg), MEDIUM_COST = 10.0 (for 1–5kg), and HEAVY_COST = 20.0 (for over 5kg). Ask the user for the package weight and display the correct shipping cost.

What this Task Teaches

This task teaches how to use constants to manage logic branches and pricing rules. You’ll practice working with conditionals, comparison operators, and applying clear naming conventions for business rules.

Hints and Tips
  • Define all constants at the top of your program.
  • Use float() for weights since they may include decimals.
  • Use if / elif / else to determine the right pricing tier.
  • Always use constants in the print() statement - not hardcoded numbers.
Common Mistakes
  1. Not using constants throughout: If you write 5.0 directly into logic, the benefit of using constants is lost.
  2. Incorrect range handling: Carefully structure your conditions to avoid overlaps or missed cases.
  3. Missing decimal conversion: Always convert input to float() before comparing with thresholds.
  4. Inconsistent output: Round all currency outputs and include clear units like “USD”.
  5. Skipping the fallback condition: Always have an else block for safety, even if not strictly needed.
Step-by-Step Thinking

Imagine this as an online store’s shipping calculator. The program selects a price tier based on weight.

  1. Define BASE_COST, MEDIUM_COST, and HEAVY_COST at the top
  2. Ask the user to input the package weight in kilograms
  3. Convert input to a float
  4. Use if / elif / else to apply correct pricing logic
  5. Display the final cost using the constant name, not a hardcoded number
How to Make it Harder
  • Include an additional constant for handling fees and add it to all prices.
  • Allow the user to choose between standard and express delivery (use constants for both rates).
  • Calculate total cost for multiple packages in one session.

2. Academic Grade Checker

Define four constants for grade thresholds: GRADE_A = 90, GRADE_B = 80, GRADE_C = 70, and GRADE_D = 60. Ask the user to input their exam score. Then print their grade based on these constants. If the score is below GRADE_D, print “You failed”.

What this Task Teaches

This task helps you build rule-based systems using constants and conditionals. It also reinforces the practice of replacing raw values with well-named constants for better readability and scalability.

Hints and Tips
  • Use integer constants for grade boundaries, not strings.
  • Make sure your comparisons go from highest to lowest grade.
  • Use if / elif blocks to avoid overlapping grade outputs.
  • Format output clearly: e.g., “Your grade: B”.
Common Mistakes
  1. Checking grades in the wrong order: If you check for GRADE_D first, it will trigger before reaching higher grades. Always check from highest to lowest.
  2. Reusing constant values manually: Don’t type 90 or 70 directly - always reference the constant.
  3. Missing an “else” fallback: If the student scores below all thresholds, show a clear failure message.
  4. Invalid data input: Always convert input to an integer and consider what happens if it’s over 100 or below 0.
  5. Spelling errors in print output: Ensure grade letters are uppercase and messages are consistent.
Step-by-Step Thinking

This task models a grading policy where ranges are clearly defined and enforced. Think like a grading system.

  1. Define constants for A, B, C, D thresholds at the top
  2. Ask the user to input their exam score
  3. Convert input to integer
  4. Use if / elif to check score ranges in descending order
  5. Display the corresponding grade or a fail message
How to Make it Harder
  • Let the user enter scores for multiple subjects and show average + grade.
  • Add a GRADE_F constant and support “pass/fail” labeling.
  • Support custom thresholds by asking the teacher to input them.