
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
When it is required to find all the numbers that are odd, and are palindromes and lie between a given range of values, and it has been told that recursion can’t be used, then, list comprehension, and ‘%’ operator can be used to achieve the same.
Palindromes are string that are same when they are read in either way- left to right and right to left.
Below is a demonstration for the same −
Example
my_list = [] lower_limit = 5 upper_limit = 189 print("The lower limit is : ") print(lower_limit) print("The upper limit is : ") print(upper_limit) my_list = [x for x in range(lower_limit,upper_limit+1) if x%2!=0 and str(x)==str(x)[::-1]] print("The numbers which are odd and palindromes between " + str(lower_limit) + " and " + str(upper_limit) + " are : ") print(my_list)
Output
The lower limit is : 5 The upper limit is : 189 The numbers which are odd and palindromes between 5 and 189 are : [5, 7, 9, 11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181]
Explanation
- An empty list, a lower limit and an upper limit is defined.
- The upper and lower limit are displayed on the console.
- The values between upper and lower limit are iterated over, and checked to see if it is divisible by 2.
- Then, it is converted to a string, and elements from the end of the string and the string are compared.
- This is assigned to a variable.
- This is displayed as output on the console.
- Related Articles
- Python program to print all odd numbers in a range
- PHP program to find the sum of odd numbers within a given range
- Python program to print all even numbers in a range
- Program to count odd numbers in an interval range using Python
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Find the sum of all odd numbers between 0 and 50.
- Find the sum of all odd numbers between 100 and 200.
- Golang Program to Print Odd Numbers Within a Given Range
- C++ program to find numbers with K odd divisors in a given range
- Program to find bitwise AND of range of numbers in given range in Python
- Program to find all contiguously increasing numbers in start end range in Python
- Write the cubes of all natural numbers between 1 and 10 and verify the following statements:(i) Cubes of all odd natural numbers are odd.(ii) Cubes of all even natural numbers are even.
- 8085 program to take all numbers which are in range 3CH and 64H in an array
- Java Program to Find Sum of First N Odd numbers and Even numbers
- Program to find sum of first N odd numbers in Python

Advertisements