Intro to Python
- Intro to Python π
- Review βData Typesβ
- Hack: Python Dictionary Key-Value
- Review Python βClassesβ and βObjectsβ
- Hack: Python Class and Instances
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?
- Readable and Simple Syntax
- Python code is often close to plain English, making it easier for beginners to learn and professionals to maintain.
- 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.
- 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, like42)float(decimal numbers, like3.14)str(strings, like'hello')bool(Boolean values:TrueorFalse)NoneType(the special valueNone, similar to JavaScriptβsnull)
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.