Python for Data Science

This lesson introduces the fundamentals of Python, a critical programming language for data scientists. You'll learn about basic syntax, data types, and control flow, which will form the foundation for more advanced data science concepts.

Learning Objectives

  • Identify and differentiate between common Python data types (integers, floats, strings, booleans, lists, and dictionaries).
  • Utilize basic Python operators (+, -, *, /, //, %, ==, !=, >, <, >=, <=) for calculations and comparisons.
  • Write and execute simple Python scripts using if/else statements, for loops, and while loops to control program flow.
  • Understand how to manipulate strings and work with lists and dictionaries to store and retrieve data.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Python: Why Python for Data Science?

Python is the go-to language for data science due to its versatility, vast libraries, and easy-to-read syntax. It allows you to clean, analyze, visualize, and build predictive models with relative ease. This lesson will get you started with the basics, focusing on core concepts that are crucial for understanding more advanced topics later on.

Data Types: The Building Blocks

Python uses different data types to store various kinds of information:

  • Integers (int): Whole numbers (e.g., 10, -5, 0)
  • Floats (float): Numbers with decimal points (e.g., 3.14, -2.5)
  • Strings (str): Sequences of characters enclosed in single or double quotes (e.g., 'Hello', "World")
  • Booleans (bool): Represent truth values, either True or False
  • Lists: Ordered collections of items, enclosed in square brackets [] (e.g., [1, 2, 3], ['apple', 'banana'])
  • Dictionaries (dict): Collections of key-value pairs, enclosed in curly braces {} (e.g., {'name': 'Alice', 'age': 30})

Basic Operators: Doing the Math

Python provides operators for performing calculations and comparisons:

  • Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), // (floor division - returns the integer part of the division), % (modulo - returns the remainder)
  • Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)

Examples:

a = 10
b = 3
print(a + b)  # Output: 13
print(a / b)  # Output: 3.3333333333333335
print(a // b) # Output: 3
print(a % b)  # Output: 1
print(a == b) # Output: False

Control Flow: Making Decisions and Repeating Actions

Control flow statements allow you to control the order in which code is executed.

  • if/else Statements: Execute code blocks based on conditions.
    ```python
    age = 20
    if age >= 18:
    print("Eligible to vote")
    else:
    print("Not eligible to vote")
*   **for Loops:** Iterate over a sequence (e.g., a list or a string).
    ```python
for fruit in ["apple", "banana", "cherry"]:
    print(fruit)
  • while Loops: Repeat a block of code as long as a condition is true.
    python count = 0 while count < 3: print(count) count += 1

Working with Strings, Lists, and Dictionaries

Strings, lists, and dictionaries are fundamental data structures.

  • Strings: You can access individual characters by index (starting from 0). You can also perform operations like concatenation and slicing.
    ```python
    my_string = "Hello"
    print(my_string[0]) # Output: H
    print(my_string + " World") # Output: Hello World
*   **Lists:**  Lists are mutable (changeable). You can add, remove, and modify items.
    ```python
my_list = [1, 2, 3]
my_list.append(4)  # Add 4 to the end
print(my_list) # Output: [1, 2, 3, 4]
  • Dictionaries: Dictionaries store data as key-value pairs, allowing you to quickly look up values based on their keys.
    python my_dict = {"name": "Alice", "age": 30} print(my_dict["name"]) # Output: Alice
Progress
0%