Python Basics for Data Science

1. Variables and Data Types: Your Data's Containers
In Python, a variable is just a name for a container that stores data.
 Python automatically handles the data type, so you don't need to declare it explicitly. 
 Key data types:
  • int (Integer): Whole numbers like 25.
  • float (Floating-Point): Numbers with decimal points, like 21.5.
  • str (String): Text data enclosed in quotes, such as "Chicago".
  • bool (Boolean): Logical values that are either True or False.
Example:
python
age = 25
temperature = 21.5
city = "Chicago"
is_raining = False

print(age, temperature, city, is_raining)
# 55 21.5 Chicago False


2. Working with Lists, Tuples, and Dictionaries
Lists: Mutable and Ordered
Think of them as a dynamic-sized array(contiguous blocks of memory) that can grow or shrink.
Example:
python
temperatures = [22, 21, 19, 24, 25]

# Calculate the average
print("Average temperature:", sum(temperatures) / len(temperatures))

# Add a new data point
temperatures.append(23)
print(temperatures)

Tuples: Immutable and Ordered
Tuples are similar to lists but are unchangeable once created. They are ideal for fixed, related pieces of data, such as a coordinate pair.
Example:
python
city_coordinates = ("Chicago", 41.8781, -87.6298)
print(f"City: {city_coordinates[0]}, Latitude: {city_coordinates[1]}")

Dictionaries: Key-Value Pairs
Dictionaries are like mini data tables that map keys to values. This structure is perfect for representing structured data, such as records parsed from an API.
Example:
python
person = {
    "name": "Ava",
    "age": 28,
    "city": "Seattle"
}

print(person["name"], "is from", person["city"])


3. Control Flow and Loops: Making Decisions and Repeating Actions
if/else Example
Conditional statements are essential for categorizing data.
Example:
python
temperature = 19

if temperature > 25:
    print("Hot Day")
elif temperature >= 20:
    print("Warm Day")
else:
    print("Cold Day")

for Loops for Simple Data Analysis
for loops are invaluable for iterating over sequences of data.
Example:
python
temps = [21, 23, 20, 19, 24, 25, 22]

total = 0
for t in temps:
    total += t

average_temp = total / len(temps)
print("Average Temperature:", round(average_temp, 2))

A more "Pythonic" and concise way to perform this is using a list comprehension.
List Comprehension Example:
python
temps = [21, 23, 20, 19, 24, 25, 22]
high_temps = [t for t in temps if t > 22]
print("Days above 22°C:", high_temps)


4. Functions and Lambda Expressions: Reusable Logic
Functions make your code modular, readable, and reusable, which is a significant advantage when cleaning or summarizing large datasets.
Defining Functions
def function is used for reusable, named blocks of code.
Example:
python
def average(values):
    return sum(values) / len(values)

temps = [21, 23, 20, 25]
print("Average:", average(temps))

Lambda Expressions (Anonymous Functions)
Lambda expressions are small, single-line, anonymous functions. 
They are often used as arguments to higher-order functions like map() and filter().
Example:
python
# Convert a list of Fahrenheit values to Celsius
fahrenheit = [98.6, 75.2, 60.8]
celsius = list(map(lambda f: (f - 32) * 5/9, fahrenheit))
print(celsius)

# Filter for even numbers
nums = [10, 15, 20, 25, 30]
even_nums = list(filter(lambda x: x % 2 == 0, nums))
print(even_nums)

Lambdas are a powerful tool for quickly applying transformations or filtering criteria without writing a full function definition.

Comments

Popular Posts