Python Fundamentals for Data Wrangling

In this lesson, you'll learn the fundamental building blocks of Python: variables, data types, and basic operations. These are essential for manipulating and transforming data, the core of data wrangling. You'll gain a solid foundation to start cleaning and shaping your data.

Learning Objectives

  • Define and declare variables in Python.
  • Identify and differentiate between common Python data types (integers, floats, strings, booleans).
  • Perform basic arithmetic and string operations.
  • Understand and utilize comments in Python code.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Variables

A variable is a named storage location that holds a value. Think of it like a container where you can store data. In Python, you create a variable by assigning a value to a name using the = sign.

# Assigning the value 10 to the variable 'age'
age = 10

# Assigning the string 'Alice' to the variable 'name'
name = 'Alice'

Data Types: The Foundation of Data

Data types classify the kind of value a variable can hold. Understanding data types is crucial for data wrangling as it dictates how you can manipulate the data. Here are some common Python data types:

  • Integers (int): Whole numbers (e.g., -3, 0, 5, 1000)
  • Floats (float): Numbers with decimal points (e.g., -2.5, 0.0, 3.14, 10.0)
  • Strings (str): Sequences of characters enclosed in single or double quotes (e.g., 'hello', "world", '123')
  • Booleans (bool): Represent truth values: True or False
age = 30  # int
price = 99.99  # float
name = 'Bob'  # str
is_active = True  # bool

Basic Operations: Manipulating Data

You can perform operations on variables based on their data types.

  • Arithmetic Operations (for numbers):

    • + (addition), - (subtraction), * (multiplication), / (division), ** (exponentiation), // (floor division), % (modulo)

    python x = 10 y = 3 print(x + y) # Output: 13 print(x - y) # Output: 7 print(x * y) # Output: 30 print(x / y) # Output: 3.3333333333333335 print(x ** y) # Output: 1000 print(x // y) # Output: 3 (integer division) print(x % y) # Output: 1 (remainder)

  • String Operations:

    • + (concatenation: joining strings) and * (repetition)

    python first_name = 'John' last_name = 'Doe' full_name = first_name + ' ' + last_name # Concatenation print(full_name) # Output: John Doe repeated_string = 'Hello ' * 3 print(repeated_string) # Output: Hello Hello Hello

Comments: Making Code Readable

Comments are notes in your code that are ignored by the Python interpreter. They're essential for explaining what your code does. Use the # symbol to create a single-line comment. For multi-line comments, you can use triple quotes (""" or ''').

# This is a single-line comment

"""
This is a multi-line comment
that explains the code below.
"""

# Calculate the sum of two numbers
sum = 5 + 3
print(sum) # Output the sum
Progress
0%