How do you assign a value to z in python?

I am noobie. I am trying to assign values from A to Z. This is very long.

   A= 97
   B= 98
   C= 99
   D= 100
   ...
   Z= 122

What is the best way to do it?

asked Aug 31, 2017 at 15:31

How do you assign a value to z in python?

9

You can do it like that:

import string

alpha_dict = {k: ord(k) for k in string.ascii_lowercase}
print(alpha_dict)  # {'r': 114, 'l': 108, 'z': 122, ...}

And access your variables like alpha_dict['a'].


Note that the values you have in your example correspond to the lowercase letters but you are naming your variables with uppercase. If you actually want the uppercase, instead of looping through ascii_lowercase loop over ascii_uppercase

answered Aug 31, 2017 at 15:36

How do you assign a value to z in python?

Ma0Ma0

14.8k3 gold badges33 silver badges62 bronze badges

Instead of hard-coding every letter you can use ord('letter'). For example ord('a') will return the number 97 which stands for the letter a in ASCII.

EDIT:

I used the code of Ev. Kounis for producing the dictionary because mine was wrong. If there's any problem I can delete it.

import string

alpha_dict = {k: ord(k) for k in string.ascii_uppercase}

'''Iterates in keys of alpha_dict, turning the key to lowercase, removes 
 the old key and replaces it with the new lowercase one'''
for key in alpha_dict:
    lower = key.lower()
    alpha_dict[lower] = alpha_dict.pop(key)

print(alpha_dict)

answered Aug 31, 2017 at 15:36

vmankvmank

6251 gold badge6 silver badges18 bronze badges

2

Given a list of elements, assign similar alphabet to same element.

Input : test_list = [4, 5, 2, 4, 2, 6] 
Output : [‘a’, ‘b’, ‘c’, ‘a’, ‘c’, ‘d’] 
Explanation : Alphabets assigned to elements as occurring.

Input : test_list = [4, 5, 2, 4, 2, 6] 
Output : [‘a’, ‘b’, ‘c’, ‘a’, ‘c’] 
Explanation : Alphabets assigned to elements as occurring. 
 

Method #1 : Using ascii_lowercase() + loop + list comprehension 

In this, we extract all lowercase alphabets using lowercase(), and create dictionary mapping same element to similar character, post that we flatten that to appropriate index using list comprehension.

Python3

import string

test_list = [4, 5, 2, 4, 2, 6, 5, 2, 5]

print("The original list : " + str(test_list))

temp = {}

cntr = 0

for ele in test_list:

    if ele in temp:

        continue

    temp[ele] = string.ascii_lowercase[cntr]

    cntr += 1

res = [temp.get(ele) for ele in test_list]

print("The mapped List : " + str(res))

Output

The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5]
The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']

Method #2 : Using defaultdict() + ascii_lowercase() + iter()

In this we use defaultdict() to assign values to similar elements, ascii_lowercase() is used to get all lowercase all lowercased alphabets.

Python3

from collections import defaultdict

import string

test_list = [4, 5, 2, 4, 2, 6, 5, 2, 5]

print("The original list : " + str(test_list))

temp = iter(string.ascii_lowercase)

res = defaultdict(lambda: next(temp))

res = [res[key] for key in test_list]

print("The mapped List : " + str(list(res)))

Output

The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5]
The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(N)

Auxiliary Space: O(N)


How do you declare a to z in Python?

“print all alphabets from a to z in python” Code Answer.
alphabet = [];.
# ord represent the character unicode code, A to 65..
# chr convert 65 to A..
#variable alphabet get all value..
for i in range(ord('A'), ord('Z') + 1):.
alphabet. append(chr(i)).
print(alphabet).
#ganesh..

How do you assign a value in Python?

The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”. After executing this line, this number will be stored into this variable.

How do you assign a numeric value to alphabets in Python?

Use the ord() Function to Convert Letters to Numbers in Python. The ord() function in Python is utilized to return the Unicode , or in this case, the ASCII value of a given letter of the alphabet. We will apply the ord() function to the letters and subtract 96 to get the accurate ASCII value.

How do you assign a value to a string in Python?

To assign it to a variable, we can use the variable name and “=” operator. Normally single and double quotes are used to assign a string with a single line of character but triple quotes are used to assign a string with multi-lines of character. This is how to declare and assign a variable to a string in Python.