Learn How to Code with Python: Beginner to Advanced Guide
In many programming languages, a constant is a value that cannot change once defined. Python does not have a built-in “constant” keyword like some other languages. Instead, constants are defined by convention. Developers typically write constant variable names in all uppercase letters to signal that the value should not be modified. For example, `PI = 3.14159` communicates that `PI` is intended to remain the same throughout the program. Python will not prevent reassignment, but disciplined developers treat it as fixed. This approach relies on clarity and responsibility rather than enforcement. Constants improve readability and reduce errors. When a value is used repeatedly and should never vary—like tax rates, configuration values, or physical constants—declaring it once prevents inconsistencies and simplifies maintenance. 1 Using the `const` keyword Using uppercase variable names by convention Using the `final` keyword What is the main way Python defines constants? An immutable object is one whose value cannot be changed after creation. If you attempt to modify it, Python creates a new object instead. This contrasts with mutable objects, which can be altered in place. In Python, common immutable types include integers, floats, strings, and tuples. Mutable types include lists, dictionaries, and sets. Understanding this distinction is essential for avoiding unintended side effects in programs. Immutability improves predictability. When a value cannot change, it eliminates entire classes of bugs related to unexpected modification. 2 List Dictionary Tuple Which of the following is immutable in Python? When you assign an integer to a variable, Python creates an integer object in memory. If you later “change” the variable’s value, you are not modifying the original object. Instead, Python creates a new integer object and reassigns the variable to it. For example: `x = 10` `x = x + 5` This does not alter the original `10`. A new integer `15` is created. This immutability ensures that numeric operations are safe and predictable, especially when values are shared between variables. 1 The original integer object changes A new integer object is created The program crashes What happens when you modify an integer variable? Strings in Python cannot be modified after creation. If you attempt to alter a character within a string, Python will raise an error. For example, `text = "hello"` cannot be modified with `text[0] = "H"`. Instead, you must create a new string: `text = "H" + text[1:]`. This design makes string operations safer, especially when passing strings between functions. 1 Strings are mutable Strings are immutable Indexing is not allowed in Python Why does `text[0] = "H"` cause an error? A tuple is similar to a list but immutable. Once created, its elements cannot be changed, added, or removed. For example: `coordinates = (10, 20)` You cannot modify `coordinates[0]` after creation. Tuples are often used to represent fixed collections of related values, such as geographic coordinates or RGB color values. 1 When you need to frequently modify elements When you need a fixed collection of values When you need better syntax highlighting Why would you choose a tuple over a list? Lists are mutable objects, meaning their content can be changed after creation. You can append, remove, or modify elements freely. For example: `numbers = [1, 2, 3]` `numbers[0] = 10` changes the original list. This flexibility is powerful but can introduce unintended side effects if multiple variables reference the same list. 2 Lists cannot be changed after creation Lists are immutable sequences Lists can be modified after creation Which statement about lists is correct? Immutability improves code reliability. When data cannot change unexpectedly, it reduces hidden dependencies between parts of a program. In concurrent programming, immutable objects are especially valuable. Since they cannot be modified, they eliminate race conditions involving shared data. Immutability also allows Python to optimize memory usage in certain cases. 1 They can always grow in size They prevent unintended modification They execute faster in every case One advantage of immutable objects is: Every object in Python has an identity, accessible with the `id()` function. When you modify an immutable object, its identity changes because a new object is created. For example: `x = 5` `print(id(x))` `x = 6` `print(id(x))` The second `id()` will differ from the first. This demonstrates that reassignment does not alter the original object but binds the variable to a new one. 1 The object was modified in place A new object was created Memory was deleted What does a change in `id()` after reassignment indicate? Constants are often defined at the top of a module so they can be reused throughout the program. This centralization makes updates easier and reduces duplication. For example: `MAX_USERS = 100` Other functions in the module reference this value instead of hardcoding numbers. Organizing constants clearly signals which values are foundational to the system’s logic. 1 To hide them from other developers To improve reuse and maintainability To make the file longer Why place constants at the top of a module? Python provides tools like `frozenset`, which is an immutable version of a set. Unlike regular sets, you cannot add or remove elements from a `frozenset`. Because it is immutable, a `frozenset` can be used as a dictionary key, while a normal set cannot. Frozen data structures are useful when you need hashable collections that must remain constant. 1 Because it is mutable Because it is immutable and hashable Because it automatically sorts elements Why can `frozenset` be used as a dictionary key?