How to Check if an Object is Iterable in Python

How to check if an object is iterable in Python

While working on a data processing script, I needed to loop through a list of customer records. The issue was, I wasn’t sure if the object I received from an API was even iterable. If you’ve been coding in Python for a while, you’ve probably run into the same problem. Sometimes, you expect a list … Read more >>

Sort a List of Lists in Python

How to sort a list of lists in Python

Recently, I was working on a project where I had to organize student test scores stored in a list of lists. Each sublist contained the student’s name and score. At first, I thought sorting this kind of nested list would be tricky. But after experimenting with Python’s built-in functions, I realized there are several simple … Read more >>

How to Capitalize Alternate Letters in a String in Python

Alternate the case of each letter in a given string in Python

When I was working on a text formatting project, I needed to make strings look “stylized” by capitalizing every alternate letter. At first, I thought Python might have a built-in function for this. But soon, I realized I needed to use a combination of string methods, slicing, and loops to achieve it. In this tutorial, … Read more >>

How to Get the First and Last Digit of a Number in Python

Get the first and last digit of a number in Python: Command Prompt output of a function tested on positive, negative and large numbers

To get the last digit of a number in Python, use the modulo operator: number % 10. To get the first digit of a number in Python, convert it to a string and take the first character: int(str(number)[0]). Both work for any size of integer; use abs() first if the number can be negative. This … Read more >>

How to Remove NaN Values from a List in Python

How to remove nan from a list in python

While cleaning a dataset for a project, I ran into an issue where my Python list had a mix of numbers and NaN values. If you’ve worked with real-world data in the US, say, sales reports, healthcare data, or survey results, you know how common missing values can be. The problem is simple: NaN (Not … Read more >>

Traffic Signal Program in Python

write python program for traffic signals

When I first started learning Python more than a decade ago, I loved building small projects that looked like real-world systems. One of my favorites was a traffic signal program. It’s simple, fun, and surprisingly practical. If you live in the USA, you see traffic lights every day, green for go, yellow for slow down, … Read more >>

Find Armstrong Numbers in an Interval in Python

armstrong number in python

I was working on a Python project where I needed to filter out all Armstrong numbers within a given range. Python doesn’t have a direct function for Armstrong numbers either. So, I had to come up with a simple program to check each number in the interval and return only those that qualify as Armstrong … Read more >>

Python Program to Swap Two Elements in a List

Python program to swap two elements in a list

Recently, I was working on a data-cleaning project where I had to reorder elements in a customer list.The challenge was simple: swap two items in a Python list. At first, I thought there might be a single “built-in” function to do this. But as I explored, I realized there are multiple ways to swap elements … Read more >>