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.
- Integers (
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. - Floats (
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 Numbers (
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).
- Addition (
+) / Subtraction (-) - Multiplication (
*) / Division (/) - Floor Division (
//): Returns the largest possible integer (removes the decimal). - Example:
7 // 2results in3. - Modulus (
%): Returns the remainder of a division. - Data Use Case: Determining if a number is even or odd (
x % 2 == 0). - Exponentiation (
**): Raises a number to a power. - Example:
2 ** 3(2 cubed) results in8.
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.
The Core Casting Functions:
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 / 2results in2.0(Float)5 / 2results in2.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
- Incompatible Data: You cannot cast a string containing letters into a number.
int("Apple")will cause aValueError. - Loss of Data: When casting a float to an integer (e.g.,
int(9.99)), Python does not round up to 10. It returns9. Always use theround()function if you need mathematical rounding. - Leading/Trailing Spaces: Often, data from CSVs has spaces like
" 100 ". Whileint()is smart enough to handle some spaces, it is safer to use the.strip()method before casting.