Python Keywords and Identifiers


1) List All Python Keywords

Write code to import the keyword module and print all Python keywords.


2) Check If a Word Is a Keyword

Ask the user for a word and print whether it is a Python keyword.


3) Valid Identifier Check

Ask the user for a string and check if it’s a valid Python identifier (use .isidentifier()).


4) Create Valid Identifiers

Create 5 valid identifiers (variables) and assign them values. Print each variable.


5) Create Invalid Identifiers

List 5 invalid identifier names in a comment and explain why they’re invalid.


6) Test Identifiers Programmatically

Write a loop that checks a list of strings (["abc","123","my-var","_name","for"]) and prints whether each is a valid identifier.


7) Avoid Keywords as Identifiers

Try assigning a value to a keyword like if = 5 and observe the error.


8) Convention — Lowercase Identifiers

Create 3 identifier names following lowercase with underscores convention (PEP8 style) and print them.

Example:


9) Identifier With Numbers

Assign values to valid identifiers that include numbers (e.g., var1, num2), then print.


10) Long Identifier

Create a long identifier (more than 15 characters) that is valid and print its value.


11) Single-Character Identifiers

Create 3 variables with single-character names (like a, b, c) and print them.


12) Mixed Case Identifiers

Create identifiers using camelCase (e.g., myVariableName) and print their values.


13) Compare Identifier and Keyword

Ask user for input string; print whether it’s a keyword, a valid identifier, or neither.


14) Store Keywords in a List

Write code to get all Python keywords as a list and print the count of keywords.


15) Rename Variable for Better Readability

Given code:

Rename x and y to more meaningful identifiers and print them.


16) Invalid Identifiers Explained

In comments, explain why each of the following is invalid:


17) Keywords in Conditions

Write a simple if statement using keyword if, else, and print().

Example:


18) Create a Function With a Good Name

Write a function with a valid identifier name that prints a message.


19) Check Identifier in Code

Define a string in code and then check if it’s a valid identifier using Python’s built-in method.


20) Generate Identifiers Dynamically

Store a list like ["varA","varB","varC"], loop through it, and print whether each is a valid identifier.


🧠 Rules Practiced

✔ Identifiers cannot start with a digit ✔ Identifiers cannot be a Python keyword ✔ Identifiers can contain letters, numbers, and underscores (_) ✔ Python keywords are reserved words (like if, for, while, class, etc.) ✔ Use .isidentifier() to test valid identifiers ✔ Use the keyword module to list keywords


📌 Helpful Code Snippets

List Python keywords

Check if a string is a keyword

Check if a string is valid identifier


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated