pyodide: loading…

[practice]Python Basics

Loops & Conditionals

# theory

if/elif/else evaluates top to bottom and stops at the first match:

if score >= 90: grade = "A"
elif score >= 80: grade = "B"
elif score >= 70: grade = "C"
else: grade = "F"

Comparison operators you'll use: ==, !=, <, >, <=, >=, and in for membership ("apple" in fruits).

for iterates anything iterable:

for fruit in fruits:
    print(fruit)

range() makes integer sequences. range(5) is 0..4, range(2, 6) is 2..5, range(0, 10, 2) steps by 2.

enumerate() when you need the index alongside the value:

for i, fruit in enumerate(fruits):
    print(f"{i}: {fruit}")

while for "keep going until":

count = 0
while count < 5:
    print(count)
    count += 1

Rule of thumb: for when the iteration count is knowable up front, while when it depends on runtime state.

# examples [3]

# example 01 · counting with conditions

Loop through and count items meeting criteria

1
2
3
4
5
6
7
8
9
🐍
Loading PythonSetting up pandas & numpy...
# example 02 · using enumerate

Track position and value together

1
2
3
4
5
🐍
Loading PythonSetting up pandas & numpy...
# example 03 · building results with loops

Create new lists based on conditions

1
2
3
4
5
6
7
8
9
10
🐍
Loading PythonSetting up pandas & numpy...

# challenges [2]

# challenge 01/02todo
Loop through numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and print only the even numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🐍
Loading PythonSetting up pandas & numpy...
# challenge 02/02todo
Use enumerate to print each name in names = ['Alice', 'Bob', 'Carol'] with their 1-based position like '1. Alice'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🐍
Loading PythonSetting up pandas & numpy...

# project

# project-challenge

thread: SF Permits Analysis · reward: 50 xp

# brief

The planning department needs a quick count of active vs completed permits. Count how many permits are 'issued' versus 'complete' to generate a status report.

# task

Count Permit Statuses

# your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
🐍
Loading PythonSetting up pandas & numpy...