Skip to main content

Type Conversion in Python: int(), float(), str() and bool() Explained

Have you ever entered a number into a Python program but received an error when trying to perform a calculation? This is a common problem for beginners. It often happens because the data type of a value is different from what the program expects. For example, when you use Python's input() function, the value entered by the user is returned as a string. If you want to perform mathematical calculations with that value, you may need to convert it into a numeric type first. This process is called type conversion . In this article, we will learn what type conversion is, why it is useful, how to convert values using int() , float() , str() , and bool() , and how to avoid common errors. 1. What Is Type Conversion in Python? Type conversion is the process of converting a value from one data type to another. For example, you may convert: A string into an integer. An integer into a floating-point number. A number into a string. A value into a Boolean value. Consider this example: number = ...

Python Constants and Literals

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:

  • age is the variable.

  • 20 is the literal value.

Another example:

name = "Rahul"

Here:

  • name is 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:

  1. Numeric literals

  2. String literals

  3. Boolean literals

  4. Special literal

  5. 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:

  • PI is the name used for the constant.

  • 3.14159 is a numeric literal.

Another example:

MAX_MARKS = 100

Here:

  • MAX_MARKS is intended to be a constant.

  • 100 is 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 literal

  • 85 → integer literal

  • 100 → 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 → variable

  • 20 → 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

TermMeaningExample
VariableName used to refer to a valueage = 20
ConstantName intended to represent an unchanged valuePI = 3.14159
Integer literalWhole number written directly100
Float literalDecimal number written directly10.5
String literalText written directly"Python"
Boolean literalLogical valueTrue
Special literalRepresents absence of a valueNone
List literalList written directly[1, 2, 3]
Tuple literalTuple written directly(1, 2, 3)
Dictionary literalDictionary written directly{"a": 1}
Set literalSet 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