Python Basics: Variables, Data Types & Operators

35 mintext

Theory & Concepts

Python Basics: Your Data Science Foundation

Python is the most popular language for data science because it's simple, readable, and has powerful libraries. Before diving into data analysis, you need to master the fundamentals.

💡 Why This Matters: These basics are the foundation of every data science script you'll write. Master them now, and everything else becomes easier!

Variables: Storing Your Data

Variables are named containers that store data. Think of them as labeled boxes where you can put information and retrieve it later.

Creating Variables

Python uses simple syntax - no type declarations needed:

python
# Create variables (Python figures out the type automatically)
student_name = "Alice"
student_age = 25
student_gpa = 3.85
is_enrolled = True

Variable Naming Rules

Follow these conventions for clean, professional code:

  1. Use descriptive names:

    • customer_revenue (clear meaning)
    • x (unclear)
  2. Use snake_case for multi-word names:

    • total_sales (Python style)
    • totalSales (JavaScript style)
  3. Start with letter or underscore:

    • _private_data
    • 2nd_quarter (can't start with number)
  4. Avoid Python keywords:

    • class, def, import (reserved)
    • class_name, definition, import_data

⚠️ Important: Variable names are case-sensitive! sales and Sales are different variables.

Data Types: Understanding Your Data

Python has several built-in data types that you'll use constantly in data science:

Numeric Types

Integers (int) - Whole numbers:

python
count = 100
year = 2024
negative = -50

Floats (float) - Decimal numbers:

python
price = 99.99
temperature = -3.5
pi = 3.14159

Text Type

Strings (str) - Text data:

python
name = "Data Science"
description = 'Python for data analysis'
multiline = """This is
a multiline
string"""

Boolean Type

Boolean (bool) - True or False:

python
is_valid = True
has_error = False

Checking Types

Use type() to check a variable's type:

python
age = 25
print(type(age)) # <class 'int'>

Summary

Key Takeaways:

  1. Variables store data with descriptive names (use snake_case)
  2. Main types: int, float, str, bool
  3. Operators: arithmetic (+, -, *, /), comparison (==, !=, <, >), logical (and, or, not)
  4. Type conversion: int(), float(), str() for transforming data
  5. I/O: print() for output, input() for getting user data

Data Science Connection:

  • Variables store dataset names, parameters, and results
  • Type conversions are essential when loading data from files
  • Operators form the basis of data transformations and filtering

Next Lesson: Control Flow - making decisions and repeating operations!

Lesson Content

Master Python fundamentals including variables, data types, operators, type conversion, and input/output - the building blocks of data science programming.

Code Example

python
# Python Basics: Variables, Data Types & Operators
# Complete hands-on examples for data science
print("="*80)
print("PYTHON BASICS FOR DATA SCIENCE")
print("="*80)
print()
# SECTION 1: Variables and Assignment
print("SECTION 1: Variables and Assignment")
print("-" * 80)
# Create variables with different types
dataset_name = "Customer Sales Data"
num_records = 10000
avg_purchase = 149.99
is_processed = False
print("Dataset Information:")
print(" Name:", dataset_name)
print(" Records:", num_records)
print(" Average Purchase: $%.2f" % avg_purchase)
print(" Processed:", is_processed)
print()
# SECTION 2: Data Types in Action
print("SECTION 2: Data Types in Action")
print("-" * 80)
# Integer examples
customers_count = 5000
print("Customers:", customers_count, "(type:", type(customers_count).__name__ + ")")
# Float examples
conversion_rate = 0.0345
print("Conversion Rate: %.2f%%" % (conversion_rate * 100))
# String examples
product_name = "Data Science Pro"
print("Product:", product_name)
# Boolean examples
has_premium = True
print("Premium Member:", has_premium)
print()
# SECTION 3: Arithmetic Operators
print("SECTION 3: Arithmetic Operators")
print("-" * 80)
price = 99.99
quantity = 5
tax_rate = 0.08
subtotal = price * quantity
tax = subtotal * tax_rate
total = subtotal + tax
print("Price: $%.2f" % price)
print("Quantity:", quantity)
print("Subtotal: $%.2f" % subtotal)
print("Tax: $%.2f" % tax)
print("Total: $%.2f" % total)
print()
# SECTION 4: Type Conversion
print("SECTION 4: Type Conversion")
print("-" * 80)
revenue_str = "125000"
revenue_int = int(revenue_str)
print("'%s' (str) -> %d (int)" % (revenue_str, revenue_int))
price_str = "49.99"
price_float = float(price_str)
print("'%s' (str) -> %.2f (float)" % (price_str, price_float))
print()
print("You've learned:")
print(" * Variables store and label your data")
print(" * Data types: int, float, str, bool")
print(" * Operators for calculations")
print(" * Type conversion for data transformations")
print()
print("Next: Control flow and functions!")
Section 1 of 14 • Lesson 1 of 4