Skip to main content

Constants

What are Constants? ๐ŸŒŸโ€‹

Constants are like permanent markersโ€”once you write something, it stays that way! These are values in your program that never change.

In pseudo-code, constants are declared using the CONSTANT keyword. Letโ€™s see how they work and how to convert them to Python.


Syntax in Pseudo-Codeโ€‹

Hereโ€™s how constants are declared in pseudo-code:

CONSTANT ConstantName โ† Value

Examples:โ€‹

CONSTANT Pi โ† 3.14159
CONSTANT SchoolName โ† "Greenwood High"
CONSTANT MaxScore โ† 100

Conversion to Python ๐Ÿโ€‹

In Python, we donโ€™t have a special CONSTANT keyword. Instead, we use uppercase variable names as a convention to show they shouldnโ€™t be changed.

Hereโ€™s how the above pseudo-code converts to Python:

Constants in Python
PI = 3.14159  # Constant for Pi
SCHOOL_NAME = "Greenwood High" # Name of the school
MAX_SCORE = 100 # Maximum score

Why Use Constants? ๐Ÿค”โ€‹

  • Prevent Mistakes: Keeps important values from being accidentally changed.
  • Improve Readability: Makes your code easier to understand.
  • Consistent Naming: Ensures everyone knows these values wonโ€™t change.

Fun Fact

In Python, thereโ€™s nothing stopping you from changing a "constant" value. But just because you can, doesnโ€™t mean you should. Stick to the rules for cleaner code!


Letโ€™s Practice ๐Ÿ“โ€‹

Convert these pseudo-code constants into Python:

Challenge
CONSTANT SpeedOfLight โ† 299792458
CONSTANT GoldenRatio โ† 1.618
CONSTANT WelcomeMessage โ† "Hello, World!"

Python Solution:

SPEED_OF_LIGHT = 299792458
GOLDEN_RATIO = 1.618
WELCOME_MESSAGE = "Hello, World!"

Well done, future coding genius! ๐ŸŽ“โœจ