DEV Community

Cover image for Type Conversion in Python
Ganesh Kumar
Ganesh Kumar

Posted on

Type Conversion in Python

Hello, I'm Ganesh Kumar, and I'm building LiveReview — a blast-radius aware AI code review built for your business-critical systems. Star us to help devs discover the project, give it a try, and share your feedback to help improve the product.

What is Type Conversion?

The process of converting from one data type to another.

For example:

Converting from str to int.

Under type conversion, Python has two types:

  1. Implicit Conversion - Automatic type conversion
  2. Explicit Conversion - Manual type conversion

Implicit Type Conversion

Whenever Python automatically converts one data type to another, it is known as Implicit Type Conversion.

Let's understand with an example.

We have decimal-point literals and integer literals.

So, decimal values will be defined as the float data type, and integer values will be the int data type.

Now, when we add these two, we will get like this:

integer_num = 123
print("Data Type:", type(integer_num))
float_num = 1.23
print("Data Type:", type(float_num))
new_num = integer_num + float_num

# display new value and resulting data type
print("Value:", new_num)
print("Data Type:", type(new_num))
Enter fullscreen mode Exit fullscreen mode

The output will be like this:

Data Type: <class 'int'>
Data Type: <class 'float'>
Value: 124.23
Data Type: <class 'float'>
Enter fullscreen mode Exit fullscreen mode

We can clearly see the final variable data type was float.

Python specifically does this to avoid data loss.

It always converts a smaller data type to a larger data type.

If we try to convert str to int, Python throws a TypeError.

integer_num = 123
print("Data Type:", type(integer_num))
test_text = "1.23"
print("Data Type:", type(test_text))

final = test_text + integer_num
Enter fullscreen mode Exit fullscreen mode

Output:

Traceback (most recent call last):
  File "d:\Python- Tutorial\code\implicit.py", line 18, in <module>
    final = test_text + integer_num
           ~~~~~~~~~~~^~~~~~~~~~~~~
TypeError: can only concatenate str (not "int") to str
Enter fullscreen mode Exit fullscreen mode

To solve this specific issue, we can follow the following approach.

Explicit Type Conversion

We convert the data type of an object to the required data type.

Let's use built-in functions like int(), float(), str(), etc. to perform explicit type conversion.

Note: There are many more type conversions available. I have covered a few common data types. You can experiment with those.

This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.

Let's actually solve the previous problem.

integer_num = 123
print("Data Type:", type(integer_num))
test_text = "1.23"
print("Data Type:", type(test_text))
float_text = float(test_text)
print("Data Type:", type(float_text))

final = float_text + integer_num
print("Value:", final)
print("Data Type:", type(final))
Enter fullscreen mode Exit fullscreen mode

Here, I used float() to convert from a string to a float.

Key Points to Remember

  1. Type Conversion is the conversion of an object from one data type to another data type.
  2. Implicit Type Conversion is automatically performed by the Python interpreter.
  3. Python avoids the loss of data in Implicit Type Conversion by choosing a bigger data type.
  4. Explicit Type Conversion is also called Type Casting. The data types of objects are converted using predefined functions by the user.
  5. In Type Casting, loss of data may occur as we enforce the object to a specific data type.

What's Next?

We will use all the learning in a program and make use of everything we have learned in a single program.
I'm building LiveReview, a blast-radius aware AI code review built for your business-critical systems.

Instead of presenting every diff with equal emphasis, LiveReview scores each change by blast radius — how far its impact reaches through your call graph — so you can focus attention where it actually matters.

Spend code review effort where business risk is highest — not spread evenly across every diff.

⭐ Star it on GitHub:

GitHub logo HexmosTech / LiveReview

Blast-Radius Aware AI Code Review for Business-Critical Systems

LiveReview

gitleaks.yml osv-scanner.yml govulncheck.yml semgrep.yml dependabot-enabled mcp-testcases.yml

LiveReview: Blast-Radius Aware AI Code Review for Business-Critical Systems

LiveReview is an AI code reviewer that scores every hunk of a diff by blast radius: how far a change reaches through your call graph, how much persistent state it touches, and how well-tested it is. A 3-line change to a shared auth check can outrank a 300-line UI tweak. Your team's attention goes to the highest-risk code first, not spread evenly across every diff.

blast-radius-demo.mp4

LiveReview's Blast Radius & Review Priority scoring, live in the diff viewer.
















The exact math, not a black box Visualize blast radius at a glance Every factor that feeds the score

How does Blast Radius scoring work? (a more technical explanation)

Here's the goal:

  • A 3-line fix in a function used by 40 other files, that also writes to a database, should score high.
  • A 300-line UI change in one file, fully covered by…




Click below to try LiveReview with your codebase:

LiveReview Banner

Top comments (0)