0% found this document useful (0 votes)
3 views

Python_Programs on 21-5-2022

Uploaded by

bhuvanvasa23s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_Programs on 21-5-2022

Uploaded by

bhuvanvasa23s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# Python program to reverse a number using for loop

#Method-1:
#If number is treated as a string.
num=input("Enter a number")
rev=num[::-1]
print("Reverse Number=", rev)
if num==rev:
print("PALINDROME")
else:
print("NOT A PALINDROME")

# Method-2:
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

#Method-3:
# take inputs
num = input('Enter the number: ')

# calculate reverse of number


reverse = ''
for i in range(len(num), 0, -1):
reverse += num[i-1]

#print reverse of number


print('The reverse number is =', reverse)
-----------------------------------------------------------------------------------
---
# Palindrome string or not
str=input("Enter a string:")
rev_str=str[::-1]
if str==rev_str:
print("PALINDROME")
else:
print("NOT A PALINDROME")

-----------------------------------------------------------------------------------
----
# WAP to find all duplicates in a list
from collections import Counter
list1=[1,2,1,2,3,4,5,1,1,2,5,6,7,8,9,9]
d=Counter(list1)
print(d)
new_list=list([item for item in d if d[item]>1])
print(new_list)
-----------------------------------------------------------------------------------
----
# Write a function unique() to find all the unique elements of a list
def unique(list1):
unique_list=[]
for x in list1:
if x not in unique_list:
unique_list.append(x)
for x in unique_list:
print(x)

list1=[10,20,10,30,40,40]
print('Unique elements from list1 are:')
unique(list1)
list2=[1,2,1,1,3,4,3,3,5]
print('Unique elements from list2 are:')
unique(list2)
-----------------------------------------------------------------------------------
----
>>>import collections
>>>dir(collections)
['ChainMap', 'Counter', 'OrderedDict', 'UserDict', 'UserList', 'UserString',
'_Link', '_OrderedDictItemsView', '_OrderedDictKeysView', '_OrderedDictValuesView',
'__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', '_chain', '_collections_abc',
'_count_elements', '_eq', '_iskeyword', '_itemgetter', '_proxy', '_recursive_repr',
'_repeat', '_starmap', '_sys', '_tuplegetter', 'abc', 'defaultdict', 'deque',
'namedtuple']

You might also like