Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Type conversion means changing a value from one data type to another.
For example:
Here, age is a string.
If we want to perform calculations with it, we can convert it into an integer:
Now age contains an integer:
Python provides several built-in functions for conversion:
Suppose a user enters their age:
Even if the user enters:
Python stores the input as:
It is a string, not an integer.
We can verify this:
If the user enters 25, the output is:
If we want to perform mathematical operations, we need to convert it:
If the user enters:
the output is:
input() always returns the user's input as a str.Python conversions can generally be understood in two ways:
Python performs the conversion automatically in certain situations.
Python produces:
You tell Python to convert the value.
The result is:
Python can automatically convert between some numeric types during an operation.
For example:
Here:
Python handles the numeric conversion automatically.
Output:
The result is a float.
You do not need to manually write:
for this simple numeric operation.
Python handles the conversion for you.
floatThere is another important rule.
In Python 3, / performs true division and returns a float.
Example:
Output:
Even though both numbers are integers, the result is a float.
Compare:
with:
Results:
We will study // in more detail when we learn Python operators.
Explicit conversion means that you manually tell Python which type you want.
The most common functions are:
Let's learn each one.
int()Suppose we have:
Currently:
Convert it using:
Now:
Example:
Output:
float()Suppose:
This is a string.
Convert it:
Now it becomes:
Example:
Output:
You can convert an integer into a float:
Output:
So:
You can also convert a float to an integer:
Output:
int() does not round the number.
It removes the decimal portion.
For example:
Output:
So int() truncates toward zero.
not:
Use str() when you want a string representation of a value.
Example:
Output:
Without str():
Python raises a TypeError because you cannot directly concatenate a string and an integer using +.
Usually, an f-string is even cleaner:
Output:
bool()The bool() function converts a value to either:
or:
Examples:
Output:
For strings:
Output:
The basic idea is:
We call these truthy and falsy values.
Python allows almost any object to be tested in an if condition.
For example:
Output:
Why?
Because "Sumit" is a non-empty string, so it is truthy.
The following values are commonly important to remember as falsy:
For example:
Output:
Look at this:
The result is:
Why?
Because "False" is a non-empty string.
Python does not read the text "False" and automatically turn it into the Boolean value False.
Compare:
with:
Results:
The second one is a string containing characters.
These are all truthy:
Why?
Because:
"0" is a non-empty string"False" is a non-empty string" " contains a space[0] contains one item[False] contains one item100 is not zero-10 is not zeroOutput:
But:
Output:
Even a single space makes the string non-empty.
One common beginner mistake is:
This raises:
Why?
Because "499.99" is not a valid integer string.
Python can directly convert:
but not:
If the string contains a decimal number:
Output:
So the conversion happens in two steps:
ValueError?A ValueError occurs when Python receives a value of the correct general type but the value itself is not suitable for the requested operation.
For example:
Python cannot turn "hello" into an integer.
So it raises:
Another example:
This also causes a ValueError.
This is one of the most important practical uses of type conversion.
Suppose we ask the user for quantity:
If the user enters:
Python receives:
To calculate with it:
If the user enters:
the output is:
For prices, use float() when appropriate:
Example input:
Output:
This pattern is extremely common in real Python programs.
Instead of writing:
you can write:
If name is empty:
the condition is false.
If it contains text:
the condition is true.
This makes Python code shorter and easier to read.
Suppose:
Instead of:
you can write:
This is a common Python style.
If the cart contains items:
then:
Output:
| Type | Example | Who performs conversion? |
|---|---|---|
| Implicit | 5 + 2.5 | Python |
| Explicit | int("25") | Programmer |
| Explicit | float("9.5") | Programmer |
| Explicit | str(100) | Programmer |
| Explicit | bool(0) | Programmer |
| Situation | Don't | Do |
|---|---|---|
| User enters quantity | qty = input() and calculate directly | qty = int(input()) |
| Decimal input | int("75.50") | float("75.50") |
| Float to int | Expect rounding | Remember int() truncates |
| Number + text | "Age: " + age | "Age: " + str(age) or an f-string |
| Empty list check | len(items) == 0 everywhere | if not items: |
| Boolean string | Assume "False" means False | Remember any non-empty string is truthy |
| Invalid conversion | Ignore possible ValueError | Validate or handle the error |
int(4.85) return?int("35.75")?input() return?False?100 / 20 in Python 3?bool("False") return True?Create a small billing program that receives product information as strings and converts the values into the correct data types.
In this lesson, you learned:
int() worksfloat() worksstr() worksbool() worksinput() returns a stringint("35.75") raises ValueErrorbool("False") is Trueif statementsIn the next lesson, we will continue your Python learning journey with Checking Data Types (isinstance) (3: Data Types).
👉 Continue to Next Lesson: Checking Data Types (isinstance) →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Data Types Overview | Checking Data Types (isinstance) |
Test your understanding of this lesson with 6 questions. Each question has one correct answer.