Practice Questions — Taking Input and Type Conversion
← Back to NotesTopic-Specific Questions
Question 1
Easy
What data type does the input() function always return?
No matter what the user types, input() returns the same type.
The
input() function always returns a string (str).Question 2
Easy
What is the output if the user types 5 and 3?
a = input("Enter a: ")
b = input("Enter b: ")
print(a + b)Remember: input() returns strings. What does + do with strings?
53Question 3
Easy
What is the output?
print(type(input()))Assume the user types: 42
input() always returns the same type.
<class 'str'>Question 4
Easy
What is the output?
x = int("25")
y = float("3.14")
z = str(100)
print(x, type(x))
print(y, type(y))
print(z, type(z))Each conversion function changes the type.
25 <class 'int'>3.14 <class 'float'>100 <class 'str'>Question 5
Medium
What is the output?
print(int(7.9))
print(int(-7.9))int() truncates toward zero, not toward negative infinity.
7-7Question 6
Medium
What happens when this code runs?
x = int("3.14")Can int() handle a string with a decimal point?
It raises a
ValueError: invalid literal for int() with base 10: '3.14'Question 7
Medium
What is the output?
print(int(float("9.75")))Evaluate from the inside out: float() first, then int().
9Question 8
Medium
What is the output?
print(int(True))
print(int(False))
print(float(True))
print(bool(0))
print(bool(""))
print(bool("0"))True is 1, False is 0. For bool(), 0 and empty strings are False, but '0' is a non-empty string.
101.0FalseFalseTrueQuestion 9
Medium
What is the output if the user types
10 20 30?a, b, c = input("Enter three numbers: ").split()
print(a, b, c)
print(type(a))split() returns strings. Does it automatically convert to numbers?
10 20 30<class 'str'>Question 10
Hard
What is the output?
print(int(" 42 "))Does int() handle leading and trailing whitespace?
42Question 11
Hard
What is the output?
x = input() # User types nothing, just presses Enter
print(repr(x))
print(len(x))
print(type(x))What does input() return when the user just presses Enter without typing anything?
''0<class 'str'>Question 12
Hard
What is the output?
print(str(int(float("3.99"))))Evaluate from the innermost function outward.
3Question 13
Medium
What is the difference between int(3.9) and int(-3.9)? Why?
int() truncates toward zero, not toward negative infinity.
int(3.9) gives 3 and int(-3.9) gives -3. The int() function truncates toward zero, meaning it simply removes the decimal part. For positive numbers it rounds down, for negative numbers it rounds up (toward zero).Question 14
Hard
Why does int("3.14") raise a ValueError, but int(3.14) works fine? Both involve the value 3.14.
Think about what int() expects when given a string vs a float.
int(3.14) works because Python knows how to truncate a float to an integer (remove the decimal part, giving 3). But int("3.14") fails because when int() receives a string, it expects the string to contain only digits (optionally with a leading - sign). The decimal point is not a digit, so it raises ValueError.Question 15
Hard
What is the output?
x = "5"
print(x * 3)
print(int(x) * 3)Multiplying a string by a number repeats the string. Multiplying an integer by a number performs arithmetic.
55515Question 16
Hard
What is the output?
print(bool("False"))
print(bool(""))
print(bool(" "))Any non-empty string is truthy, even if it contains the word 'False'.
TrueFalseTrueQuestion 17
Easy
What is the output?
print(str(42) + str(8))Both values are converted to strings first.
428Question 18
Medium
What is the difference between split() and map() when taking multiple inputs?
split() divides the string, map() applies a function to each part.
split() divides a string into a list of substrings based on a separator (default: space). map() applies a function to every element of an iterable. When combined as map(int, input().split()), split() breaks the input into strings, and map(int, ...) converts each string to an integer.Mixed & Application Questions
Question 1
Easy
What is the output if the user types
Priya?name = input("Name: ")
print("Hello, " + name + "!")String concatenation with the + operator.
Hello, Priya!Question 2
Easy
Write a program that asks the user for their name and age, then prints: 'My name is [name] and I am [age] years old.'
Use input() for both values. Age does not need conversion since you are just printing it.
name = input("Enter your name: ")
age = input("Enter your age: ")
print("My name is", name, "and I am", age, "years old.")Question 3
Medium
What is the output if the user types 8?
num = input("Number: ")
result = num * 3
print(result)
print(type(result))num is a string. What does * do with a string and an integer?
888<class 'str'>Question 4
Medium
Write a program that asks the user for the radius of a circle and prints the area. Use pi = 3.14159. The formula is: area = pi * radius^2.
Use float(input()) for the radius since it could be a decimal.
radius = float(input("Enter the radius: "))
pi = 3.14159
area = pi * radius ** 2
print("Area of the circle:", area)Question 5
Medium
What is the output?
a = "10"
b = "3"
print(a + b)
print(int(a) + int(b))
print(a * int(b))Line 1: string + string. Line 2: int + int. Line 3: string * int.
10313101010Question 6
Medium
What is the output?
x = float("10")
print(x)
print(x == 10)
print(x == 10.0)float('10') gives 10.0. Does 10.0 equal 10?
10.0TrueTrueQuestion 7
Hard
Explain why eval() is dangerous to use with input(). Give a specific example of what could go wrong.
eval() executes any Python code the user types.
eval() evaluates a string as a Python expression and executes it. If used with input(), a malicious user could type any Python code instead of a number. For example, typing __import__('os').system('rm -rf /') would attempt to delete all files on the system. Even typing __import__('os').listdir('.') would expose the file system.Question 8
Hard
What is the output?
a = "5"
b = 2
print(a + str(b))
print(int(a) + b)
print(float(a) + b)Line 1: string + string. Line 2: int + int. Line 3: float + int.
5277.0Question 9
Hard
What is the output?
x = int(" -42 ")
y = float(" 3.14 ")
print(x + y)
print(type(x + y))int() and float() strip whitespace. What type do you get when adding int and float?
-38.86<class 'float'>Question 10
Hard
What is the output?
print(int(True) + int(True) + int(False))
print(str(True) + str(False))
print(float(False))int(True) = 1, int(False) = 0. str(True) = 'True'.
2TrueFalse0.0Question 11
Medium
Write a program that takes a temperature in Fahrenheit from the user and converts it to Celsius. Formula: C = (F - 32) * 5/9.
Use float(input()) since temperature can be a decimal.
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5 / 9
print(fahrenheit, "F =", round(celsius, 2), "C")Question 12
Hard
What is the output?
x = "100"
print(x > "99")
print(int(x) > 99)String comparison is lexicographic (character by character), not numeric.
FalseTrueMultiple Choice Questions
MCQ 1
What does input() always return in Python?
Answer: C
C is correct. The
C is correct. The
input() function always returns a string, regardless of what the user types. If the user types 42, it returns "42" (a string). Option D is a common misconception; Python does not automatically detect the type.MCQ 2
How do you take an integer input from the user?
Answer: A
A is correct.
A is correct.
int(input()) first gets the string from input(), then converts it to an integer with int(). Option B would pass an integer (0) as the prompt, which is a TypeError. Option C is invalid (no such function). Option D is invalid syntax.MCQ 3
What is the output of: print(type("42"))?
Answer: B
B is correct.
B is correct.
"42" is a string because it is enclosed in quotes. The type is str. It does not matter that the characters form a number; the quotes make it a string.MCQ 4
What does int(3.7) return?
Answer: C
C is correct.
C is correct.
int() truncates toward zero, meaning it removes the decimal part without rounding. int(3.7) gives 3, not 4. Option A would be the result of rounding, but int() truncates.MCQ 5
What is the result of: '5' + '3'?
Answer: C
C is correct. When both operands are strings, the
C is correct. When both operands are strings, the
+ operator performs concatenation: '5' + '3' = '53'. Option A (8) would be the result if both were integers.MCQ 6
Which of the following will cause a ValueError?
Answer: C
C is correct.
C is correct.
int("3.14") raises a ValueError because the string contains a decimal point, which int() cannot handle when parsing a string. Option B int(3.14) works fine (truncates to 3) because it is converting a float, not a string.MCQ 7
What is the output of: print(bool('0'))?
Answer: A
A is correct.
A is correct.
bool('0') returns True because '0' is a non-empty string. Python checks if the string is empty, not if its content represents zero. Only bool('') (empty string) returns False.MCQ 8
What does the split() method do?
Answer: B
B is correct.
B is correct.
split() splits a string into a list of substrings based on a delimiter (default: whitespace). "10 20 30".split() returns ["10", "20", "30"]. It does NOT convert to numbers (option C) or work on numbers (option A).MCQ 9
What is the output of: print(float(10))?
Answer: B
B is correct.
B is correct.
float(10) converts the integer 10 to the float 10.0. Python represents this as 10.0, not 10.00. The float() function adds the minimum necessary decimal representation.MCQ 10
Why is eval(input()) considered dangerous?
Answer: C
C is correct.
C is correct.
eval() evaluates any Python expression, meaning a user could execute harmful code like file deletion or data theft. Options A and B are incorrect (eval is reasonably fast and handles any expression). Option D is wrong (eval does return a value).MCQ 11
What is int(-2.9)?
Answer: B
B is correct.
B is correct.
int() truncates toward zero. For -2.9, truncating toward zero gives -2 (not -3). This is the opposite of floor division, where -2.9 // 1 would give -3.0. Remember: int() always moves toward zero.MCQ 12
What is the output of: print(int(float(' -3.75 ')))?
Answer: B
B is correct. Step by step:
B is correct. Step by step:
float(' -3.75 ') strips whitespace and gives -3.75. Then int(-3.75) truncates toward zero, giving -3. Option A (-4) would be the floor, not the truncation.MCQ 13
What is the output of: print('5' > '30')?
Answer: A
A is correct. String comparison is lexicographic (character by character). Python compares the first characters: '5' vs '3'. Since '5' comes after '3' in ASCII/Unicode,
A is correct. String comparison is lexicographic (character by character). Python compares the first characters: '5' vs '3'. Since '5' comes after '3' in ASCII/Unicode,
'5' > '30' is True. Numerically 5 < 30, but string comparison does not work numerically. This is why proper type conversion matters.MCQ 14
What does map(int, ['1', '2', '3']) produce?
Answer: B
B is correct.
B is correct.
map() returns a map object (an iterator), not a list. It lazily applies int() to each element when iterated. To get a list, you would need list(map(int, ['1', '2', '3'])). However, when used with unpacking like a, b, c = map(...), the values are extracted correctly.MCQ 15
Which of the following will NOT raise a ValueError?
Answer: C
C is correct.
C is correct.
int(' 42 ') works because int() automatically strips leading and trailing whitespace. Option A fails (empty string). Option B fails (decimal point in string). Option D fails ('hello' is not a number).MCQ 16
What is the output of: print(bool(''), bool(' '), bool('0'), bool(0))?
Answer: B
B is correct.
B is correct.
bool('') = False (empty string). bool(' ') = True (string with a space is non-empty). bool('0') = True (non-empty string, even though it contains '0'). bool(0) = False (integer zero is falsy).MCQ 17
What is the output of: print(str(5) + str(3.14))?
Answer: B
B is correct.
B is correct.
str(5) gives "5" and str(3.14) gives "3.14". String concatenation: "5" + "3.14" = "53.14". This is string joining, not arithmetic.MCQ 18
Which function converts a value to a floating-point number?
Answer: C
C is correct.
C is correct.
float() converts values to floating-point numbers. int() converts to integers, str() converts to strings, and decimal() is not a built-in function (though there is a Decimal class in the decimal module).MCQ 19
What is the output when the user enters 'hello'?
x = int(input())Answer: C
C is correct.
C is correct.
int("hello") raises a ValueError: invalid literal for int() with base 10: 'hello'. The string "hello" cannot be converted to an integer because it does not represent a number.MCQ 20
What is the output of: print(int(True) + float(False) + bool(1))?
Answer: A
A is correct.
A is correct.
int(True) = 1, float(False) = 0.0, bool(1) = True. Now: 1 + 0.0 = 1.0 (float), 1.0 + True = 1.0 + 1 = 2.0 (True is treated as 1 in arithmetic). The result is float because of the float(False) in the chain.Coding Challenges
Challenge 1: Greeting Program
EasyWrite a program that asks the user for their name and city, then prints: 'Hello, [name] from [city]! Welcome to Python.'
Sample Input
Name: Ananya
City: Mumbai
Sample Output
Hello, Ananya from Mumbai! Welcome to Python.
Use input() with descriptive prompts. Use string concatenation or comma-separated print.
name = input("Enter your name: ")
city = input("Enter your city: ")
print("Hello,", name, "from", city + "! Welcome to Python.")Challenge 2: Rectangle Area Calculator
EasyWrite a program that takes the length and width of a rectangle from the user and prints the area and perimeter.
Sample Input
Length: 10
Width: 5
Sample Output
Area: 50.0
Perimeter: 30.0
Use float(input()) since dimensions can be decimals.
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
perimeter = 2 * (length + width)
print("Area:", area)
print("Perimeter:", perimeter)Challenge 3: Bill Splitter
MediumAarav and his friends went to a restaurant. Write a program that takes the total bill amount and the number of people, then calculates how much each person pays (rounded to 2 decimal places).
Sample Input
Total bill: 1573
Number of people: 7
Sample Output
Each person pays: 224.71
Use float() for bill, int() for people. Round to 2 decimal places.
bill = float(input("Enter total bill: "))
people = int(input("Enter number of people: "))
each_pays = round(bill / people, 2)
print("Each person pays:", each_pays)Challenge 4: Percentage Calculator
MediumWrite a program that takes marks obtained and total marks from the user, calculates the percentage, and prints it rounded to 2 decimal places.
Sample Input
Marks obtained: 437
Total marks: 500
Sample Output
Percentage: 87.4%
Use float(input()) for both values. Print with % sign.
obtained = float(input("Enter marks obtained: "))
total = float(input("Enter total marks: "))
percentage = (obtained / total) * 100
print("Percentage:", str(round(percentage, 2)) + "%")Challenge 5: Time Converter
MediumWrite a program that takes a number of seconds from the user and converts it to hours, minutes, and remaining seconds. For example, 3672 seconds = 1 hour, 1 minute, and 12 seconds.
Sample Input
Enter seconds: 3672
Sample Output
1 hours, 1 minutes, 12 seconds
Use // and % operators. Take input as int.
total_seconds = int(input("Enter seconds: "))
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
print(hours, "hours,", minutes, "minutes,", seconds, "seconds")Challenge 6: Two-Number Calculator with Menu
HardWrite a program that takes two numbers from the user and displays the results of all arithmetic operations: addition, subtraction, multiplication, division, floor division, modulus, and exponentiation.
Sample Input
Enter first number: 17
Enter second number: 5
Sample Output
17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3.4
17 // 5 = 3
17 % 5 = 2
17 ** 5 = 1419857
Use int(input()) for both numbers. Display results for all 7 arithmetic operators.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a, "+", b, "=", a + b)
print(a, "-", b, "=", a - b)
print(a, "*", b, "=", a * b)
print(a, "/", b, "=", a / b)
print(a, "//", b, "=", a // b)
print(a, "%", b, "=", a % b)
print(a, "**", b, "=", a ** b)Challenge 7: Swap Values Using Arithmetic
HardTake two numbers from the user and swap their values without using a third variable. Print the values before and after swapping. Use arithmetic operators only.
Sample Input
Enter a: 15
Enter b: 30
Sample Output
Before swap: a = 15, b = 30
After swap: a = 30, b = 15
Do not use a third variable or Python's tuple swap (a, b = b, a).
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("Before swap: a =", a, ", b =", b)
a = a + b
b = a - b
a = a - b
print("After swap: a =", a, ", b =", b)Challenge 8: Multiple Input CGPA Calculator
HardTake 5 subject grades (as floats on one line, space-separated) from the user using split() and map(). Calculate and print the CGPA (average of all grades) rounded to 2 decimal places.
Sample Input
Enter 5 grades: 8.5 9.0 7.5 8.0 9.5
Sample Output
Grades: 8.5 9.0 7.5 8.0 9.5
CGPA: 8.5
Take all 5 inputs on a single line using split() and map(). Round CGPA to 2 decimal places.
g1, g2, g3, g4, g5 = map(float, input("Enter 5 grades: ").split())
print("Grades:", g1, g2, g3, g4, g5)
cgpa = (g1 + g2 + g3 + g4 + g5) / 5
print("CGPA:", round(cgpa, 2))Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn Python with a live mentor?
Explore our Python course