Welcome to the Python Numbers tutorial! In this lesson, we'll dive into the world of numbers in Python, which is the fundamental building block for every programming language. Let's get started! 💡
Python has several built-in data types for handling numbers. The primary number types are:
int) - whole numbers (e.g., 123, -45)float) - numbers with decimal points (e.g., 3.14, -12.98)Integers are whole numbers, and they can be positive or negative. To declare an integer in Python, simply assign a whole number to a variable.
my_integer = 123
print(my_integer) # Output: 123Python provides various operations for integers. Here's a list of common integer operations:
+)-)*)/)%) - returns the remainder of a division operation//) - returns the largest integer not greater than the result of the divisionmy_integer1 = 12
my_integer2 = 5
# Examples of integer operations
sum = my_integer1 + my_integer2
difference = my_integer1 - my_integer2
product = my_integer1 * my_integer2
quotient = my_integer1 / my_integer2
remainder = my_integer1 % my_integer2
floor_division = my_integer1 // my_integer2
print(f'Sum: {sum}')
print(f'Difference: {difference}')
print(f'Product: {product}')
print(f'Quotient: {quotient}')
print(f'Remainder: {remainder}')
print(f'Floor Division: {floor_division}')Floating-point numbers represent real numbers with decimal points. To declare a floating-point number in Python, simply assign a number with a decimal point to a variable.
my_float = 3.14
print(my_float) # Output: 3.14Python provides the same set of operations for floating-point numbers as for integers.
my_float1 = 3.14
my_float2 = 2.0
# Examples of floating-point operations
sum_float = my_float1 + my_float2
difference_float = my_float1 - my_float2
product_float = my_float1 * my_float2
quotient_float = my_float1 / my_float2
print(f'Sum: {sum_float}')
print(f'Difference: {difference_float}')
print(f'Product: {product_float}')
print(f'Quotient: {quotient_float}')It's important to note that the // operator for floor division can produce unexpected results with floating-point numbers. To perform floor division on floating-point numbers, use the math.floor() function from the math module.
from math import floor
# Examples of floor division on floating-point numbers
floor_division_float1 = floor(my_float1 // my_float2)
floor_division_float2 = floor(my_float2 // my_float1)
print(f'Floor Division: {floor_division_float1}')
print(f'Floor Division (reverse): {floor_division_float2}')Question: What is the difference between integers and floating-point numbers in Python?
A: They are the same thing. B: Integers can only handle whole numbers, while floating-point numbers can handle real numbers. C: Integers can only handle real numbers, while floating-point numbers can handle whole numbers.
Correct: B Explanation: Integers can only handle whole numbers, while floating-point numbers can handle real numbers with decimal points.
That's it for this lesson on Python numbers! We've covered both integers and floating-point numbers, including their operations and differences. Next up, we'll dive into Python variables and data types! 🚀
Stay tuned, and happy coding! 💡