Bird
Raised Fist0
Pythonprogramming~5 mins

Numeric values (int and float behavior) in Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the difference between int and float in Python?

int represents whole numbers without decimals, like 5 or -3.

float represents numbers with decimals, like 3.14 or -0.001.

Click to reveal answer
beginner
What happens when you divide two integers using / in Python?

The result is always a float, even if the division is exact.

Example: 4 / 2 gives 2.0, not 2.

Click to reveal answer
beginner
How can you convert a float to an int in Python?

Use the int() function. It removes the decimal part (truncates).

Example: int(3.9) becomes 3.

Click to reveal answer
beginner
What is the result of int(3.9999) and why?

The result is 3 because int() cuts off the decimal part without rounding.

Click to reveal answer
intermediate
Why can floating point numbers sometimes give unexpected results in calculations?

Because floats are stored in a way that can’t exactly represent some decimal numbers, small rounding errors can happen.

Example: 0.1 + 0.2 might not exactly equal 0.3.

Click to reveal answer
What type does the expression 5 / 2 return in Python?
Abool
Bint
Cstring
Dfloat
What does int(7.8) return?
A7
B8
C7.8
DError
Which of these is a valid float number in Python?
A3
B"3.0"
C3.0
D3,0
What is the output of 0.1 + 0.2 == 0.3 in Python?
ATrue
BFalse
CError
DNone
How do you convert an integer 5 to a float?
Afloat(5)
Bint(5)
Cstr(5)
Dbool(5)
Explain the difference between int and float types in Python and how division behaves with these types.
Think about whole numbers vs numbers with decimals and what happens when you divide.
You got /4 concepts.
    Describe why floating point numbers can cause unexpected results in calculations and how Python stores these numbers.
    Consider how computers store decimal numbers in binary.
    You got /4 concepts.

      Practice

      (1/5)
      1.

      Which of the following is an example of a float in Python?

      1. 42
      2. 3.14
      3. -7
      4. 0
      easy
      A. 42
      B. 3.14
      C. -7
      D. 0

      Solution

      1. Step 1: Understand what a float is

        A float is a number with a decimal point, like 3.14.
      2. Step 2: Identify the float among options

        Only 3.14 has a decimal point, so it is a float.
      3. Final Answer:

        3.14 -> Option B
      4. Quick Check:

        Float = 3.14 [OK]
      Hint: Floats always have a decimal point [OK]
      Common Mistakes:
      • Choosing integers as floats
      • Confusing negative numbers with floats
      • Ignoring decimal points
      2.

      Which of the following is the correct way to convert the float 5.7 to an integer in Python?

      easy
      A. float(5.7)
      B. int(5,7)
      C. int('5.7')
      D. int(5.7)

      Solution

      1. Step 1: Recall how to convert float to int

        Use the int() function with a float value as argument.
      2. Step 2: Check each option

        int(5.7) correctly converts float 5.7 to int 5. int('5.7') causes error, int(5,7) is invalid syntax.
      3. Final Answer:

        int(5.7) -> Option D
      4. Quick Check:

        int(5.7) = 5 [OK]
      Hint: Use int() directly on float to convert [OK]
      Common Mistakes:
      • Trying int() on string with decimal
      • Using wrong syntax like int(5,7)
      • Using float() instead of int()
      3.

      What is the output of the following code?

      x = 7 / 2
      print(type(x))
      print(x)
      medium
      A. <class 'int'>\n3.5
      B. <class 'int'>\n3
      C. <class 'float'>\n3.5
      D. SyntaxError

      Solution

      1. Step 1: Understand division operator in Python 3

        Division with / always returns a float, even if numbers divide evenly.
      2. Step 2: Evaluate the code

        7 / 2 = 3.5, which is a float. So type(x) is <class 'float'> and x is 3.5.
      3. Final Answer:

        <class 'float'>\n3.5 -> Option C
      4. Quick Check:

        7 / 2 = 3.5 float [OK]
      Hint: Division / always returns float in Python 3 [OK]
      Common Mistakes:
      • Assuming integer division with /
      • Confusing type output format
      • Expecting 3 instead of 3.5
      4.

      Find the error in this code snippet:

      num = 4.8
      num_int = int(num)
      print(num_int + ' is the integer value')
      medium
      A. Cannot add int and str directly
      B. No error, code runs fine
      C. Syntax error in print statement
      D. Cannot convert float to int

      Solution

      1. Step 1: Check conversion from float to int

        int(4.8) converts float 4.8 to int 4 without error.
      2. Step 2: Check print statement

        Adding int and string directly causes a TypeError in Python.
      3. Final Answer:

        Cannot add int and str directly -> Option A
      4. Quick Check:

        int + str causes TypeError [OK]
      Hint: Convert int to str before adding to string [OK]
      Common Mistakes:
      • Thinking int() conversion fails
      • Ignoring type mismatch in addition
      • Assuming print syntax error
      5.

      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
      A. new_nums = [int(n) for n in nums]
      B. new_nums = [float(n) for n in nums]
      C. new_nums = [str(int(n)) for n in nums]
      D. new_nums = [round(n) for n in nums]

      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]
      Hint: Use int() in list comprehension to drop decimals [OK]
      Common Mistakes:
      • Using round() instead of int()
      • Converting to string instead of int
      • Using float() which keeps decimals