Skip to content
Programs · Program 5-8

Q.Program 5-8: Program to show explicit type casting.

Rajasthan RbseTextbookSubjective· 2mImportance★★★★★est
33% · 16/49 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Explicit type casting uses int(), float() or str() to force a value into the type you need — here a string mark is cast to int for arithmetic, and the float total is cast back to int.

The idea (explicit type casting). Casting is the programmer explicitly changing a value's type by calling a conversion function. You need it whenever the type you have does not support the operation you want — the everyday case being numeric text (e.g. from input()) that must become a real number before arithmetic.

Program

# prog5-8.py
# Program to show explicit type casting
marksStr = "78"              # a string, e.g. as returned by input()
marks = int(marksStr)        # explicit cast: str -> int
bonus = 2.5
totalMarks = marks + bonus   # 78 + 2.5
print(totalMarks)
print(type(totalMarks))
print(int(totalMarks))       # explicit cast: float -> int (truncates)

Expected output

80.5
<class 'float'>
80

Key lines

  • int(marksStr) — the explicit cast that turns the string "78" into the integer 78. Without it, marksStr + bonus would raise TypeError (cannot add str and float). …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.