In data analysis, precision is everything. Whether you are calculating the interest on a loan or the average temperature of a city, you must understand how Python handles numbers and, more importantly, how to convert between different data types to ensure your calculations are accurate.
1. Types of Numbers in Python
Python primarily uses three types of numbers, but as a data analyst, you will spend 99% of your time with the first two.
int): Whole numbers without a decimal point (e.g., 10, -5, 1000). Use these for discrete counts, like the number of transactions or the number of website visitors.float): Numbers that contain a decimal point (e.g., 10.5, -0.01, 3.14). Floats are essential for continuous measurements like currency, weight, or percentages.complex): Written with a "j" as the imaginary part (e.g., 3 + 5j). These are rarely used in standard data analysis but are vital in advanced engineering or physics simulations.2. Arithmetic Operations
Python follows standard mathematical order of operations (BODMAS/PEMDAS).
+) / Subtraction (-)*) / Division (/)//): Returns the largest possible integer (removes the decimal).7 // 2 results in 3.%): Returns the remainder of a division.x % 2 == 0).**): Raises a number to a power.2 ** 3 (2 cubed) results in 8.3. What is Type Casting?
Type Casting is the process of converting a variable from one data type to another.
In data analysis, casting is a mandatory skill. When you import data from a text file or a website, numbers often arrive as "Strings" (text). You cannot perform math on a string; you must first cast it into a numeric type.
int(): Converts a value to an integer. If casting a float, it truncates the decimal (it doesn't round; it just drops the decimal).float(): Converts a value to a decimal number.str(): Converts a number (or any type) into a string.4. Practical Data Analysis Scenarios
Here is how you will apply casting in your real-world analysis:
A. Converting Strings to Numbers
If your raw data says "150" (text) and you need to calculate tax:
Python
price_text = "150" price_numeric = int(price_text) total = price_numeric * 1.05 # Now math is possible
B. Ensuring Precision in Division
Even if you divide two integers, Python 3 automatically "casts" the result to a float to prevent data loss.
4 / 2 results in 2.0 (Float)5 / 2 results in 2.5 (Float)C. Formatting Output
If you want to print a final report, you must cast your numeric results back into strings to combine them with text:
Python
sales = 5000
print("Total Sales for today: " + str(sales))
5. Potential Pitfalls in Casting
int("Apple") will cause a ValueError.int(9.99)), Python does not round up to 10. It returns 9. Always use the round() function if you need mathematical rounding." 100 ". While int() is smart enough to handle some spaces, it is safer to use the .strip() method before casting.