When learning Python variables, you will often come across two important terms: constants and literals.
Although these terms may sound complicated at first, they are actually quite easy to understand.
In this article, we will learn:
What constants are
What literals are
Difference between constants and literals
Different types of literals
Examples of literals in Python
How constants are commonly represented in Python
Common mistakes
Practice questions
What Is a Constant?
A constant is a value that is intended to remain unchanged throughout a program.
For example, suppose a program uses the value of π:
PI = 3.14159
Here, PI is intended to represent a fixed value.
Similarly:
MAX_MARKS = 100
DAYS_IN_WEEK = 7
These values are intended to remain the same.
Constants in Python
Python does not provide a special keyword for declaring a constant.
Instead, programmers generally use uppercase variable names to indicate that a value should not be changed.
Example:
PI = 3.14159
MAX_MARKS = 100
DAYS_IN_WEEK = 7
The uppercase names communicate the programmer's intention that these values should remain unchanged.
For example:
MAX_MARKS = 100
print(MAX_MARKS)
Output:
100
Why Are Constants Useful?
Constants are useful when a particular value is used repeatedly in a program and should remain fixed.
For example:
PI = 3.14159
radius = 5
area = PI * radius * radius
print("Area =", area)
Instead of repeatedly writing the value of π, we use the name PI.
This makes the program easier to read and maintain.
What Is a Literal?
A literal is a value written directly in a Python program.
For example:
age = 20
Here:
ageis the variable.20is the literal value.
Another example:
name = "Rahul"
Here:
nameis the variable."Rahul"is a string literal.
So, a simple way to remember it is:
A literal is a value directly written in the program.
Example of Literals
Consider this program:
name = "Amit"
age = 20
percentage = 85.5
The literals are:
"Amit"
20
85.5
The variables are:
name
age
percentage
Types of Literals in Python
Python supports different types of literals.
Common types include:
Numeric literals
String literals
Boolean literals
Special literal
Collection literals
Let's understand them one by one.
1. Numeric Literals
Numeric literals represent numbers.
They can be divided into different types.
Integer Literals
Integer literals are whole numbers without a decimal point.
Examples:
10
25
100
-50
0
Example:
age = 20
Here, 20 is an integer literal.
Floating-Point Literals
Floating-point literals represent numbers containing a decimal point.
Examples:
10.5
25.75
3.14
-2.5
Example:
price = 99.50
Here, 99.50 is a floating-point literal.
Complex Literals
Python also supports complex numbers.
Example:
z = 3 + 4j
Here:
3 + 4j
is a complex literal.
The j represents the imaginary part.
2. String Literals
A string literal represents text.
Strings can be written using single or double quotation marks.
Example:
name = "Rahul"
Here:
"Rahul"
is a string literal.
We can also use single quotes:
name = 'Rahul'
Both represent a string.
String Literal Examples
city = "Kolkata"
country = 'India'
message = "Welcome to Python"
Here:
"Kolkata"is a string literal.'India'is a string literal."Welcome to Python"is a string literal.
3. Boolean Literals
Python provides two Boolean values:
True
False
They represent logical values.
Example:
is_student = True
Here, True is a Boolean literal.
Another example:
is_logged_in = False
Here, False is a Boolean literal.
Boolean values are commonly used when working with conditions.
4. Special Literal: None
Python has a special literal called:
None
None represents the absence of a value.
Example:
result = None
Here, the variable result currently has no meaningful value.
None is different from:
0
and:
""
It represents the absence of a value rather than a number or an empty string.
5. Collection Literals
Python also provides literals for creating collections.
Some common collection literals are:
List
Tuple
Dictionary
Set
These topics will be studied in greater detail later in the Python course.
List Literal
A list is written using square brackets:
numbers = [10, 20, 30]
Here:
[10, 20, 30]
is a list literal.
Tuple Literal
A tuple can be written using parentheses:
numbers = (10, 20, 30)
Here:
(10, 20, 30)
represents a tuple.
Dictionary Literal
A dictionary uses curly brackets with key-value pairs:
student = {"name": "Rahul", "age": 20}
Here the dictionary is represented directly in the program.
Set Literal
A set can be written using curly brackets:
numbers = {10, 20, 30}
These collection concepts will be covered in more detail in later articles.
Constant vs Literal
Constants and literals are related, but they are not the same thing.
Consider:
PI = 3.14159
Here:
PIis the name used for the constant.3.14159is a numeric literal.
Another example:
MAX_MARKS = 100
Here:
MAX_MARKSis intended to be a constant.100is an integer literal.
So remember:
Constant → A name representing a value intended to remain unchanged.
Literal → The actual value written directly in the program.
Simple Example
Consider:
PI = 3.14159
radius = 10
area = PI * radius * radius
print("Area =", area)
Let's identify the different parts.
PI is intended as a constant.
3.14159 is a numeric literal.
radius is a variable.
10 is another numeric literal.
The expression:
PI * radius * radius
calculates the area.
Another Example: Student Program
Consider:
MAX_MARKS = 100
student_name = "Rahul"
marks = 85
print("Student:", student_name)
print("Marks:", marks)
print("Maximum Marks:", MAX_MARKS)
Here:
MAX_MARKS→ constant-style name"Rahul"→ string literal85→ integer literal100→ integer literal
Output:
Student: Rahul
Marks: 85
Maximum Marks: 100
Important Point About Constants in Python
Python allows the value of an uppercase variable to be changed.
For example:
MAX_MARKS = 100
MAX_MARKS = 200
Python will allow this.
However, using uppercase names communicates that the value is intended to be treated as a constant.
Therefore, the uppercase convention is important for writing clear Python programs.
Common Mistakes
Mistake 1: Confusing a variable with a literal
Consider:
age = 20
Do not say that age is the literal.
Correct:
age→ variable20→ literal
Mistake 2: Using lowercase names for constants
You may see:
pi = 3.14159
This can work, but when a value is intended to be a constant, Python programmers commonly use:
PI = 3.14159
The uppercase name makes the intention clearer.
Mistake 3: Confusing None with 0
These are different:
a = None
b = 0
None represents the absence of a value, while 0 is a numeric value.
Quick Revision Table
| Term | Meaning | Example |
|---|---|---|
| Variable | Name used to refer to a value | age = 20 |
| Constant | Name intended to represent an unchanged value | PI = 3.14159 |
| Integer literal | Whole number written directly | 100 |
| Float literal | Decimal number written directly | 10.5 |
| String literal | Text written directly | "Python" |
| Boolean literal | Logical value | True |
| Special literal | Represents absence of a value | None |
| List literal | List written directly | [1, 2, 3] |
| Tuple literal | Tuple written directly | (1, 2, 3) |
| Dictionary literal | Dictionary written directly | {"a": 1} |
| Set literal | Set written directly | {1, 2, 3} |
Practice Questions
Try answering these questions yourself.
Question 1
What is a literal in Python?
Question 2
Identify the literal in:
age = 25
Question 3
Write a Python statement to create a constant representing the maximum marks as 100.
Question 4
Identify the type of literal:
"Python"
Question 5
Identify the type of literal:
25.5
Question 6
What is the purpose of None in Python?
Question 7
What is the difference between a constant and a literal?
Final Thoughts
Constants and literals are simple concepts, but understanding them properly will help you read and write Python programs more confidently.
The most important point to remember is:
A literal is a value written directly in the program, while a constant is a name used for a value that is intended to remain unchanged.
As you continue learning Python, you will encounter literals in almost every program you write.
Comments
Post a Comment