-
Classes
-
File operations
-
Comprehensions
-
loops
-
Python Installation
-
Numbers
-
Data Types in Python
-
Operators
-
Sequence Data types part 3 – Tuples
-
Sequence Data types part 2 – Lists
-
Expressions and statements
-
Data Unpacking
-
Functions
-
Dictionaries
-
Sequence Data types part 1-strings
-
if…else
-
Taking Input from the user
-
Python Typecasting
In order to take input from user, we need a function called input()
Let’s say we want to accept the user’s age. Then we will use the input() function as follows:
If we run this code, we get the output as follows:
C:\practice>py 1.py
Enter your age:
After entering the value, the value will be stored in the age variable. But the input() function converts the entered data into string.
This can be an undesired phenomenon if we are dealing with numeric values. To solve this problem, we have to convert the value back to a number using the int() or float() function as required.
If we are dealing with integers, we will use the int() function and for floating-point numbers, we will use the float() function.
Interview Questions
Q. What is the primary function used to obtain user input in Python?
Ans. The primary function for getting user input in Python is input().
Q. Explain the process of accepting the user’s age as input and storing it in a variable.
Ans. Use the input() function along with a prompt (e.g., “Enter your age: “) to guide the user.
Store the input received from the user into a variable, such as age.
Q. What is the default data type of the value returned by the input() function? How can this be potentially problematic when dealing with numeric input?
Ans. The input() function always returns the user’s input as a string. This can be problematic when working with numerical values (integers or floats) because you’ll need to convert the string back to its appropriate numeric type before performing calculations or comparisons.
Q. How can you convert user input that represents an integer or a floating-point number back to its correct data type?
Ans. To convert a string representing an integer into an actual integer, use the int() function.
To convert a string representing a floating-point number into a float, use the float() function.
Coding Problems
Q. You need to write a Python program that calculates the user’s birth year. How would you handle the input and calculations?
Ans.
age_str = input(“Enter your age: “)
age = int(age_str) # Convert age to integer
current_year = 2024 # Assume current year (can be obtained dynamically)
birth_year = current_year – age
print(“You were likely born in:”, birth_year)
Q. Calculate the Area of a Rectangle
Ans.
length_str = input(“Enter the length of the rectangle: “)
width_str = input(“Enter the width of the rectangle: “)
length = float(length_str)
width = float(width_str)
area = length * width
print(“The area of the rectangle is:”, area)
Q. Convert Celsius to Fahrenheit
Ans.
celsius_str = input(“Enter the temperature in Celsius: “)
celsius = float(celsius_str)
fahrenheit = (celsius * 9/5) + 32
print(“The temperature in Fahrenheit is:”, fahrenheit)
Q. Calculate the Sum and Average of Two Numbers
Ans.
num1_str = input(“Enter the first number: “)
num2_str = input(“Enter the second number: “)
num1 = float(num1_str)
num2 = float(num2_str)
sum = num1 + num2
average = sum / 2
print(“The sum of the numbers is:”, sum)
print(“The average of the numbers is:”, average)
Q. Display a Custom Greeting
Ans.
name = input(“Enter your name: “)
print(“Hello,”, name + “!”)
Q. Calculate the Square of a Number
Ans.
number_str = input(“Enter a number: “)
number = float(number_str)
square = number * number
print(“The square of the number is:”, square)
Code snippet based questions
Q.
age_str = input(“Enter your age: “)
age = int(age_str)
next_age = age + 1
print(“Next year, you will be:”, next_age)
What is the purpose of the input() function in the first line?
Why is the int() function used on the age_str variable?
What will the code print if the user enters “35” as their age?
Ans.
The input() function prompts the user to enter their age and stores the input as a string in the age_str variable.
The int() function is used to convert the string stored in age_str into an integer data type, allowing for numerical calculations later.
If the user enters “35”, the code will print “Next year, you will be: 36”.
Q.
price_str = input(“Enter the price of the item: “)
price = float(price_str)
discount = 0.15 * price
final_price = price – discount
print(“The final price after discount is:”, final_price)
What would happen if the user entered “twenty” as the price?
How can you modify the code to ensure the discount is calculated correctly even if the user enters a price with a dollar sign (e.g., “$25.50”)?
Ans.
If the user entered “twenty”, the code would raise a ValueError because the float() function cannot convert the string “twenty” into a floating-point number.
You can modify the code to strip the dollar sign before converting to float:
price_str = input(“Enter the price of the item: “)
price_str = price_str.replace(“$”, “”) # Remove dollar sign
price = float(price_str)
# … (rest of the calculation)
Q. If the user enters “10” for num1_str and “20” for num2_str, what will be the output of the print() statement?
num1_str = input(“Enter the first number: “)
num2_str = input(“Enter the second number: “)
result = num1_str + num2_str
print(“The result is:”, result)
Ans. The output will be “The result is: 1020” because the + operator performs string concatenation when both operands are strings. To get the numerical sum, you need to convert the input strings to numbers using int() or float().