Python Variables and assignments
Assign variables and examine in Debugger using this notebook.
Python Variables: Homework Notbook`
Use this Notbook to perform Python Variable Homework Hacks.
'''
Add-Dictionary Homework Instructions:
- Add multi-line comment to describe purpose of primitive types, replace existing single line comment
- Add multi-line comment to describe purpose of dictionary type, replace existing single line comment
- Add dictionary inside the user_profile dictionary, a favorites dictionary as dictionary in user_profile
- Print the user_profile.favorites dictionary
'''
# --- Primitive Types ---
user_id = 101 # int: unique user ID
user_name = 'Alice' # str: user name
user_email = 'alice@example.com' # str: user email
is_active = True # bool: is the user active?
last_login = None # NoneType: no login yet
# --- Reference Types ---
user_profile = { # dict: user profile as a dictionary
'id': user_id,
'name': user_name,
'email': user_email,
'active': is_active,
'scores': [0.91, 0.87, 0.76, 0.55, 0.92], # reference to a list of float: user scores
'roles': ['student', 'scrummer'], # reference to a list of str: user roles
'last_login': last_login
} # dict: user profile as a dictionary
print(user_profile['name'])
print('user_profile:', user_profile, '| type:', type(user_profile))
print('user_profile["name"]:', user_profile['name'], '| type:', type(user_profile['name']))
Alice
user_profile: {'id': 101, 'name': 'Alice', 'email': 'alice@example.com', 'active': True, 'scores': [0.91, 0.87, 0.76, 0.55, 0.92], 'roles': ['student', 'scrummer'], 'last_login': None} | type: <class 'dict'>
user_profile["name"]: Alice | type: <class 'str'>
'''
Add-Behavior Homework Instructions:
- Comment the code for class
- Comment the code for objects and dot notation usage
- Add new tracking item, for instance the favorites dictionary created in Add-Dictionary Homework
- Add behavior using the method to the class that uses the new tracking item
'''
class User:
def __init__(self, user_id, name, email, active, scores, roles, last_login):
self.id = user_id
self.name = name
self.email = email
self.active = active
self.scores = scores
self.roles = roles
self.last_login = last_login
def __repr__(self):
return (
f'User(id={self.id}, name={self.name}, email={self.email}, active={self.active}, ' f'scores={self.scores}, roles={self.roles}, last_login={self.last_login})'
)
def add_score(self, score):
self.scores.append(score)
def average_score(self):
return sum(self.scores) / len(self.scores) if self.scores else 0
alice = User(101, 'Alice', 'alice@example.com', True, [0.91, 0.87, 0.76, 0.55, 0.92], ['student', 'scrummer'], None)
john = User(102, 'John', 'john@example.com', True, [0.85, 0.80, 0.78, 0.90, 0.88], ['student'], None)
# 4th hack: add a favorites dictionary for each object using dot notation
alice.favorites = {}
john.favorites = {}
# add behavior for the new tracking item (example)
alice.favorites['color'] = 'blue'
john.favorites['food'] = 'pizza'
print(alice)
print(alice.favorites)
print(john)
print(john.favorites)
User(id=101, name=Alice, email=alice@example.com, active=True, scores=[0.91, 0.87, 0.76, 0.55, 0.92], roles=['student', 'scrummer'], last_login=None)
{'color': 'blue'}
User(id=102, name=John, email=john@example.com, active=True, scores=[0.85, 0.8, 0.78, 0.9, 0.88], roles=['student'], last_login=None)
{'food': 'pizza'}