Geek Logbook

Tech sea log book

Inserting a Student into a Sorted List in Python

When working with sorted lists in Python, it’s essential to ensure that any new elements are added in the correct order. Problem Statement You have a list of student names sorted in alphabetical order, like this: You need to define a function add_student(sorted_list, new_student) that receives a list of names in the format above and

Ensuring Type Safety in Python Functions

When writing Python functions, ensuring that the parameters are of the correct type is crucial for robust and error-free code. In this post, we’ll explore how to enforce type checks in a function to ensure it raises an error if the parameter is not of the expected type. The Problem Consider a simple function that

Filtering and Counting Keys in Python Dictionaries

In this post, we’ll explore how to count the keys in a dictionary and filter a dictionary by its values in Python. These are common tasks that can be useful in a variety of situations when working with dictionaries. Counting the Keys of a Dictionary To count the number of keys in a dictionary, you

How to Aggregate Values by Date and Sum Them in Python

Problem Statement Suppose you have a list of transactions or events, each associated with a date and a numeric value (e.g., sales amount, transaction amount). Your goal is to aggregate these values by date and calculate the total sum for each date. Solution Approach We’ll use Python’s dictionary data structure to achieve this. Dictionaries allow

Combining Multiple CSV Files into One with Python

If you work with data, chances are you’ve encountered situations where you need to merge multiple CSV files into a single file for analysis. Manually combining these files can be time-consuming and error-prone. In this post, I’ll show you how to automate this task using Python and the powerful pandas library. Problem Statement Let’s assume

Creating Directories in Python

The os Module Python’s os module provides a way to interact with the operating system. It includes functions for creating, removing, and checking the existence of directories and files. In this tutorial, we’ll use the os.path.exists and os.makedirs functions. This module contains the necessary functions to check for the existence of a directory and create

Pandas Dataframe: apply method

Calculating Discounts, Taxes, and Total Amount in a DataFrame Suppose you have the following data in a DataFrame: Product Price Category 0 A 100 Electronic 1 B 200 Cloth 2 C 150 Electronic 3 D 300 Colth 4 E 250 Electronic In this DataFrame, you want to create three new columns: discount, taxes, and total_amount.

 Escape sequences in Pyhon

Escape sequences as in C While reading Automate the Boring Stuff with Python: Practical Programming for Total Beginners, I noticed the existence of raw strings. A raw string is created in such a way that escape sequences listed in the Python documentation are not processed. In fact, at the beginning of the section on Lexical

Dictionary methods: keys(), values(), and items()

In Python, there are three special methods related to dictionaries that are worth mentioning: keys(), values(), and items(). Interestingly, these methods do not return true lists. They cannot be modified and do not have an append() method. However, dict_keys, dict_values, and dict_items can be used in for loops. This distinction is important to keep in