Variables, Data Types, and Operators
# CHAPTER 4
Variables, Data Types, and Operators
1. Chapter Introduction
In Data Science, you must know exactly what type of data you are working with. You cannot perform mathematical operations on text, and you cannot easily parse a number without converting it to text first. This chapter explores Python's core primitive data types and the mathematical and comparison operators used to manipulate them.2. Core Data Types
Python has four primary primitive data types. You can always check a variable's type using the built-in type() function.
3. Type Conversion (Casting)
Often, data comes from a CSV file in the wrong format (e.g., a number is read as a string). You must convert (cast) it before doing math.
4. Arithmetic Operators
Python acts as a powerful calculator using standard arithmetic operators.
*Note on Modulus (%):* This is heavily used in data science to find even/odd numbers. If x % 2 == 0, the number is even.
5. Comparison Operators
Comparison operators compare two values and ALWAYS return a Boolean (True or False). These are critical for filtering data.
6. Logical Operators
Logical operators are used to combine multiple comparison statements.
-
and: Returns True if BOTH statements are true.
-
or: Returns True if ONE of the statements is true.
-
not: Reverses the result (True becomes False).
7. Mini Project: Interactive Profit Calculator
Let's use input() (from Ch 3) and type casting to build a calculator.
8. Common Mistakes
-
Confusing
=with==: A single equals sign=*assigns* a value (age = 20). Double equals==*compares* values (age == 20). Using=when you mean==is a very common beginner error.
-
Forgetting to cast input: Doing
math = input("Enter number: ") + 10will crash because Python tries to add text and a number. You must doint(input()).
9. MCQs
Which data type represents decimal numbers in Python?
What is the output of type(True)?
How do you convert the integer 45 to the string "45"?
What does the // operator do?
What does the Modulus operator % return?
What is the difference between = and ==?
If x = 5, what does (x > 3) and (x < 10) evaluate to?
What happens if you execute "10" + 10?
Which logical operator reverses a boolean value?
Standard division (/) in Python always returns what data type, even if the result is a whole number?
10. Interview Questions
-
Q: You import a CSV containing temperatures, and they all read as strings like
"98.6". Write the code to convert the variabletemp_strinto a format you can use for math.
-
Q: Explain how you would use the modulus operator (
%) to determine if a customer ID number is even or odd.
11. Summary
Data in Python relies on four primitive types:int, float, str, and bool. You can seamlessly cast between these types using functions like str() and float(). Math relies on arithmetic operators (+, -, *, /, , %), while logical decisions are made using comparison operators (==, >, <) combined with and/or.
12. Next Chapter Recommendation
In Chapter 5: Conditional Statements and Loops**, we will use our new comparison operators to control the flow of our program usingif statements, and automate repetitive tasks using for loops.