Posts

Showing posts with the label if/elif

Functions III Type Hints

Image
Type Hinting in Python Functions Type Hinting in Python Functions As you start to write more complex Python code, you might have come across function definitions that include syntax like : str or -> int . These are examples of type hinting, a feature introduced in Python 3.5 as part of PEP 484. Type hints make your code more explicit and easier to understand. They can also help with debugging and allow some IDEs and tools to provide better autocompletion and linting. 1. Basic Type Hints The most basic type hints are straightforward. Just add a colon and the type after the parameter name in the function definition. You can do the same for the return type by adding -> type before the final colon. Here's an example: def greet(name: str) -> str: return f'Hello, {name}!' In this example, we're saying that the name parameter should be a string, and the function will return a stri...

Comparing DataFrames

Image
Comparing Relationships in Dataframes with Python Comparing Relationships in Dataframes with Python If you work with data, chances are you will need to compare different datasets to each other. One specific task could be to compare the relationships between categorical variables in different datasets. In this blog post, we will use Python and its powerful libraries such as Pandas and NumPy to accomplish this task. What are we trying to achieve? Imagine you have two datasets, a 'base' and a 'comparison' dataset. Each dataset has the same categorical variables, for example 'degree_type' and 'acad_degree'. You want to check if the relationship between these variables is the same in both datasets. For instance, if in the base dataset 'AAS' always corresponds to 'Associates', you want to ensure the same applies to the comparison dataset. How do we do it? We start by creating a function, let's cal...

Blog Topics

Image
This platform aims to systematically curate and preserve my favored Python techniques for future reference. This digital journal, accessible globally wherever an internet connection is available, is a convenient and effective learning tool.  Going down a wooded path at night, alone, with your trusted companion by your side, can be scary and daunting.  However, with support from your friends, documenting your path (curating a code base like a blog), and continuously working on the code base can make the process appear as a walk in the park!  In a world dominated by sophisticated language models like OpenAI's GPT, blogging may seem like a relic of the past. But here's a nuanced perspective: this blog serves as an innovative instrument for both teaching and mastering new programming languages. It's about immersing in syntax, applying it, and then translating the information into layman's terms for broader understanding. Each topic presented here is elaborated in a separate...