Specifically, the different types of numbers you can use in Python. Because who doesn’t love a good math lesson?
First up, we have integers. These are just plain old whole numbers without any decimals or fancy stuff like that. For example: 1234567890 is an integer. But 123.456 isn’t because it has a decimal point in there.
To check if something is an integer, you can use the type() function. If it returns “int”, then congratulations! You have yourself an integer. Here’s some code to demonstrate:
# This script checks if a given number is an integer by using the type() function.
# First, we define a variable "num" and assign it the value of 1234567890, which is an integer.
num = 1234567890
# Next, we use the print() function to output the type of the variable "num".
# The type() function returns the data type of a given variable.
# In this case, it should return "int" since "num" is an integer.
print(type(num)) # Output: <class 'int'>
Next, we have floating-point numbers. These are the ones with decimals and fancy stuff like that. For example: 3.14 is a floating-point number. But 3 isn’t because it doesn’t have any decimals in there.
To check if something is a floating-point number, you can use the type() function again. If it returns “float”, then congratulations! You have yourself a floating-point number. Here’s some code to demonstrate:
# Define a variable "num" and assign it the value of 3.14
num = 3.14
# Print the type of "num" using the type() function
print(type(num)) # Output: <class 'float'> - This line prints the type of "num" which is a floating-point number
# To check if something is a floating-point number, you can use the type() function again.
# If it returns "float", then congratulations! You have yourself a floating-point number.
# Here's some code to demonstrate:
# Define a variable "num" and assign it the value of 3
num = 3
# Print the type of "num" using the type() function
print(type(num)) # Output: <class 'int'> - This line prints the type of "num" which is an integer, not a floating-point number.
Finally, we have complex numbers. These are the ones with imaginary parts and fancy stuff like that. For example: 5 + 2j is a complex number (where j represents an imaginary unit). But 7 isn’t because it doesn’t have any imaginary part in there.
To check if something is a complex number, you can use the type() function again. If it returns “complex”, then congratulations! You have yourself a complex number. Here’s some code to demonstrate:
# Define a complex number with an imaginary part
num = 5 + 2j
# Use the type() function to check if the number is a complex number
# If the type is "complex", then it is a complex number
print(type(num)) # Output: <class 'complex'>
Now that we know the different types of numbers in Python, let’s see how we can do basic arithmetic with them. Here are some examples using the four basic operators (addition, subtraction, multiplication, and division):
# Define two variables, num1 and num2, and assign them integer values
num1 = 5 # num1 is an integer with value 5
num2 = 3 # num2 is an integer with value 3
# Perform addition using the + operator and assign the result to a new variable, result1
result1 = num1 + num2 # Addition of num1 and num2, result1 is an integer with value 8
# Print the value of result1 to the console
print(result1) # Output: 8
# Perform subtraction using the - operator and assign the result to a new variable, result2
result2 = num1 - num2 # Subtraction of num1 and num2, result2 is an integer with value 2
# Print the value of result2 to the console
print(result2) # Output: 2
# Perform multiplication using the * operator and assign the result to a new variable, result3
result3 = num1 * num2 # Multiplication of num1 and num2, result3 is an integer with value 15
# Print the value of result3 to the console
print(result3) # Output: 15
# Perform division using the / operator and assign the result to a new variable, result4
result4 = num1 / num2 # Division of num1 and num2, result4 is a floating-point number with value 1.6666666666666667
# Print the value of result4 to the console
print(result4) # Output: 1.6666666666666667
You now know how to work with numbers in Python, including integers, floating-point numbers, and complex numbers.