Python Numbers
Study Python numerical types: integers (int), decimals (float), scientific notations, and basic operations.
1. The Three Number Types
Python classifies numbers into three main classes:
- Integer (
int): Whole numbers, positive or negative, of unlimited length. E.g.,10,-999. - Float (
float): Real numbers containing a decimal point. E.g.,2.5,-8.1. Can also represent scientific numbers with an "e" to indicate power of 10. - Complex (
complex): Written with a "j" as the imaginary part. E.g.,3+5j. (Useful in advanced physics and graphics programming).
2. Integers vs. Floats
Arithmetic rules dictate how data types convert. Mixing integers and floats in operations automatically converts the result to a float type: 5 + 2.0 yields float 7.0.
💻 Python Code Examples
Example 1: Integer Numbers
x = 1
y = -3579
z = 99999999999
print(type(x), type(y), type(z))
Description: Declares various positive and negative whole numbers, all classified as int.
Example 2: Float Numbers
x = 1.10
y = -35.59
z = 12.0
print(type(x), type(y), type(z))
Description: Declares decimal values. The presence of decimal points yields float types.
Example 3: Scientific Floats
x = 35e3 # 35 * 10^3
y = 12E4
z = -87.7e100
print(x, type(x))
Description: Uses e or E to write scientific power-of-10 numbers, yielding floats.
Example 4: Complex Numbers
c = 3 + 5j
print(c)
print(type(c))
Description: Declares complex numbers using standard algebraic imaginary 'j' notation.
Example 5: Implicit Float Promotion
x = 10
y = 2.5
result = x + y
print(result, type(result))
Description: Adding an integer and a float yields a float type automatically.
Example 6: Exponents Math checks
val = 2 ** 3 # 2^3
print(val, type(val)) # Output: 8, int
Description: Calculates exponents. Integer exponent operations yield integers.
🧠 Active Retrieval Quizzes
Answer the following 10 multiple-choice questions to test your storage strength.
Question 1: Which Python numeric type handles positive or negative whole numbers of unlimited length?
Question 2: What data type is the value: 3.14?
Question 3: What does a scientific float notation like '3e2' represent mathematically?
Question 4: Which character represents the imaginary part of a complex number in Python?
Question 5: What is the data type result of mixing float and integer: 5 + 3.0?
Question 6: What data type is: 12E3?
Question 7: Which of these numbers is classified as a complex type?
Question 8: What is the value of 5.5 // 2 (floor division with float)?
Question 9: What is the data type of the result: 10 // 3?
Question 10: What does the complex class represent in programming?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
print(type(5.5))
Predict the output of the code snippet below. Type the exact output in the console.
print(type(3e2))
Predict the output of the code snippet below. Type the exact output in the console.
print(5 + 5.0)
Predict the output of the code snippet below. Type the exact output in the console.
print(type(2 + 4j))
Predict the output of the code snippet below. Type the exact output in the console.
print(10 - 2.0)
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.
📋 Progress Status
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.