Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 09

Input with Type Casting

Convert keyboard strings to numbers with float() and multiply two values. Several compact input styles.

Beginner15 min

What you will learn

By the end of this lesson, you'll understand:

  • Why must input() be converted before arithmetic?
  • What is separate conversion vs conversion in one line?
  • What are compact ways to print the product of two numbers?
  • When should you use float() and int()?

Why do we need type casting?

In the previous lesson, you learned that input() always returns a string (str).

If the user types 3, Python stores "3".

A string is text, not a number. Before performing arithmetic, we must convert it into a numeric type.

python
a = input("Enter First Value:")
b = input("Enter Second Value:")
Variable Value Type
a "3" str
b "2" str

To multiply numbers, convert them first:

python
x = float(a)
y = float(b)

Your source notes use float() because it supports both whole numbers and decimal numbers.

Understanding type casting

Type casting means converting one data type into another.

float() converts the string "3.5" into the number 3.5

Style 1 — read first, convert later (DataRead3.py)

This is the clearest approach for beginners.

python
# DataRead3.py
a = input("Enter First Value:")
b = input("Enter Second Value:")
# here a, b are of type str and convert them into float
x = float(a)
y = float(b)
z = x * y
print("Mul({},{})={}".format(x, y, z))
text
Enter First Value:3
Enter Second Value:6.5
Mul(3.0,6.5)=19.5
Read "3", convert with float, then multiply to 19.5

This style separates reading, converting, and calculating.

Style 2 — convert while reading (DataRead4.py)

This is the most common practical pattern.

python
# DataRead4.py
x = float(input("Enter First Value:"))
y = float(input("Enter Second Value"))
z = x * y
print("Mul({},{})={}".format(x, y, z))

Instead of writing:

python
a = input()
x = float(a)

we combine them into one line:

python
x = float(input())
User to input() to float() to variable x

float(input(...)) is the usual industry pattern: one line per value.

Style 3 — compute inside print() (DataRead5.py)

You don't always need a separate variable.

python
# DataRead5.py
x = float(input("Enter First Value:"))
y = float(input("Enter Second Value:"))
print("Mul({},{})={}".format(x, y, x * y))

The multiplication happens directly inside print().

Style Calculation
Style 2 z = x * y
Style 3 x * y inside print()

Both produce identical output.

Style 4 — two values after one prompt (DataRead6.py)

python
# DataRead6.py
print("Enter Two Values")
x = float(input())
y = float(input())
print("Mul({},{})={}".format(x, y, x * y))
print("--------------OR-------------------")
print("Mul(%0.2f,%0.2f)=%0.2f" % (x, y, x * y))
print("--------------OR-------------------")
print("Mul({},{})={}".format(x, y, round(x * y, 2)))
text
Enter Two Values
3
6.789
Mul(3.0,6.789)=20.367
--------------OR-------------------
Mul(3.00,6.79)=20.37
--------------OR-------------------
Mul(3.0,6.789)=20.37
Method Result
format() Full value
%0.2f 2 decimal places
round(..., 2) Rounded value

Style 5 — one-line program (DataRead7.py)

This is the shortest version from your source.

python
# DataRead7.py
print("Mul={}".format(float(input("Enter First Value:")) * float(input("Enter Second Value:"))))
print("-------------------OR-----------------------------")
print("Mul=%0.2f" % (float(input("Enter First Value:")) * float(input("Enter Second Value:"))))
input times input, then float, then print

It works correctly, but Style 2 is clearer for learning and maintenance.

float() vs int()

Your notes mention both type casting functions.

Function Input Output
int("10") "10" 10
float("10") "10" 10.0
float("3.14") "3.14" 3.14
python
# DataReadIntFloatEx.py
age = int(input("Enter Age:"))
price = float(input("Enter Price:"))

Use int() for whole numbers and float() for decimal numbers.

round() function

The source also uses round(x * y, 2).

Expression Result
round(12.3456, 2) 12.35
round(8.999, 1) 9.0
round(7.234, 0) 7.0

round(number, digits) limits the displayed precision.

Which style should you use?

Style Best for
Style 1 Learning type casting
Style 2 Recommended (industry default)
Style 3 Small calculations
Style 4 Comparing formatting methods
Style 5 Compact code (less readable)

Recommendation: Style 2 is the clearest and most maintainable approach for everyday Python programs.

Complete practical program (DataReadProductEx.py)

python
# DataReadProductEx.py
x = float(input("Enter First Value: "))
y = float(input("Enter Second Value: "))
product = x * y
print("First Value :", x)
print("Second Value:", y)
print("Product     :", round(product, 2))
text
Enter First Value: 12.5
Enter Second Value: 4
First Value : 12.5
Second Value: 4.0
Product     : 50.0

Key terms

Term Meaning
Type casting Converting one data type into another
float() Converts text into a decimal number
int() Converts text into an integer
round() Rounds a number to specified decimal places
Dynamic input Values entered while the program runs

Summary

  • input() always returns str, so convert it before arithmetic
  • float(input("...")) is the practical default for numeric input
  • Style 1 separates reading and conversion; Style 2 combines them into one line
  • format(), %0.2f, and round() are different ways to control the appearance of the result
  • Style 2 is the most readable and commonly used pattern for real programs

Quick knowledge check

  1. Why do we use float() after input()?
    To convert the returned string into a numeric value.

  2. Which style is the recommended practical default?
    Style 2: float(input("..."))

  3. What does round(x * y, 2) do?
    Rounds the product to 2 decimal places.

  4. Which function should you use for whole numbers only?
    int()

Next lesson: three versions of adding two numbers — hard-coded values vs dynamic keyboard input.