Tuesday, 8 July 2025

input() Function and Type Conversion in Python

input() Function and Type Conversion in Python

🔶 1. input() Function – Taking Input from the User

In many real-world programs, you don't just hard-code data — you take it from users. This is where the input() function comes in.

✅ What is input()?

It is a built-in function used to take input from the user.

Whatever the user types is always treated as a string (text) — even if it's a number.

🔸 Syntax:

variable = input("Message to user: ")

🔸 Example:

name = input("Enter your name: ")
print("Hello", name)

🔹 Output:

Enter your name: Milan
Hello Milan

🔶 2. Behavior of input() – Always Returns a String

Let's try this:

age = input("Enter your age: ")
print(age)
print(type(age))

Even if you type 25, Python will treat it as '25' — a string, not a number.

🔶 3. Type Conversion – Changing the Type of a Value

Since input() gives us a string, we must convert it to other data types if needed (like int or float).

This is called type conversion or type casting.

✅ Common Type Conversion Functions

Function Converts to Example Output Type
int() Integer (whole number) int("5") <class 'int'>
float() Floating point (decimal) float("3.14") <class 'float'>
str() String (text) str(100) <class 'str'>
bool() Boolean (True/False) bool(1) <class 'bool'>
list() List list("abc") ['a', 'b', 'c']

🔶 4. Examples of Type Conversion with input()

🔸 Example 1: Add two numbers entered by the user

a = input("Enter first number: ")
b = input("Enter second number: ")

# Without conversion
print("Sum:", a + b)  # Output: '105' if input is 10 and 5 (string concatenation)

# With conversion
a = int(a)
b = int(b)
print("Correct Sum:", a + b)  # Output: 15

🔸 Example 2: Convert input to float

height = float(input("Enter your height in meters: "))
print("Your height is:", height, "meters")

🔸 Example 3: Boolean conversion

response = input("Do you like Python? (yes/no): ")

if response.lower() == "yes":
    print(True)
else:
    print(False)

🔶 5. Chaining Input and Conversion Together

Instead of writing in two steps:

x = input("Enter a number: ")
x = int(x)

You can do it in one line:

x = int(input("Enter a number: "))

✅ This is cleaner and more efficient.

🔶 6. type() Function – Check Data Type

Use the type() function to check what type of data you are working with.

🔸 Example:

x = input("Enter something: ")
print(type(x))  # Always <class 'str'>

y = int(input("Enter a number: "))
print(type(y))  # <class 'int'>

🟦 Real-life Use Cases in Data Science

Scenario Use of Input Type Conversion Required
User enters age input() Convert to int()
User enters product price input() Convert to float()
Ask user for yes/no feedback input() Compare as str.lower()
Enter list of items separated by commas input().split(",") Convert to list()

🧠 Key Points to Remember

  • input() always returns a string.
  • Use int(), float(), or bool() to convert data to other types.
  • Always validate or check input if necessary.
  • Use type() function to check the data type.
  • You can combine input() and type conversion in a single line.

📝 Practice Questions

  1. Take two numbers as input and print their multiplication.
  2. Ask user to enter their full name and print its length.
  3. Write a program to convert temperature from Celsius to Fahrenheit using:
    F = C × 9/5 + 32
  4. Ask user for age and check if the person is eligible to vote (age ≥ 18).

No comments:

Post a Comment

Total Pageviews

Search This Blog

Write a program which performs the following operations using a simple queue. : insert() -> delete() -> display()

Write a program which performs the following operations using a simple queue. : insert() -> delete() -> display() ...