Skip to content

Commit f2e44d4

Browse files
Merge pull request #5 from StructuralWizard/include-day1
Include Day2
2 parents 76bc73a + 502d43e commit f2e44d4

File tree

9 files changed

+748
-21
lines changed

9 files changed

+748
-21
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ package.json
1717
vendor/
1818

1919
# Ignore folders for mermaid diagrams
20-
.assets/
20+
.assets/
21+
22+
# Environment files
23+
.venv

_config.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ just_the_docs:
1818
name: Python Code
1919
description: Python code examples for the 10 Days of Code
2020
nav_exclude: false # to exclude the entire collection from the main navigation
21-
nav_fold: true # to fold the collection, instead of showing links to all its top-level pages2
21+
nav_fold: false # to fold the collection, instead of showing links to all its top-level pages2
2222
search_exclude: false # to exclude all the collection pages from search results
2323
# For copy button on code
2424
enable_copy_code_button: true
@@ -39,3 +39,23 @@ mermaid:
3939
# path: "/assets/js/mermaid.min.js"
4040
# Note: copy both `mermaid.esm.min.mjs` (v10+) or `mermaid.min.js` (<v10) and the associated
4141
# `.map` file from the specified version of `mermaid/dist` to `/assets/js/`.
42+
43+
# Color scheme currently only supports "dark", "light"/nil (default), or a custom scheme that you define
44+
color_scheme: nil
45+
46+
callouts_level: quiet # or loud
47+
callouts:
48+
highlight:
49+
color: yellow
50+
important:
51+
title: Important
52+
color: blue
53+
new:
54+
title: New
55+
color: green
56+
note:
57+
title: Note
58+
color: purple
59+
warning:
60+
title: Warning
61+
color: red

docs/Day1/index.md

Lines changed: 139 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ Feeling a bit lost when your **Vibe Coders** agents spit out lines of python cod
2121

2222

2323
---
24-
## 1.1. 🧭 How is it explained? <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
24+
## 🧭 1.1. How is it explained? <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
2525

2626
To cover all the basic Python concepts, let’s create a **mini text-based** game called **“Monster Maze”**. It’s fun, simple, and touches on all the listed topics.
2727

2828
You are stuck in a maze. Each turn, you decide to move through rooms, pick up items, and fight random monsters. The goal is to find the **magic key** to escape.
2929

3030
---
3131

32-
## 1.2. 🧠 What you will learn? <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
32+
## 🧠 1.2. What you will learn? <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
3333

3434
| Concept | Covered in | Use/Purpose |
3535
| ----------------------- | --------------------------------------------------------- | ------------------------------------------- |
@@ -61,9 +61,9 @@ You are stuck in a maze. Each turn, you decide to move through rooms, pick up it
6161

6262
---
6363

64-
### 🧱 1.3. Step-by-Step Coding <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
64+
## 🧱 1.3. Step-by-Step Coding <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
6565

