Bird
0
0

You have a list of mixed numbers: nums = [1, 2.5, 3, 4.75, 5]. You want to create a new list with all numbers converted to integers by dropping decimals. Which code correctly does this?

hard📝 Application Q15 of 15
Python - Data Types as Values

You have a list of mixed numbers: nums = [1, 2.5, 3, 4.75, 5]. You want to create a new list with all numbers converted to integers by dropping decimals. Which code correctly does this?

Anew_nums = [int(n) for n in nums]
Bnew_nums = [float(n) for n in nums]
Cnew_nums = [str(int(n)) for n in nums]
Dnew_nums = [round(n) for n in nums]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to convert all numbers to int by dropping decimals, not rounding.
  2. Step 2: Evaluate each option

    C uses int() which drops decimals correctly. A converts to string, not int. B converts to float, not int. D rounds numbers, which changes values.
  3. Final Answer:

    new_nums = [int(n) for n in nums] -> Option A
  4. Quick Check:

    int() drops decimals, list comprehension applies to all [OK]
Quick Trick: Use int() in list comprehension to drop decimals [OK]
Common Mistakes:
MISTAKES
  • Using round() instead of int()
  • Converting to string instead of int
  • Using float() which keeps decimals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes