Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

def most_frequent(string):

d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d

print most_frequent('aabbbc')
Returning:

{'a': 2, 'c': 1, 'b': 3}


------------------------------------------------------------------------------------------------------------------------------------
Python program to find smallest
# number in a list

# list of numbers
list1 = [10, 20, 4, 45, 99]

# sorting the list


list1.sort()

# printing the first element


print("Smallest element is:", list1[0])

Output:
smallest element is: 4
------------------------------------------------------------------------------------------------------------------------------------
# list of numbers
list1 = [10, 20, 4, 45, 99]

# sorting the list


list1.sort(reverse=True)

# printing the first element


print("Smallest element is:", list1[-1])

Output:
smallest element is: 4
------------------------------------------------------------------------------------------------------------------------------------
Find minimum list element for a user defined list
# Python program to find smallest
# number in a list

# creating empty list


list1 = []

# asking number of elements to put in list


num = int(input("Enter number of elements in list: "))

# iterating till num to append elements in list


for i in range(1, num + 1):
ele= int(input("Enter elements: "))
list1.append(ele)

# print minimum element:


print("Smallest element is:", min(list1))
Output:
Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 11
Enter elements: 99
Smallest element is: 11

Python program to count Even and Odd


numbers in a List
# Python program to count Even
# and Odd numbers in a List

# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

# iterating each number in list


for num in list1:

# checking condition
if num % 2 == 0:
even_count += 1

else:
odd_count += 1

print("Even numbers in the list: ", even_count)


print("Odd numbers in the list: ", odd_count)
Output:
Even numbers in the list: 3
Odd numbers in the list: 4

------------------------------------------------------------------------------------------------------------------------------------
Function to print the characters
# of the given String in decreasing
# order of their frequencies

You might also like