coding180 icon
Coding180
Python Constant

Python Constant

Home > Tutorials > Python for beginners > Python Datatypes


A Python constant is a value that cannot be changed once it has been set. This is different from a regular variable, which can be changed.

For example, if you have a variable called name that stores your name, you can change the value of the variable by assigning a new value to it, like this:

name = "Samantha"

# Later on, you can change the value of the "name" variable
name = "Bob"

But if you have a constant, you cannot change its value once it has been set. For example, you might have a constant called PI that stores the value of the mathematical constant pi:

PI = 3.14

# You cannot change the value of the "PI" constant
PI = 3.15  # This will cause an error!

In Python, constants are typically written in all uppercase letters to make them easy to identify.

Quiz

  1. What is a Python constant?
    1. A. A value that cannot be changed once it has been set.
    2. B. A collection of unique values.
    3. C. A list of values.
    4. D. A collection of key-value pairs
  2. What is the purpose of using constants in Python?
    1. A. To make it easier to identify important values
    2. B. To make it easier to change the values of variables
    3. C. To make it easier to understand the code
    4. D. To make it easier to store a collection of unique values
  3. How are constants typically written in Python?
    1. A. In lowercase letters
    2. B. In uppercase letters
    3. C. In camel case
    4. D. In snake case

user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel