Create a list of alphabets dynamically
listx = []
for i in range(ord('a'),ord('z')+1):
listx.append(chr(i))
print(listx)
#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']
ord(char)
: Theord
function is used to get the Unicode code point (integer representation) of a character. When you pass a character as an argument toord
, it returns an integer representing the Unicode code point of that character. For example,ord('a')
returns 97, which is the Unicode code point for the lowercase letter 'a'.chr(unicode_code_point)
: Thechr
function is used to convert a Unicode code point (an integer) back into its corresponding character. When you pass an integer as an argument tochr
, it returns the character represented by that Unicode code point. For example,chr(97)
returns 'a', which is the character corresponding to the Unicode code point 97.
Create a dictionary with an example output
{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L', 'm': 'M', 'n': 'N', 'o': 'O', 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U', 'v': 'V', 'w': 'W', 'x': 'X', 'y': 'Y', 'z': 'Z'}
we can make this possible by using dictionary comprehension
listx = {}
listx = {chr(i):chr(i).upper() for i in range(ord('a'),ord('z')+1)}
print(listx)
Create a dictionary for the below output
{ 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26 }
listx = {}
listx = {chr(ord('a')+i):i+1 for i in range(0,20)}
print(listx)
Create a dictionary for the below output
{ 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26 }
listx = {}
listx = {i-96:chr(i) for i in range(ord('a'),ord('z')+1)}
print(listx)