66-
#### 1.3.1. Import module and Comments <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
66+
### 📦 1.3.1. Import module and Comments <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
6767
The import statement in Python allows you to include and use code from other modules in your current program. For example the in code below:
6868
```python
6969
# Simple import - access with module_name.item
@@ -74,7 +74,7 @@ random_number = random.randint(1, 10) # Generates a random integer between 1 an
7474
Python searches for a module named "random" or file named random.py in several locations and executes its code once. A namespace named "random" is created in your program and then you can access the module's functions and variables. In the example the function randint is used to create a random integer number between 1 and 10.
7575
In python, anything written after "#" until the end of the line is interpreted as a comment and editors generally show them in green or grey.
7676

77-
#### 1.3.2. Constants and Lists <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
77+
### 📋 1.3.2. Constants and Lists <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
7878

7979
In Python, variables that are meant to remain unchanged throughout a program are often written in ALL_CAPS to indicate they are **constants**. While Python doesn't enforce this (variables can still be changed), it's a convention to signal to other programmers that these values shouldn't be modified.
8080

@@ -101,7 +101,7 @@ Lists are incredibly versatile in Python:
101101
Later in our game, we'll select random elements from these lists using `random.choice()` to create unpredictable gameplay.
102102

103103

104-
#### 1.3.3. Global variables, Functions and Print <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
104+
### 🌐 1.3.3. Global variables, Functions and Print <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
105105
Global variables in Python are variables that are defined outside of any function and can be accessed throughout the program, including inside functions. The Global variable found_key gets the value False at the beginning of monster_maze.py
106106
```python
107107
# Global variable
@@ -134,7 +134,7 @@ def print_welcome():
134134
The string written below the function with triple """ contains a
135135
short documentation text named **"Docstrings"** which is used to convey the purpose and functionality of Python functions, modules, and classes.
136136

137-
#### 1.3.4. Dictionaries and Functions with Output <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
137+
### 🔑 1.3.4. Dictionaries, Lists of Dictionaries, Tuples and Slicing <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
138138

139139
**Dictionaries** are one of Python's most powerful data structures. They store data as key-value pairs, allowing you to retrieve values quickly using their associated keys (similar to how you look up definitions in a real dictionary). Dictionaries are created using curly braces `{}` with each key-value pair separated by commas.
140140

@@ -157,6 +157,79 @@ In this dictionary:
157157
- You access values using their keys: `player["health"]` would give you `100`
158158
- Values can be modified: `player["health"] -= 20` would reduce health by 20
159159

160+
**Lists of Dictionaries** are powerful data structures that can store multiple records with named fields. They're ideal for collections of similar objects.
161+
162+
```python
163+
# List of dictionaries for multiple players
164+
players = [
165+
{"name": "Alex", "health": 100, "inventory": ["sword"]},
166+
{"name": "Taylor", "health": 80, "inventory": ["potion", "shield"]},
167+
{"name": "Jordan", "health": 120, "inventory": []}
168+
]
169+
170+
# Accessing data
171+
print(players[0]["name"]) # Output: Alex
172+
print(players[1]["inventory"][0]) # Output: potion
173+
174+
# Adding new player to the list
175+
players.append({"name": "Casey", "health": 90, "inventory": ["map"]})
176+
177+
# Looping through all players
178+
for player in players:
179+
print(f"{player['name']} has {player['health']} health")
180+
```
181+
182+
**Tuples** are immutable sequences similar to lists but enclosed in parentheses. Once created, their values cannot be changed.
183+
184+
```python
185+
# Basic tuple creation
186+
coordinates = (10, 20)
187+
rgb_color = (255, 0, 128)
188+
189+
# Tuple unpacking - assigns each value to a variable
190+
x, y = coordinates
191+
print(f"X: {x}, Y: {y}") # Output: X: 10, Y: 20
192+
193+
# Tuples can contain mixed data types
194+
player_data = ("Alex", 100, ["sword", "potion"])
195+
name, health, inventory = player_data
196+
197+
# Tuples are immutable - this would cause an error:
198+
# coordinates[0] = 15
199+
200+
# But if a tuple contains a mutable object, that object can be modified:
201+
player_data[2].append("shield") # This works!
202+
```
203+
204+
**Slicing** allows you to extract portions of sequences (lists, strings, tuples) using `[start:stop:step]` syntax.
205+
206+
```python
207+
# Slicing a list
208+
items = ["sword", "shield", "potion", "key", "map"]
209+
first_two = items[0:2] # ["sword", "shield"]
210+
last_three = items[2:] # ["potion", "key", "map"]
211+
middle_items = items[1:4] # ["shield", "potion", "key"]
212+
213+
# Negative indices count from the end
214+
last_item = items[-1] # "map"
215+
second_last = items[-2] # "key"
216+
everything_but_last = items[:-1] # ["sword", "shield", "potion", "key"]
217+
218+
# Step parameter skips elements
219+
every_second = items[::2] # ["sword", "potion", "map"]
220+
reversed_list = items[::-1] # ["map", "key", "potion", "shield", "sword"]
221+
222+
# Slicing strings works the same way
223+
name = "Monster Maze"
224+
first_word = name[:7] # "Monster"
225+
last_word = name[8:] # "Maze"
226+
reversed_name = name[::-1] # "ezaM retsnoM"
227+
```
228+
229+
Slicing is a concise and powerful way to manipulate sequences in Python, while lists of dictionaries and tuples provide flexible options for organizing complex data structures in your games.
230+
231+
### ⚙️ 1.3.5 Functions with Input and Output <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
232+
**Functions with Input** are functions where a variable is passed as value when they are called. This is done in our code when `game_loop(player)` is called in `main()`.
160233
**Functions with Output** are functions that return values to be used elsewhere in your code. In Python, the `return` statement is used to specify what value a function should output. Without a return statement, functions return `None` by default.
161234

162235
Our `create_player()` function above is a perfect example:
@@ -167,7 +240,59 @@ Our `create_player()` function above is a perfect example:
167240

168241
Return values are essential when a function needs to compute or create something that will be used by other parts of your program. In our game, the player dictionary is central to the entire program's state, which is why we have a dedicated function that returns it.
169242

170-
#### 1.3.5. Conditional Statements and String Formatting <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
243+
**Functions with unknown input** In Python, it is possible to create a function that accepts an unknown number of arguments using `*args` and `**kwargs`. Here's a breakdown of when and why we use each:
244+
245+
`*args` (Arbitrary Positional Arguments): Used when you need to create a function that can operate on an unspecified number of inputs of the same type.
246+
247+
How it works:
248+
- The *args syntax in a function definition collects all the extra positional arguments passed to the function into a tuple.
249+
- The name args is a convention; you could use *whatever if you wanted, but *args is widely understood and recommended.
250+
251+
Example:
252+
253+
```python
254+
def sum_all_numbers(*args):
255+
total = 0
256+
for num in args:
257+
total += num
258+
return total
259+
260+
print(sum_all_numbers(1, 2, 3)) # Output: 6
261+
print(sum_all_numbers(10, 20, 30, 40)) # Output: 100
262+
print(sum_all_numbers()) # Output: 0
263+
```
264+
265+
`**kwargs` (Arbitrary Keyword Arguments): used when you want a function to accept any number of keyword arguments (arguments passed with a `key=value` syntax).
266+
267+
How it works:
268+
- The `**kwargs` syntax in a function definition collects all the extra keyword arguments passed to the function into a dictionary.
269+
- The name kwargs is a convention; you could use `**whatever_else` but `**kwargs` is the standard.
270+
271+
Example:
272+
```python
273+
def configure_settings(**kwargs):
274+
settings = {
275+
"theme": "dark",
276+
"font_size": 12,
277+
"language": "en"
278+
}
279+
for key, value in kwargs.items():
280+
settings[key] = value
281+
return settings
282+
283+
print(configure_settings(theme="light", font_size=14))
284+
# Output: {'theme': 'light', 'font_size': 14, 'language': 'en'}
285+
286+
print(configure_settings(language="fr", debug_mode=True))
287+
# Output: {'theme': 'dark', 'font_size': 12, 'language': 'fr', 'debug_mode': True}
288+
289+
print(configure_settings())
290+
# Output: {'theme': 'dark', 'font_size': 12, 'language': 'en'}
291+
```
292+
You can combine `*args` and `**kwargs`, for example `def generic_printer(arg1, *args, **kwargs):`
293+
294+
295+
### 🔀 1.3.6. Conditional Statements and String Formatting <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
171296

172297
**Conditional Statements** (if/elif/else) are fundamental building blocks in Python that allow your program to make decisions. They execute different code blocks based on whether certain conditions are true or false. Let's look at the `describe_room()` function as an example:
173298

@@ -219,7 +344,7 @@ else:
219344
The combination of these features makes our code both functional and readable. Notice how the function uses conditions to create dynamic gameplay (sometimes finding items, sometimes not) and formatted strings to clearly communicate what's happening to the player.
220345

221346

222-
#### 1.3.6. Range() and Logical Operators <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
347+
### 🔢 1.3.7. Range() and Logical Operators <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
223348

224349
**The `range()` Function** is a built-in Python function that generates a sequence of numbers. It's commonly used in for loops to execute code a specific number of times.
225350

@@ -309,7 +434,7 @@ def encounter_monster(player):
309434
exit() # Exits the script
310435
```
311436

312-
#### 1.3.7. While and For Loops to Control Flow. Function Recursion <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
437+
### 🔄 1.3.8. While and For Loops to Control Flow. Function Recursion <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
313438

314439
Here is where we put the computer to properly work for us by using while and for loops to repeat actions. **While loops** execute a block of code repeatedly as long as a condition remains true. They're ideal when you don't know in advance how many iterations you'll need.
315440

@@ -381,7 +506,7 @@ if choice in ["yes", "y"]:
381506

382507
This creates a chain of function calls that continue until a terminating condition is met (finding the key or dying). Recursion is powerful but needs a clear exit condition to avoid infinite recursion.
383508

384-
#### 1.3.8. Main Execution and Flow Diagram <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
509+
### 🏃 1.3.9. Main Execution and Flow Diagram <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
385510

386511
**Python Script Execution** follows a specific order:
387512

@@ -468,7 +593,7 @@ graph TD;
468593
H --> W;
469594
```
470595

471-
#### 1.3.9. Debugging <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
596+
### 🐛 1.3.10. Debugging <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
472597

473598
**Debugging** is the process of finding and fixing errors (bugs) in your code. Common debugging techniques in Python include:
474599

@@ -502,7 +627,7 @@ Good debugging practices:
502627
- Use descriptive print statements
503628
- Check edge cases (empty lists, zero values, etc.)
504629

505-
#### 1.3.10. Refactor and Test, Code Structure and UI Polish <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
630+
### 🔧 1.3.11. Refactor and Test, Code Structure and UI Polish <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
506631

507632
**Refactoring** is the process of restructuring code without changing its behavior. Benefits include:
508633
- Improved readability
@@ -545,7 +670,7 @@ As a final step, thorough testing ensures your code works as expected across dif
545670

546671
---
547672

548-
### 🎯 1.4. Exercises <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
673+
## 🎯 1.4. Exercises <a href="#top" class="back-to-top-link" aria-label="Back to Top">↑</a>
549674

550675
#### 🧪 Practice 1: Custom Weapons
551676
> Modify the `ITEMS` list to include new weapons like "laser", "bow", or "fireball". Have the monster encounter logic recognize them.

0 commit comments

Comments
 (0)