Skip to main content

Numbers & Type Casting 🚀

Python Numbers & Type Casting is a core Python concept covering master Python's numeric types (int, float, complex) and learn how to convert between them using explicit and implicit type casting. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.

Mentor's Note: In programming, a 5 (number) and a "5" (text) look similar, but they behave differently. Casting is how we tell Python to switch between these "modes." 💡


🌟 The Scenario: The Currency Exchange 💱

Imagine you are traveling from the US to India.

  • The Logic: You have Dollars ($), but the shop only accepts Rupees (₹). You must go to a counter and convert your money. 📦
  • The Result: The value stays the same (you are just as rich), but the type of currency changes so you can use it. This is Type Casting. ✅

📖 Concept Explanation

1. Python Numeric Types

  • int: Whole numbers. 1, 100, -5.
  • float: Numbers with decimals. 3.14, -0.001.
  • complex: Numbers with an imaginary part. 1+2j.

2. Implicit Casting (Automatic) 🪄

Python automatically converts one type to another if it's safe (e.g., adding an integer to a float).

x = 5 # int
y = 2.5 # float
z = x + y # z becomes 7.5 (Automatic float)

3. Explicit Casting (Manual) 🛠️

You use "Constructor" functions to force a change:

  • int(): Truncates decimals (3.9 -> 3).
  • float(): Adds .0 to integers.
  • str(): Turns numbers into text.

🎨 Visual Logic: The Conversion Flow


💻 Implementation: Casting Lab

# 🛒 Scenario: Calculating a bill from user input
# 🚀 Action: Converting string input to numbers

# User inputs are ALWAYS strings by default 🔡
input_price = "19.99"
input_quantity = "5"

# 🛠️ Explicit Casting
price = float(input_price)
quantity = int(input_quantity)

total = price * quantity

print(f"Grand Total: ₹{total} 🛍️")

📊 Sample Dry Run (Truncation)

VariableInstructionFinal ValueWhy?
xint(5.9)5Decimal is discarded, NOT rounded! ⚠️
yfloat(10)10.0Decimal point added.

📉 Technical Analysis

  • Range: Unlike C++ or Java, Python integers have arbitrary precision, meaning they can grow as large as your computer's memory allows! 🤯

🎯 Practice Lab 🧪

Task: The Price Fixer

Task: You have a price 99.99. Write a program that converts this price to an integer and prints the "discounted" whole-number price. Hint: Use int(). 💡


💡 Pro Tip: "Make it work, make it right, make it fast." - Kent Beck



← Back: Variables | Next: Arithmetic Operators →

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir