Create a list of lowercase letters


This Python code creates a list called lowercase_letters using a list comprehension to generate lowercase letters of the English alphabet. Here's how the code works:

  • lowercase_letters = [chr(x) for x in range(ord('a'), ord('z')+1)] : This line of code initializes a variable named lowercase_letters and assigns it the result of a list comprehension.
    • range(ord('a'), ord('z')+1): This part of the code generates a range of integer values that correspond to the Unicode code points of lowercase letters in the English alphabet. ord('a') returns the Unicode code point of the letter 'a', and ord('z') returns the Unicode code point of the letter 'z'. Adding 1 ensures that 'z' is included in the range.
    • chr(x): For each integer x in the range, this expression converts it back to a character using the chr() function. chr(x) returns the character that corresponds to the Unicode code point x.
    • [chr(x) for x in range(ord('a'), ord('z')+1)]: This is the list comprehension itself. It iterates through the range of Unicode code points for lowercase letters and converts each code point back to its corresponding character. These characters (lowercase letters) are collected into a new list.
  • print(lowercase_letters): This line of code prints the lowercase_letters list to the console.

Source Code

lowercase_letters = [chr(x) for x in range(ord('a'), ord('z')+1)]
print(lowercase_letters)

Output

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Example Programs