1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
DedPeter [7]
3 years ago
11

Write a program that reads in your question #2 Python source code file and counts the occurrence of each keyword in the file. Yo

ur program should prompt the user to enter the Python source code filename

Computers and Technology
1 answer:
scoray [572]3 years ago
7 0

Answer:

Here is the Python program:

import keyword  #module that contains list of keywords of python

filename = input("Enter Python source code filename: ") # prompts user to enter the filename of a source code

code = open(filename, "r") # opens the file in read mode

keywords = keyword.kwlist #extract list of all keywords of Python and stored it into keywords

dictionary = dict() #creates a dictionary to store each keyword and its number of occurrence in source code

for statement in code: # iterates through each line of the source code in the file

   statement = statement.strip() # removes the spaces in the statement of source code  

   words = statement.split(" ") #break each statement of the source code into a list of words by empty space separator

   for word in words:# iterates through each word/item of the words list  

       if word in keywords:#checks if word in the code is present in the keywords list of Python  

           if word in dictionary: #checks if word is already present in the dictionary

               dictionary[word] = dictionary[word] + 1 #if word is present in dictionary add one to the count of the existing word

           else: #if word is not already present in the dictionary  

               dictionary[word] = 1 #add the word to the dictionary and set the count of word to 1

for key in list(dictionary.keys()): #iterates through each word in the list of all keys in dictionary  

   print(key, ":", dictionary[key])# prints keyword: occurrences in key:value format of dict

Explanation:

The program is well explained in the comments attached with each line of the program.  

The program prompts the user to enter the name of the file that contains the Python source code. Then the file is opened in read mode using open() method.

Then the keyword.kwlist statement contains the list of all keywords of Python. These are stored in keywords.

Then a dictionary is created which is used to store the words from the source code that are the keywords along with their number of occurrences in the file.

Then source code is split into the lines (statements) and the first for loop iterates through each line and removes the spaces in the statement of source code .

Then the lines are split into a list of words using split() method. The second for loop is used to iterate through each word in the list of words of the source code. Now each word is matched with the list of keywords of Python that is stored in keywords. If a word in the source code of the file is present in the keywords then that word is added to the dictionary and the count of that word is set to 1. If the word is already present in the dictionary. For example if there are 3 "import" keywords in the source code and if 1 of the import keywords is already in the dictionary. So when the second import keyword is found, then the count of that keyword is increased by 1 so that becomes 2.

Then the last loop is used to print each word of the Python that is a keyword along with its number of occurrences in the file.

The program and its output is attached in a screenshot. I have used this program as source code file.

You might be interested in
Respond to the following in three to five sentences. Select the workplace skill, habit, or attitude described in this chapter th
Serjik [45]

A skilled and successful worker is any worker who has special skill, training, knowledge, and (usually acquired) ability in their work. A skilled worker may have attended a college, university or technical school. Or, a skilled worker may have learned their skills on the job. Examples of skilled labor include engineers, software development, paramedics, police officers, soldiers, physicians, crane operators, truck drivers, machinist, drafters, plumbers, craftsmen, cooks and accountants. These workers can be either blue-collar or white-collar workers, with varied levels of training or education.

1. They Think About the Skills They Need for the Next Job

We all (OK, most of us) try to be awesome at the skills in our job descriptions, but the most successful people also focus on what they’ll need to know to succeed in their next jobs. Not sure what skills you should be developing? Check out career expert Laura Katen’s tips for honing in on exactly what to reach for next.

2. They Speak Up in Meetings

Especially if you’re in a large meeting, intimidated by the higher-ups there, or don’t know much about what’s going on, it’s easy to sit tight and listen. But the people who get ahead don’t wait for permission or an invitation to speak—they make sure everyone in the room knows they have something to contribute. Even if you don’t have a suggestion? “Speaking up to advocate for a co-worker’s point of view or asking a well thought-out question can go just as far,” says leadership coach Jo Miller.

3. They Dress for the Job They Want

You’ve heard it a thousand times—but it consistently holds true. People who get ahead at work look to those above them and emulate not only the clothes they wear, but the ways in which they present themselves in the office, interact with others, and approach their work.

4. They Get to Know the Higher-Ups

It’s pretty hard to get promoted if your boss’ boss doesn’t know who you are—so make it a point to get to know the higher-ups in your department. Check out Sara McCord’s tips for talking to your boss’ boss the right way.

5. They Know How to Communicate With Those Higher-Ups

If you’ve ever been in an executive-level meeting, you know that c-suiters communicate a bit differently than the rest of us. So, if you want to make it there someday, it’s key to learn how to talk the talk. Career coach Lea McLeod gives a few tips for getting started.

8 0
4 years ago
Read 2 more answers
Mecanismo que permite conocer si la persona que esta ingresando a un sistema es realmente quien deba y no un intruso
Tems11 [23]

Answer:

Sistemas de autenticación y seguridad de la información.

Explicación:

La seguridad de la información es un mecanismo que permite saber si la persona que está ingresando a un sistema es realmente quien debería y no un intruso. La seguridad de la información básicamente ayuda a prevenir el acceso no autorizado y permite que la única persona autorizada ingrese al sistema. Los sistemas de autenticación son el mecanismo de seguridad que se utiliza para proteger los datos y los sistemas. Estos sistemas de autenticación también ayudan a garantizar que los usuarios sean la persona autorizada o no.

4 0
3 years ago
To make it easier to enter names into your code, NetBeans provides a feature known as
Alexus [3.1K]

Answer:

Code completion.

Explanation:

The NetBeans is an integrated development environment framework of java language. Code completion is the features of NetBeans which is easier to complete the code This feature is known as smart code features because it completes the code in a very easy manner. The Code completion features are needful when we have to fill the missing code or program in the java language.  

Hence Code completion.  is used to enter the name into the code in the  NetBeans.

3 0
3 years ago
What type of memory speeds up the action performed by the computer and anticipates instructions?
kherson [118]
A cache memory is a small piece of memory that can be accessed very fast. It keeps copies of data that is used often. For example, by keeping data read from a database in memory, the second time the data is needed, it can be taken from the cache in stead of a lengthy query in the database.

Caches can be found in hardware (ie., on the cpu itself), but also as a programming concept in software.
4 0
3 years ago
Which of the following is NOT true about adjustment layers? brainbuffet
otez555 [7]

Answer the adjustment layers can not be used a lot?

Explanation: i think that was it

6 0
3 years ago
Other questions:
  • Write the printitem() method for the base class. sample output for below program: last name: smith first and last name: bill jon
    6·2 answers
  • Which one of these is a mem?
    8·1 answer
  • Read the description of Mike’s work, and identify his profession. Mike’s job is to record sounds in a studio. He studies a video
    14·2 answers
  • Two groups of simple machines and list the simple machines that belong in each group
    7·1 answer
  • A Raycast returns a float that tells you how far away an Object is
    8·1 answer
  • What is a word processor in ms word​
    9·2 answers
  • What are the most positive and the most negative decimal numbers that can be represented by a 2C (n+k) bit fixed-point number, w
    12·1 answer
  • A boundary marks the inside and outside of a system and sets the system apart from its environment.
    11·1 answer
  • On tool hackers use to get sensitive information from victims is/are:
    15·2 answers
  • A penetration tester is experimenting with Network Mapper (Nmap) on a test network as a privileged user. The tester would like t
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!