Write a Python program that takes a sentence and creates a dictionary containing each word and the number of times it occurs.
The counting should be case-insensitive.
Example
Input:
Python is easy and Python is useful
Output:
{'python': 2, 'is': 2, 'easy': 1, 'and': 1, 'useful': 1}
import numpy as npsentence ="Python is easy and Python is useful"words = np.array(sentence.lower().split())unique_words, counts = np.unique(words, return_counts=True)frequency =dict(zip(unique_words.tolist(), counts.tolist()))print(frequency)
sentence ="Python is easy and Python is useful"words = sentence.lower().split()frequency = {}for word in words:if word in frequency: frequency[word] +=1else: frequency[word] =1print(frequency)
highest =max(marks.values())average =sum(marks.values()) /len(marks)print(highest)for name, mark in marks.items():if mark == highest:print(name)print(average)for name, mark in marks.items():if mark > average:print(name)
92
Ravi
Meena
75.0
Asha
Ravi
Meena
Question 7
Write a function
prime_numbers(numbers)
that accepts a list of integers and returns a new list containing only the prime numbers.
def is_prime(n):if n <2:returnFalsefor i inrange(2, n):if n % i ==0:returnFalsereturnTruedef prime_numbers(numbers): result = []for num in numbers:if is_prime(num): result.append(num)return resultnumbers = [4, 7, 10, 13, 18, 23, 1]print(prime_numbers(numbers))