Member-only story

Discovering Palindromic Numbers: A Python Journey Through Decimal and Binary Worlds

Unveil the magic of numbers that read the same forwards and backwards in both decimal and binary formats

Max N
3 min readJun 2, 2023
Photo by Andrew Seaman on Unsplash

Introduction

In this tutorial, we’ll explore a fascinating Python programming problem that involves finding palindromic numbers in both decimal and binary formats.

A palindromic number reads the same forwards and backwards, like the decimal number 121 or the binary number 1001. Our goal is to print all numbers from 1 to 1000 (inclusive) that are palindromic in both formats.

Step 1: Defining a Function to Check Palindromes

First, let’s create a function that checks if a given string is a palindrome. This function will be useful for verifying if a number is palindromic in both decimal and binary formats.

def is_palindrome(s):
return s == s[::-1]

This function takes a string s as input and returns True if the string is a palindrome, and False otherwise. The expression s[::-1] reverses the string using Python’s slice notation.

Step 2: Iterating Over the Numbers from 1 to 1000

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet