Lesson 09
Input with Type Casting
Convert keyboard strings to numbers with float() and multiply two values. Several compact input styles.
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()andint()?
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.
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:
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.
Style 1 — read first, convert later (DataRead3.py)
This is the clearest approach for beginners.
# 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))Enter First Value:3
Enter Second Value:6.5
Mul(3.0,6.5)=19.5This style separates reading, converting, and calculating.
Style 2 — convert while reading (DataRead4.py)
This is the most common practical pattern.
# 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:
a = input()
x = float(a)we combine them into one line:
x = float(input())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.
# 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)
# 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)))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.
# 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:"))))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 |
# 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)
# 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))Enter First Value: 12.5
Enter Second Value: 4
First Value : 12.5
Second Value: 4.0
Product : 50.0Key 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 returnsstr, so convert it before arithmeticfloat(input("..."))is the practical default for numeric input- Style 1 separates reading and conversion; Style 2 combines them into one line
format(),%0.2f, andround()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
-
Why do we use
float()afterinput()?
To convert the returned string into a numeric value. -
Which style is the recommended practical default?
Style 2:float(input("...")) -
What does
round(x * y, 2)do?
Rounds the product to 2 decimal places. -
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.