from typing import TypedDict, List
class UsersTypes(TypedDict):
name: str
favorite_color: str
age: int
is_adult: bool
users: List[UsersTypes] = [
{
"name": "Alice".lower(),
"favorite_color": "Blue",
"age": 25,
"is_adult": True
},
{
"name": "Bob".lower(),
"favorite_color": "Green",
"age": 17,
"is_adult": False
},
{
"name": "Charlie".lower(),
"favorite_color": "Red",
"age": 30,
"is_adult": True
}
]1.6 Project
Working with Variables
For this project, let’s begin by creating a data store from a user. To do this we will use the built in input() function to allow users to provide information.
Our job is to create a list of information on 3 users. For each user we will ask for them to include their name, favorite color, and age. In addition, we will include a variable to indicate if the user is an adult or not. We will base that value on whether or not the age provided is greater than or equal to 18.
Let’s start by creating an empty list to store the users infromation:
users = []Notice the nameof the variable is users, which is a plural noun. This indicates that it will hold multiple users.
Next, we need to get information for user1. We will use the input() function to get the user’s name, favorite color, and age. We will also determine if the user is an adult based on their age.
user1_name: str = input("User 1: What is your name? ")
user1_favorite_color: str = input("User 1: What is your favorite color? ")
user1_age: int = int(input("User 1: What is your age? Only include numbers, please: "))Now we have information for user1. Notice that we used the int() function to convert the age input from a string to an integer. This is important because we will need to perform calculations with the age. All inputs are included by default as strings, so we must convert it to an integer If a user provides a non-numeric entry, the int() function will fail with an error. There are ways to alleviate this problem, and we will introduce them in Chapter 2. For now, we will risk the error since we are just learning.
Now let’s create a variable for user1 to see if he or she is an adult. We will use a boolean variable to indicate this. A boolean variable can only be True or False. In this case, we will set it to True if the user is 18 or older, and False otherwise.
user1_is_adult: bool = user1_age >= 18Now we have all the information for user1. Let’s create a dictionary to store this information. A dictionary is a collection of key-value pairs, where each key is unique. We will use the user’s name as the key and the other information as the value. We might also want to store all names in their lower case form to keep consistent casing. Some people might not upper case their first name on accident for example. We will simply use the lower() string method to compel all names to lower case.
user1 = {
"name": user1_name.lower(),
"favorite_color": user1_favorite_color,
"age": user1_age,
"is_adult": user1_is_adult
}Now we can add user1 to the users list. We can use the append() method to add user1 to the list:
users.append(user1)We can repeat this process for user2 and user3. We will just copy the logic we used above and change 1 to 2 or 3, depending on the user. There is a superior way to produce this same outcome using loops (chapter 3) and functions (chapter 4), but we haven’t gotten there, yet.
user2_name: str = input("User 1: What is your name? ")
user2_favorite_color: str = input("User 1: What is your favorite color? ")
user2_age: int = int(input("User 1: What is your age? Only include numbers, please: "))
user3_name: str = input("User 1: What is your name? ")
user3_favorite_color: str = input("User 1: What is your favorite color? ")
user3_age: int = int(input("User 1: What is your age? Only include numbers, please: "))
user2 = {
"name": user2_name.lower(),
"favorite_color": user2_favorite_color,
"age": user2_age,
"is_adult": user2_age >= 18
}
users.append(user2)
user3 = {
"name": user3_name.lower(),
"favorite_color": user3_favorite_color,
"age": user3_age,
"is_adult": user3_age >= 18
}
users.append(user3)To save a couple of lines of code, we did not create a separate variable for user2_is_adult and user3_is_adult. Instead, we just used the expression user2_age >= 18 and user3_age >= 18 directly in the dictionary. This is a common practice in programming to reduce redundancy.
Now we have a list of users. For the sake of this project, I have manually created an example of the users list.
Now that we have our list of users, we can return information about the list of users. For instance, let’s see only the information for “Bob.” We can do this in two ways. First, we know that Bob is in the second location (or index 1)1 of the list. That means we can use index notation to extract that index of the list:
bob: UsersTypes = users[1]
print(bob){'name': 'bob', 'favorite_color': 'Green', 'age': 17, 'is_adult': False}
Now we can choose to change the name of Bob to “Robert” by accessing the name key in the dictionary and reassigning it. Here we want to reassign the name directly on the dictionary in the list. Bob, which we just created is a new variable separate from users, so if we want to update the users list, we have to access it directly.
users[1]['name'] = "robert"
print(users[1]){'name': 'robert', 'favorite_color': 'Green', 'age': 17, 'is_adult': False}
To access the namee variable on index 1 of our users list we use the syntax users[1]['name']. This accesses the second element of the list (index 1) and then accesses the name key in the dictionary at that index. Then we only need to reassign it using the equals sign (=).
We could update the dictionary differently. Instead of chaning index notation together, we could use the update() method on the dictionary.
users[1].update({"name": "bob"})
print(users[1]){'name': 'bob', 'favorite_color': 'Green', 'age': 17, 'is_adult': False}
Some programmers prefer this method because it is more explicit about what is being updated. It also allows us to update multiple keys at once if we wanted to.
Now if we want to access only the name of Bob, we can do so by accessing the name and then using the capitalize() string method to capitalize only the first letter.
print(users[1]['name'].capitalize())Bob
We have successfully created a list of users, stored their information in dictionaries, and accessed and modified that information.
Footnotes
Remember that indexes use base 0 notation.↩︎