Numbers and Type Casting in Python

Python supports different types of numbers such as integers, floating-point numbers, and complex numbers. Type casting is used to convert one data type into another.

💡 Numbers are widely used in calculations, marks, age, prices, and more.

Types of Numbers

Python mainly supports the following numeric data types:

  • int – Whole numbers (10, -5, 100)
  • float – Decimal numbers (3.14, 0.5)
  • complex – Numbers with real and imaginary parts
a = 10
b = 3.5
c = 2 + 3j

print(type(a))
print(type(b))
print(type(c))

Basic Arithmetic Operations

Python supports all common arithmetic operations:

x = 10
y = 3

print(x + y)   # Addition
print(x - y)   # Subtraction
print(x * y)   # Multiplication
print(x / y)   # Division
print(x % y)   # Modulus
print(x ** y)  # Power

Type Casting

Type casting is the process of converting one data type into another. Python provides built-in functions for casting.

🔁 Type casting is commonly used when taking user input.

int() Casting

x = "10"
y = int(x)
print(y + 5)

float() Casting

x = "3.5"
y = float(x)
print(y + 1.5)

str() Casting

age = 18
print("I am " + str(age) + " years old")
❌ Invalid conversions will raise errors (e.g., int("abc"))

Practice

  1. Convert a float to an integer
  2. Add two numbers taken as input from the user
  3. Print your age using string casting