Complete Data Science Bootcamp (Python, Statistics, Machine Learning)
Python Basics: Variables, Data Types & Operators
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:
# Create variables (Python figures out the type automatically)student_name = "Alice"student_age = 25student_gpa = 3.85is_enrolled = TrueVariable Naming Rules
Follow these conventions for clean, professional code:
-
Use descriptive names:
- ✅
customer_revenue(clear meaning) - ❌
x(unclear)
- ✅
-
Use snake_case for multi-word names:
- ✅
total_sales(Python style) - ❌
totalSales(JavaScript style)
- ✅
-
Start with letter or underscore:
- ✅
_private_data - ❌
2nd_quarter(can't start with number)
- ✅
-
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:
count = 100year = 2024negative = -50Floats (float) - Decimal numbers:
price = 99.99temperature = -3.5pi = 3.14159Text Type
Strings (str) - Text data:
name = "Data Science"description = 'Python for data analysis'multiline = """This isa multilinestring"""Boolean Type
Boolean (bool) - True or False:
is_valid = Truehas_error = FalseChecking Types
Use type() to check a variable's type:
age = 25print(type(age)) # <class 'int'>Summary
Key Takeaways:
- Variables store data with descriptive names (use snake_case)
- Main types: int, float, str, bool
- Operators: arithmetic (+, -, *, /), comparison (==, !=, <, >), logical (and, or, not)
- Type conversion: int(), float(), str() for transforming data
- 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 Basics: Variables, Data Types & Operators# Complete hands-on examples for data scienceprint("="*80)print("PYTHON BASICS FOR DATA SCIENCE")print("="*80)print()# SECTION 1: Variables and Assignmentprint("SECTION 1: Variables and Assignment")print("-" * 80)# Create variables with different typesdataset_name = "Customer Sales Data"num_records = 10000avg_purchase = 149.99is_processed = Falseprint("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 Actionprint("SECTION 2: Data Types in Action")print("-" * 80)# Integer examplescustomers_count = 5000print("Customers:", customers_count, "(type:", type(customers_count).__name__ + ")")# Float examplesconversion_rate = 0.0345print("Conversion Rate: %.2f%%" % (conversion_rate * 100))# String examplesproduct_name = "Data Science Pro"print("Product:", product_name)# Boolean exampleshas_premium = Trueprint("Premium Member:", has_premium)print()# SECTION 3: Arithmetic Operatorsprint("SECTION 3: Arithmetic Operators")print("-" * 80)price = 99.99quantity = 5tax_rate = 0.08subtotal = price * quantitytax = subtotal * tax_ratetotal = subtotal + taxprint("Price: $%.2f" % price)print("Quantity:", quantity)print("Subtotal: $%.2f" % subtotal)print("Tax: $%.2f" % tax)print("Total: $%.2f" % total)print()# SECTION 4: Type Conversionprint("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!")