﻿ Python: Test whether a given number is symmetrical or not - w3resource

# Python: Test whether a given number is symmetrical or not

## Python Basic - 1: Exercise-83 with Solution

Write a Python program to test whether a given number is symmetrical or not. A number is symmetrical when it is equal to its reverse.

For example- 121 is the symmetric number.

Sample Solution:

Python Code:

``````# Define a function to check if a number is symmetrical
def is_symmetrical_num(n):
# Convert the number to a string and compare it with its reverse
return str(n) == str(n)[::-1]

# Test cases
print(is_symmetrical_num(121))    # True, as 121 is a symmetrical number
print(is_symmetrical_num(0))      # True, as single-digit numbers are symmetrical
print(is_symmetrical_num(122))    # False, as 122 is not a symmetrical number
print(is_symmetrical_num(990099)) # True, as 990099 is a symmetrical number
``````

Sample Output:

```True
True
False
True
```

Explanation:

Here is a breakdown of the above Python code:

• The function "cal_median()" takes a list of numbers as input.
• The list is sorted in ascending order using the "sort()" method.
• The length of the list ('n') is calculated, and the middle index ('m') is determined.
• If the length is even, the average of the two middle numbers is returned; otherwise, the middle number is returned.
• Test cases are provided to demonstrate the function's functionality.

Visual Presentation:

Flowchart:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