Data Type Conversion and Operators in Python

Data Type Conversion

Understanding how to convert between core data types is essential for data manipulation. Python provides built-in functions for these conversions.

Integer Conversions (int)

Convert int to float:

value_x = 5
result = float(value_x)
print(result)  # Output: 5.0

Convert int to bool: A boolean evaluates to False for zero and True for any other integer.

print(bool(7))   # Output: True
print(bool(0))   # Output: False
print(bool(-3))  # Output: True

Convert int to str:

num = 42
string_num = str(num)
print(type(string_num))  # Output: <class 'str'> (Now a string)

Float Conversions (float)

Convert float to int: Conversion truncates the decimal; it does not perform rounding.

print(int(3.7))   # Output: 3
print(int(9.99))  # Output: 9
print(int(-2.1))  # Output: -2

Convert float to str:

a_float = 5.5
a_string = str(a_float)
print(a_string)  # Output: '5.5'

Convert float to bool: Any non-zero float is True.

print(bool(0.0))  # Output: False
print(bool(3.14)) # Output: True
print(bool(-0.0)) # Output: False

Boolean Conversions (bool)

Convert bool to int:

print(int(True))   # Output: 1
print(int(False))  # Output: 0

Convert bool to float:

print(float(True))  # Output: 1.0
print(float(False)) # Output: 0.0

Convert bool to str:

print(str(True), type(str(True)))  # Output: True <class 'str'>

String Conversions (str)

Convert str to int: The string must represent a valid integer (e.g., '10', '-5').

print(int('25'))  # Output: 25
# print(int('hello')) # This would cause a ValueError.

Convert str to float: Works with strings representing integers or floats.

print(float('12'))     # Output: 12.0
print(float('-3.14'))  # Output: -3.14

Convert str to bool: Only an empty string ('') evaluates to False. A string containing any character, including spaces, evaluatse to True.

print(bool(''))      # Output: False
print(bool('True'))  # Output: True
print(bool(' '))     # Output: True
print(bool('0'))     # Output: True

The None Type

None represents the absence of a value.

null_value = None
print(str(null_value))   # Output: 'None'
print(bool(null_value))  # Output: False

Python Operators

Arithmetic Operators

print(10 + 3)   # Addition: 13
print(10 - 3)   # Subtraction: 7
print(10 * 3)   # Multiplication: 30
print(10 / 3)   # Division: 3.333...
print(10 % 3)   # Modulus (remainder): 1
print(10 ** 2)  # Exponentiation: 100
print(10 // 3)  # Floor division: 3

Comparison Operators

x, y = 15, 25
print(x == y)  # Equal to: False
print(x != y)  # Not equal to: True
print(x > y)   # Greater than: False
print(x < y)   # Less than: True
print(x <= y)  # Less than or equal to: True
print(x >= y)  # Greater than or equal to: False

Assignment Operator

The basic assignment operator (=) sets a variable to a value.

count = 5
# Compound assignment
count += 2  # Equivalent to count = count + 2
print(count)  # Output: 7

Tags: python Data Types Type Conversion Operators Programming Basics

Posted on Fri, 08 May 2026 11:45:07 +0000 by beerman