Range Over Built in CLIPS
Ranging Over Built-in Types
In this example, we will demonstrate how to iterate over different built-in data structures using Python.
Explanation
Summing the numbers in a list: Here, we iterate over the elements of the list
nums
and calculate the sum of its elements.Getting the index and value from a list: We use
enumerate
to get both the index and the value while iterating over the list. When the value equals 3, we print the index.Iterating over key/value pairs in a dictionary: Using the
.items()
method, we iterate over the key/value pairs of the dictionarykvs
, and print each pair.Iterating over keys in a dictionary: We iterate over the keys of the dictionary
kvs
using the.keys()
method.Iterating over Unicode code points in a string: We use
enumerate
to get both the index and the Unicode code point (rune
) of each character in the string"go"
. We useord()
to get the Unicode code point of each character.
Running the Program
To run the program, save the code in a file named range_over_builtin_types.py
and execute it with Python.
This will output: