Posts

Showing posts with the label for loop

Fun Function Friday!!!

Image
My Random Function: Count and Percent The other day I was working on a project to simply count and give me the total percent count. First I started with the data.  The Data: import pandas as pd from itertools import combinations data = { 'id': list(range(1, 21)), 'course': ['Math', 'Math', 'Bio', 'Chem', 'Bio', 'Math', 'Chem', 'Bio', 'Chem', 'Math', 'Bio', 'Chem', 'Math', 'Math', 'Bio', 'Chem', 'Bio', 'Math', 'Chem', 'Math'], 'building': ['A', 'B', 'A', 'B', 'B', 'A', 'A', 'B', 'B', 'A', 'A', 'B', 'A', 'B', 'A', 'B', 'B', 'A', 'A', 'B'], 'room': [101, 102, 101, 102, 103, 101, 104, 103, 102, 101, 105, 106, 107, 108, 109...

f'{strings}' are so useful!!!

Image
Unraveling Python's String Formatting Syntax Python, an incredibly versatile programming language, offers a rich array of features. One of them is the string formatting syntax. This capability enables us to insert various objects, often other strings, directly into our strings. >>> name = "Andrew" >>> print(f"My name is {name}. What's your name?") My name is Andrew. What's your name? This mechanism doesn't just stop at simple insertions; it also allows the embedding of expressions within these strings. >>> name = "Andrew" >>> print(f"{name}, which starts with {name[-1]}") Andrew, ends with w Beyond this, Python's string formatting syntax provides tools to control the formatting of each inserted string component, lending your code an even greater degree of flexibility and precision. At first glance, Python's string formatting syntax may appear quite complex, with a myriad ...

Functions Intro II

Image
An Diving Deeper into Function Arguments 5. Diving Deeper into Function Arguments There are several ways to feed arguments into a Python function, allowing for a lot of flexibility. Let's go through some of the most important ones: 5.1 Positional Arguments Positional arguments are the most basic type of argument. They're called "positional" because the order in which you pass them matters. For instance: def greet(name, greeting): print(f'{greeting}, {name}!') greet('Alice', 'Hello') # prints: Hello, Alice! In this example, 'Alice' is the first argument and 'Hello' is the second argument. Their order matches the order of parameters in the function declaration. 5.2 Keyword Arguments Keyword arguments are identified by the keyword used before them when calling the function. You can use keyword arguments to make your code more readable or to specify default values for parameters: def greet...

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...

for Loops for Graphics

Image
Your Website Title Grouping Sales Data By Month Let's say you have sales data stored as a list of dictionaries, where each dictionary represents a sale and contains details about the fruit, the quantity sold, and the month of sale. For example: sales_data = [ {"fruit": "apple", "quantity": 5, "month": "January"}, {"fruit": "banana", "quantity": 4, "month": "January"}, {"fruit": "apple", "quantity": 7, "month": "February"}, ... ] To report the sum of sales for each fruit, grouped by month, you can use Python's built-in data types and a for loop. Here's a sample Python code to achieve that: # Initialize an empty dictionary to store the results monthly_sales = {} # Loop over the sales data for sale in sales_data: # Get the month and fruit from the sale month = sale["mont...

Intro to for Loops

Image
Your Website Title Understanding For Loops and Break Statement in Python Introduction For loops are an integral part of any programming language, including Python. They allow us to execute a block of code multiple times, which is particularly useful when we want to iterate over a sequence such as a list, a tuple, a dictionary, a string, etc. In this post, we will also cover the usage of the 'break' statement which allows us to have more control over the loop's execution. Basic For Loop Structure The basic structure of a for loop in Python is as follows: for variable in sequence: # statements to execute for each iteration Here, 'variable' is the variable that takes the value of the item inside the sequence on each iteration. The 'sequence' could be any iterable object in Python. Example 1: for i in range(5): print(i) In the above code, range(5) generates a sequence of numbers from 0 to 4. For each iteration, ...