SoFunction
Updated on 2024-11-12

How to view python keywords

Now let's talk about keywords, ready to start taking notes, as the saying goes, a good memory is not as good as a bad pen, remember, often heard people mention about how many keywords in Python? In fact, there are currently 31 keywords in Python, you want to specifically view as well as observe the number of ways very simple, the following for you in detail.

1. keyword module for output viewing

Help on module keyword:
NAME
 keyword - Keywords (from "")
FILE
 /usr/lib64/python2.6/
DESCRIPTION
 This file is automatically generated; please don't muck it up!
 To update the symbols in this file, 'cd' to the top directory of
 the python source tree after building the interpreter and run:
 python Lib/
FUNCTIONS
 iskeyword = __contains__(...)
 x.__contains__(y) y in x.
DATA
 __all__ = ['iskeyword', 'kwlist']
 kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', ...

2. Get a list of python keywords

>>> 

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

3, to determine whether the string contains keywords

>>> ('and')
True
>>> 
>>> ('has')
False

Python Keyword Knowledge Point Expansion:

TF-IDF

TF-IDF (Term Frequencey-Inverse Document Frequency) refers to Word Frequency-Inverse Document Frequency, which belongs to the category of numerical statistics. Using TF-IDF, we are able to learn the importance of a word for a document in the dataset.

Concept of TF-IDF

TF-IDF has two parts, word frequency and inverse document frequency. Firstly, word frequency is introduced, this word is very intuitive, word frequency indicates how often each word appears in a document or dataset. The equation is as follows:

TF(t) = number of times word t occurs in a document / total number of words in this document

The second part - the inverse document frequency actually tells us how important a word is to the document. This is because when calculating the TF we assign equal importance to each word, the more times it occurs the higher its TF is, if it occurs 100 times maybe it doesn't carry as much information compared to other words that occur less, so we need to assign them weights to determine the importance of each word. Use the following equation to get the IDF:

IDF(t) = (number of log10 documents / number of documents containing the word t)

Then, the TF-IDF is calculated as follows:

TF * IDF = (number of occurrences of word t in a document/total number of words in this document) * log10 (number of documents/number of documents containing word t)

to this article on how to view python keywords is introduced to this article, more related python keywords to view examples of content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!