Loading video player…

Understanding Constants

00:00 Understanding Constants and Variables

00:04 Variables and constants are two historical and fundamental concepts in computer programming. Most programming languages use these concepts to manipulate data and work in an effective and logical fashion.

00:16 Variables and constants will probably be present in each project, app, library, or other piece of code that you’ll write. The question is, what are they? In math, a variable is defined as a symbol that refers to a value or quantity that can change over time.

00:33 In programming, a variable is also a symbol or name, typically associated with a memory address containing a value, object, or piece of data. As in math, the content of a programming variable can change during the execution of the code that defines it.

00:50 Variables typically have a descriptive name that’s somehow associated with a target value or object. This target value can be any data type, so you can use variables to represent numbers, strings, sequences, custom objects, and more.

01:06 You can perform two main operations on a variable: access its value, and assign it a new value.

01:14 In most programming languages, you can access the value associated with a variable by citing the variable’s name in your code. To assign a new value to a given variable, you’ll use an assignment statement, which often consists of the variable’s name an assignment, operator, and the desired value.

01:32 In practice, you’ll find many examples of magnitudes, data, and objects that you can represent as variables. A few examples include temperature, speed, time, and length.

01:44 Other examples of data that you can treat as variables include the number of registered users in a web app, the number of active characters in a video game, and the number of miles covered by a runner.

01:55 Math also has the concept of constants. This term refers to a value or quantity that never changes. In programming, constants refer to names associated with values that never change during a program’s execution.

02:10 Just like variables, programming constants consist of two things, a name and an associated value. The name will clearly describe what the constant is about.

02:19 The value is the concrete expression of the constant itself.

02:25 As with variables, the value associated with a given constant can be of any data type. So you can define integer constants, floating-point constants, character constants, string constants, and more.

02:39 Once you define a constant, it will only allow you to perform a single operation on it. You can only access the constants’ value but not change it over time.

02:48 This is different from a variable which allows you to access it, but also reassign it. You’ll use constants to represent values that won’t change. You’ll find lots of these values in day-to-day programming.

03:00 A few examples include the speed of light, the number of minutes in an hour, and the name of a project’s root folder.

03:08 In most programming languages, constants protect you from accidentally changing their values somewhere in the code when you are coding at two in the morning, causing unexpected and hard to debug errors.

03:19 Constants also help you make your code more readable and maintainable. Some of the advantages of using constants instead of using their values directly in your code include improved readability.

03:33 A descriptive name representing a given value throughout a program is always more readable and explicit than the bare bones value itself. For example, it’s easier to read and understand a constant named MAX_SPEED rather than just the number itself.

03:49 Clear communication of intent. Most people will assume that 3.14 may refer to the PI constant, but using Pi, pi, or PI as a name will communicate your intent more clearly than using the value directly.

04:03 This practice will allow other developers to understand your code quickly and accurately. Most of the time these assumptions would be correct, but there may be situations where 3.14 wouldn’t be referring to PI and it could mislead someone reading the code further down the line. Better Maintainability Constants enable you to use the same name to identify the same value throughout your code.

04:27 If you need to update the constants value, then you don’t have to change every instance of the value. You just have to change the value in a single place–the constant definition.

04:37 This improves the code’s maintainability.

04:41 Lower Risk of Errors A constant representing a given value throughout a program is less error-prone than several explicit instances of the value. Let’s say that you use different precision levels for PI depending on the target calculation.

04:55 You’ve explicitly used the values with the required precision for every calculation. If you need to change the precision in a set of calculations, then replacing the values can be error-prone because you can end up changing the wrong values.

05:08 It’s safer to create different constants for the different precision levels and change the code in a single place.

05:16 Reduce Debugging Needs Constants will remain unchanged during the program’s lifetime. Because they’ll always have the same value they shouldn’t cause errors and bugs.

05:26 This feature may not be necessary in small projects, but it may be crucial in larger projects with multiple developers. They won’t have to invest time debugging the current value of any constant.

05:38 Thread-safe Data Storage. Constants can only be accessed, not written. This feature makes them thread-safe objects, which means that several threads can simultaneously use a constant without the risk of corrupting or losing the underlying data.

05:54 You’ve just seen a number of good reasons that constants are an important concept in programming. They can make your life more pleasant and your code more reliable, maintainable, and readable.

06:04 So the next question is when you should use them. Life and particularly science is full of constant values, which never change. A few examples are seen on screen. A constant denoted by PI spelled as PI in English, which represents the ratio of a circle’s circumference to its diameter.

06:24 A constant denoted by ‘e’ are known as Euler’s number, which is closely related to the natural logarithm and compound interest. The number of seconds in an hour, which is considered constant in most applications, even though leap seconds are sometimes added to account for variability in the Earth’s rotation speed.

06:43 Constant representing absolute zero and degree Celsius, which is equal to zero on the Kelvin temperature scale. All of these examples are constant values that people commonly use in life and science. In programming, you’ll often find yourself dealing with these and many other similar values that you can consider and treat as constants.

07:04 So you now know you should use a constant to represent a quantity, magnitude, object, parameter, or any other piece of data that’s supposed to remain unchanged during its lifetime.

07:15 So in the next section of the course, you’ll take a look at how you define constants in Python.

Become a Member to join the conversation.