Generate a list of uppercase letters using ASCII values


This Python code generates a list called uppercase_letters using a list comprehension to create uppercase letters of the English alphabet. It does this by using the chr() function to convert ASCII values to characters. Here's how the code works:

  • uppercase_letters = [chr(code) for code in range(65, 91)]: This line initializes a variable named uppercase_letters and assigns it the result of a list comprehension.
    • for code in range(65, 91): This part sets up a loop that iterates through ASCII values ranging from 65 to 90 (inclusive). In the ASCII table, these values correspond to uppercase letters 'A' to 'Z'.
    • chr(code): For each ASCII value in the specified range, this expression uses the chr() function to convert it into the corresponding character. chr() takes an ASCII value as input and returns the character associated with that value.
    • [chr(code) for code in range(65, 91)]: This is the list comprehension itself. It iterates through the ASCII values for uppercase letters and includes the corresponding characters in the new list.
  • print(uppercase_letters): This line of code prints the uppercase_letters list to the console.

Source Code

uppercase_letters = [chr(code) for code in range(65, 91)]
print(uppercase_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