Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping While Loops

Steven Zhang
Steven Zhang
310 Points

Constant variables

what is the difference between constant and normal variables, and how does a constant work and why use it in the first place. 1.)difference 2.)how does it work 3.)purpose

Nancy Melucci
seal-mask
.a{fill-rule:evenodd;}techdegree
Nancy Melucci
Front End Web Development Techdegree Student 34,198 Points

I get your meaning but "constant" and "variable" are actually different from each other. A constant is a value you set during your program that cannot be manipulated by the functions and processes in it, but can be used by it. I suppose it's a "variable" because you may set it at the start, but unlike a true variable, it should not be changed by the program's activities. An example would be a tax rate in a simple program to calculate a tax bill. You can set two or three "constant variables" or I would call them "constants" - one for the bottom tier (lorate = .15), one for the middile (midrate = .23) and one for the high earners (hirate = .29) and then your decision structure would determine which one should be applied. The three constants will not change and your if/elif/else will apply them to the gross earnings of the user of the program. I hope that makes sense. The tax bill would be a true variable, and its final value would be determined by the gross pay times the selected tax rate.

1 Answer

Josh Keenan
Josh Keenan
19,426 Points

A constant and variable are two different things. A variable you know, it stores a value and can be changed and updated; a constant is a value that should not change.

Both can be used in almost the exact same ways with the only constraint of never being able to change a constant, so you can use it for things you know won't be changing anywhere during your program, that you will still need to use