Skip to main content
Python for Data Science
CHAPTER 04 Beginner

Variables, Data Types, and Operators

Updated: May 18, 2026
5 min read

# 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.

python
123456789101112131415
# 1. Integer (Whole numbers, positive or negative)
quantity = 50
print(type(quantity))  # Output: <class 'int'>

# 2. Float (Decimal numbers)
price = 19.99
print(type(price))     # Output: <class 'float'>

# 3. String (Text, wrapped in single or double quotes)
product_id = "ITEM-4421"
print(type(product_id)) # Output: <class 'str'>

# 4. Boolean (True or False - MUST be capitalized)
is_active = True
print(type(is_active))  # Output: <class 'bool'>

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.

python
12345678910111213141516
# A number stored as a string
string_num = "100"

# This will cause an error: string_num + 50 

# Convert string to integer
real_num = int(string_num)
print(real_num + 50)  # Output: 150

# Convert integer to string
new_string = str(real_num) + " dollars"
print(new_string)     # Output: 100 dollars

# Convert integer to float
decimal_num = float(real_num)
print(decimal_num)    # Output: 100.0

4. Arithmetic Operators

Python acts as a powerful calculator using standard arithmetic operators.

python
12345678910
x = 10
y = 3

print(x + y)  # Addition (13)
print(x - y)  # Subtraction (7)
print(x * y)  # Multiplication (30)
print(x / y)  # Division (3.333) -> ALWAYS returns a float
print(x // y) # Floor Division (3) -> Chops off the decimal, returns integer
print(x ** y) # Exponentiation (10 to the power of 3 = 1000)
print(x % y)  # Modulus (1) -> Returns the remainder of division

*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.

python
12345678
a = 10
b = 20

print(a == b)  # Equal to (False) - Notice it is TWO equals signs!
print(a != b)  # Not equal to (True)
print(a > b)   # Greater than (False)
print(a < b)   # Less than (True)
print(a >= 10) # Greater than or equal to (True)

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).

python
12345678910
age = 25
income = 60000

# Using 'and'
is_target_demographic = (age > 18) and (income > 50000)
print(is_target_demographic) # True

# Using 'or'
is_student_or_rich = (age < 22) or (income > 100000)
print(is_student_or_rich) # False

7. Mini Project: Interactive Profit Calculator

Let's use input() (from Ch 3) and type casting to build a calculator.

python
123456789101112131415
# 1. Get input (Remember, input() returns a STRING)
revenue_str = input("Enter total revenue: ")
expenses_str = input("Enter total expenses: ")

# 2. Cast strings to floats for math
revenue = float(revenue_str)
expenses = float(expenses_str)

# 3. Calculate
profit = revenue - expenses
profit_margin = (profit / revenue) * 100

# 4. Output
print(f"Total Profit: ${profit}")
print(f"Profit Margin: {profit_margin}%")

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: ") + 10 will crash because Python tries to add text and a number. You must do int(input()).

9. MCQs

Question 1

Which data type represents decimal numbers in Python?

Question 2

What is the output of type(True)?

Question 3

How do you convert the integer 45 to the string "45"?

Question 4

What does the // operator do?

Question 5

What does the Modulus operator % return?

Question 6

What is the difference between = and ==?

Question 7

If x = 5, what does (x > 3) and (x < 10) evaluate to?

Question 8

What happens if you execute "10" + 10?

Question 9

Which logical operator reverses a boolean value?

Question 10

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 variable temp_str into 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 using if statements, and automate repetitive tasks using for loops.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·