HOMEWORK DELIVERY
STEP 1
STEP 2
βœ‰ Export your solutions, then commit to submit...

Submit Failed β€” Enter Manually

Intro to Python

4 min read β€’ Assignment

Intro to Python πŸ‰

Python is one of the most popular and versatile programming languages in the world. Known for its readability and simplicity, it allows developers to focus on solving problems rather than dealing with complex syntax. Whether you’re building web applications, automating tasks, analyzing data, or experimenting with artificial intelligence, Python provides a rich ecosystem of tools and libraries to help you succeed.

Why Python?

  1. Readable and Simple Syntax
    • Python code is often close to plain English, making it easier for beginners to learn and professionals to maintain.
  2. Cross-Domain Use
    • Web Development: Frameworks like Django and Flask.
    • Data Science & AI: Libraries such as NumPy, Pandas, TensorFlow, PyTorch.
    • Automation: Scripts for repetitive tasks and system operations.
    • Game Development & More: Tools like Pygame and Kivy.
  3. Community Support
    • Python has a vast global community, offering countless tutorials, forums, and open-source packages.

The Developer’s Mindset

As you begin Python development, focus on:

  • Experimentation: Try small scripts and build confidence.
  • Problem-Solving: Use Python to automate or simplify real tasks.
  • Incremental Growth: Learn the core first (variables, loops, functions), then explore libraries that match your interests.

Review β€œData Types”

Just like JavaScript, Python has two main categories of data types: primitive types (which store simple values directly) and reference types (which store references to more complex data).

Primitive Data Types

In JavaScript: You learned about Number, String, Boolean, Undefined, Null, Symbol, and BigInt as primitive types.

In Python: The most common primitive types are:

  • int (integer numbers, like 42)
  • float (decimal numbers, like 3.14)
  • str (strings, like 'hello')
  • bool (Boolean values: True or False)
  • NoneType (the special value None, similar to JavaScript’s null)

Python does not have undefined, symbol, or bigint types, but the core idea is the same: these types hold simple, single values.

Reference Data Types

In JavaScript: You used Object, Array, and Function as reference types. These store references (links) to more complex data structures.

In Python: The most common reference types are:

  • list (like JavaScript arrays: [1, 2, 3])
  • dict (like JavaScript objects: {'name': 'Mario', 'score': 0})
  • set (a collection of unique values: {'apple', 'banana'})
  • function (functions are also objects in Python)

When you assign a reference type in Python, you are assigning a reference (a link) to the object, not a copy of the objectβ€”just like in JavaScript.

Why does this matter? Understanding the difference between primitive and reference types helps you predict how variables behave when you assign, copy, or modify them.

Hack: Python Dictionary Key-Value

Why?

Defining user properties in key-value format is a requirement of most computer languages. In the user_profile modify code to track a new property (for example, a user’s favorite color, login count, or last activity).

CODE_RUNNER: Dictionary Key-Value Hack

β€” 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

print(β€˜user_id:’, user_id, β€˜ type:’, type(user_id))
print(β€˜user_name:’, user_name, β€˜ type:’, type(user_name))
print(β€˜user_email:’, user_email, β€˜ type:’, type(user_email))
print(β€˜is_active:’, is_active, β€˜ type:’, type(is_active))
print(β€˜last_login:’, last_login, β€˜ type:’, type(last_login))

β€” 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:’, user_profile, β€˜ type:’, type(user_profile))

list: login history (empty to start)

login_history = [] print(β€˜login_history:’, login_history, β€˜| type:’, type(login_history))

set: unique permissions

permissions = set([β€˜read’, β€˜write’, β€˜delete’]) print(β€˜permissions:’, permissions, β€˜| type:’, type(permissions))

function: a simple function to greet the user

def greet(user): print(f”Hello, {user[β€˜name’]}!”)

print(β€˜greet:’, greet, β€˜| type:’, type(greet)) greet(user_profile)

Review Python β€œClasses” and β€œObjects”

Classes

Define a class class User Define a constructor def __init__(self, user_id, name, email, active, scores, roles, last_login)

Define methods

Define procedures inside the class (methods) that enable interaction with the object” def add_score(self, score)

Create a variable

Define an instance of the class: alice = User(101, 'Alice', 'alice@example.com', True, [0.91, 0.87, 0.76, 0.55, 0.92], ['student', 'scrummer'], None)

Hack: Python Class and Instances

Why?

Defining user object is a key to most programming languages. Think about and add something you would want to track in a user class (for example, use the properties you defined in previous hack).

Define multiple user objects.

CODE_RUNNER: Python-Class Hack

’’’

β€” Object Type β€”

Define a User class to encapsulate user data and behavior

’’’ class User: # Constructor to initialize user properties in the object 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

# String representation of the User object  
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) print(alice) print(alice.name, round(alice.average_score(), 2), alice.scores) alice.name = β€˜Alice Smith’ alice.add_score(0.95) print(alice.name, round(alice.average_score(), 2), alice.scores) print(john) print(john.name, round(john.average_score(), 2), john.scores)

Submit Assignment

Need to update a submission later? Open the submissions dashboard.

Course Timeline