Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
Nancy Melucci
Courses Plus Student 35,157 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,652 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