Skip to content

Programming Elements in Python: Names and Markers

Comprehensive Educational Hub: Our platform encompasses a wide array of subject matters, including computer science, programming, school education, professional development, commerce, software tools, and competitive exam preparation, catering to learners in diverse fields.

Programming Elements in Python: Names and Definitions
Programming Elements in Python: Names and Definitions

Programming Elements in Python: Names and Markers

Python 3.11 is equipped with 35 reserved keywords, each serving a distinct role in the language's syntax and control flow. These keywords cannot be used as identifiers (like variable or function names). Here is a list of the keywords along with their primary functions:

| Keyword | Function / Description | |------------|-------------------------------------------------------------------------------------------------------| | and | Logical AND operator | | as | Used to create an alias (e.g., in imports or exception handling) | | assert | For debugging; raises an exception if a condition is false | | async | Defines an asynchronous function or coroutine | | await | Waits for the completion of an asynchronous coroutine | | break | Exits the nearest enclosing loop | | class | Defines a new class | | continue | Skips the rest of the current loop iteration, moves to next iteration | | def | Defines a function | | del | Deletes objects (variables, list items, dictionary keys) | | elif | Else if; used in conditional branching | | else | Defines a block executed if previous conditions are false in if-elif-else or if-except | | except | Catches exceptions in try-except blocks | | False | Boolean false literal | | finally| Block that always executes after try-except, often for cleanup | | for | For loop iterating over a sequence | | from | Imports specific parts of a module | | global | Declares a variable is global (not local) | | if | Conditional execution | | import | Imports a module or package | | in | Tests membership or iteration over a sequence | | is | Tests object identity (whether two references point to the same object) | | lambda | Defines an anonymous function | | None | Represents the null value (no value) | | nonlocal | Refers to variables in the nearest enclosing scope that is not global | | not | Logical NOT operator | | or | Logical OR operator | | pass | Null statement, a placeholder that does nothing | | raise | Raises an exception | | return | Exits a function and optionally returns a value | | True | Boolean true literal | | try | Defines a block to catch exceptions | | while | Loop that continues as long as a condition is true | | with | Wraps the execution of a block with methods defined by a context manager (e.g., file handling) | | yield | Returns a generator value from a generator function |

These keywords form the core syntax building blocks in Python 3.11, enabling control flow, error handling, function and class definition, and more[1][5].

Examples of some keywords usage:

  • to define anonymous functions:
  • to return values from functions:
  • to delete an item from a list:
  • Exception handling keywords , , : This tries to compute division, catches division by zero error, and always executes the finally block regardless of exceptions[2].

This set and functions correspond to Python 3.11 keywords as listed in recent and authoritative sources[1][5]. - Example 10 illustrates the use of the yield keyword in generator functions to produce values one at a time. - Example 4 demonstrates the use of def, if, and else keywords in defining a function. - Example 8 provides an example of the del keyword for deleting a variable or an item from a list or tuple. - Example 5 provides an example of try, except, and raise keywords for error handling. - The keyword module provides functions like iskeyword() and kwlist. - Python Identifiers should start with an alphabet character or an underscore. - Example 11 presents the use of the assert keyword for asserting that a condition is true, raising an exception if it is not. - Python 3.11 has 35 keywords. - Example 15 demonstrates the use of the is keyword for testing if two variables or objects refer to the same object. - The break keyword is used to exit a loop in Python. - Python Keywords are predefined and reserved words with special meanings.

[1] Python Language Reference: https://docs.python.org/3/reference/lexical_analysis.html [2] Python Keywords: https://docs.python.org/3/reference/simple_stmts.html#keywords [5] Python 3.11 Documentation: https://docs.python.org/3.11/

Read also:

Latest