128k views
2 votes
Python programming questions:

1. Assume the names variable references a list of strings. Write code that determines whether 'Dale' is in the names list. If it is, display the message 'Hello Dale'. Otherwise, display the message 'No Dale'.
2. Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
3. Write a program that reads the contents of a text file. The program should create a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the dictionary would contain an element with 'the' as the key and 128 as the value. The program should either display the frequency of each word or create a second file containing a list of each word and its frequency.

User Autonomy
by
6.7k points

1 Answer

5 votes

Answer:

Following are the program in the Python Programming Language:

1.)

l=['Ruby','Dale','Kate']

n=input('Enter the name: ')

if(n in l):

print('Hello {}'.format(n))

else:

print('No {}'.format(n))

Output:

Enter the name: Dale

Hello Dale

2.)

file=open("text1.txt", "r+") #open file

str=file.read() #read file

txt=str.replace('\\', ' ') #replace spaces

word=txt.split(' ') #split words in ' '

for words in word: #set for loop

word2=' '

for i in range(len(words)): #set for loop

if(words[i].isalpha()): #set if statement

word2 += words[i].lower()

word[word.index(words)]=word2

while(' ' in word): #set while loop

word.remove(' ') #remove spaces

st=set(word)

print('The ', len(st),' unique words which appears in document are ' \

'(in no particular order): ')

for words in st: #set for loop

print('-', word)

file.close() #close file

3.)

file=open("text1.txt", "r+") #open file

str=file.read() #read file

txt=str.replace('\\', ' ') #replace spaces

word=txt.split(' ') #split words in ' '

for words in word: #set for loop

word2=' '

for i in range(len(words)): #set for loop

if(words[i].isalpha()): #set if statement

word2 += words[i].lower()

word[word.index(words)]=word2

while(' ' in word): #set while loop

word.remove(' ') #remove spaces

dic={} #set dictionary

for words in word: #set for loop

if(words not in dic): #set if statement

dic[words]=1

else:

dic[words] +=1

print('\033[4m' + 'word frequency' + '\033[0m')

for words in dic:

print(format(words, '15s'), format(dic[words], '5d'))

file.close() #close file

Step-by-step explanation:

1.) Here, we define the list data type variable 'l' then, set the variable 'n' and assign value by get input from the user,

  • we set if conditional statement and pass condition is that the variable 'n' is in the variable l then, print message.
  • otherwise, we print No message.

2.) Here, we define the variable "file" in which we open file and save that file in it.

Then, we set variable st which read the file then we set the variable in which we apply the replace().

Then. we set variable in which we split the by suing split()

2.) Here, we define the variable "file" in which we open file and save that file in it.

Then, we set variable st which read the file then we set the variable in which we apply the replace().

Then. we set variable in which we split the by suing split()

Then, we apply all that given in the question.

User Speg
by
7.4k points