Type conversion python

Understanding Type Conversion in Python

In Python, you often encounter situations where you need to convert data from one type to another. For example, you might want to convert an immutable tuple into a list so you can modify it. Python provides simple and powerful mechanisms to handle such conversions.


Types of Type Conversion

There are two ways to perform type conversion in Python:

  1. Implicit Type Conversion — Done automatically by Python.

  2. Explicit Type Conversion — Done manually by the programmer using built-in functions.


Implicit Type Conversion

Python automatically performs type conversion when needed to avoid data loss during operations involving mixed types.

Example:

a = 10 b = 1.5 c = a + b print(c) # Output: 11.5 d = True e = a + d print(e) # Output: 11
  • When adding an int and a float, Python promotes the integer to a float.

  • When adding an int and a bool, Python treats True as 1 and False as 0.


Explicit Type Conversion

You can explicitly convert data from one type to another using built-in functions like:

  • int()

  • float()

  • str()

  • list()

  • tuple()

  • set()

Example:

s = "135" i = 10 + int(s) f = float(s) print(i) # Output: 145 print(f) # Output: 135.0

If you attempt to perform arithmetic between incompatible types (e.g. int + str) without conversion, Python will raise an error.


Converting Between Container Types

You can easily convert between lists, tuples, and sets:

s = "blog" print(list(s)) # ['b', 'l', 'o', 'g'] print(tuple(s)) # ('b', 'l', 'o', 'g') print(set(s)) # {'b', 'l', 'o', 'g'}
  • list() and tuple() preserve order.

  • set() removes duplicates and does not preserve order.


String Representation of Containers

You can convert containers and numbers into strings using str():

l = ['a', 'b', 'c'] print(str(l)) # ['a', 'b', 'c'] a = 10 b = 11 print(str(a) + str(b)) # 1011 c = 12.5 print(str(c)) # 12.5

👀Note that string concatenation using + will combine the strings rather than performing numeric addition.


Converting Containers to Lists

You can convert tuples and sets into lists:

t = (10, 20, 30) print(list(t)) # [10, 20, 30] s = {10, 20, 30} print(list(s)) # [10, 20, 30]
  • Tuples maintain order.

  • Sets are unordered, so the resulting list order may vary.


Binary, Octal, and Hexadecimal Conversions

Python makes it easy to convert integers to binary, octal, or hexadecimal:

a = 20 print(bin(a)) # 0b10100 print(hex(a)) # 0x14 print(oct(a)) # 0o24

Prefixes indicate the number system:

  • 0b → binary

  • 0x → hexadecimal

  • 0o → octal


Converting Back to Decimal

To convert binary, octal, or hexadecimal strings back to decimal:

a = "1001" print(int(a, 2)) # 9 b = "12" print(int(b, 8)) # 10 c = "A1" print(int(c, 16)) # 161

Use int() with the appropriate base as the second argument.


Summary

  • Implicit conversion is automatic and seamless.

  • Explicit conversion uses functions like int(), float(), str(), etc.

  • You can convert between container types (lists, tuples, sets) and between numeric bases (binary, octal, hex, decimal).

  • str() is very useful for debugging and preparing data for display.


Popular Posts