Learn how to test if a number or string is a palindrome in Python by reversing text with slicing [::-1].
Learn how to convert minutes into seconds in Python using arithmetic multiplication and basic unit conversions.
Create a function is_symmetrical(num) that takes a number and returns True if the number is symmetrical (a palindrome that reads the same backward as forward), and False otherwise.
Palindrome checks test string conversion, sequence comparison, and numerical data manipulation.
Convert the number to a string using str(), then compare it to its reversed slice s[::-1]:
def is_symmetrical(num):
s = str(num)
return s == s[::-1]
# Testing our function
print(is_symmetrical(7227)) # Output: True
print(is_symmetrical(12567)) # Output: False
print(is_symmetrical(444444)) # Output: True
print(is_symmetrical(9939)) # Output: False
str(7227) yields string "7227"."7227"[::-1] yields reversed string "7227"."7227" == "7227" evaluates to True.num[::-1]) raises a TypeError: 'int' object is not subscriptable. Always convert numbers to strings with str(num) first!Cast numbers to strings before slicing. Next, let's check if a Python dictionary is empty!