搜尋此網誌

2026年7月3日星期五

For loop

animalLookup = {

    'a': ['aardvark', 'antelope'],

    'b': ['bear'],

    'c': ['cat'],

    'd': ['dog'],

}


for letter, animals in animalLookup.items():

    if len(animals) > 1:

        continue

    print(f'Only one animal! {animals[0]}')


for letter, animals in animalLookup.items():

  • .items() gives you key-value pairs from the dictionary.

  • letter will be the key ('a', 'b', etc.).

  • animals will be the list of animals associated with that key.

Example iteration:

  • First loop: letter = 'a', animals = ['aardvark', 'antelope']

  • Second loop: letter = 'b', animals = ['bear']

  • …and so on.


if len(animals) > 1: continue

len(animals) counts how many animals are in the list.
If there are more than one, the loop skips this iteration (continue means “go to the next loop cycle”).
So 'a' (two animals) will be skipped.

print(f'Only one animal! {animals[0]}')

This runs only if the list has exactly one animal.
animals[0] gets the first (and only) item in the list.
It prints a message like:
"Only one animal! bear"

The f before the string tells Python: evaluate expressions inside {}.
{animals[0]} is replaced with the first element of the list.
For 'b', animals[0] = 'bear'.
So the actual string becomes:
"Only one animal! bear"

The curly brackets {} in are part of the f‑string syntax. They tell Python: “evaluate whatever is inside these brackets and insert the result into the string.”

A prime number is a natural number greater than 1 that has no divisors other than 1 and itself.

  • Example: 2,3,5,7,11,13...

  • Non‑prime (composite) numbers can be divided evenly by other numbers (e.g. 6=2×3).


In Python, the symbol ** is the exponentiation operator. It raises a number to the power of another number.

for number in range(2, 100):
    for factor in range(2, int(number ** 0.5) + 1):
        if number % factor == 0:
            break
    else:
        print(f'{number} is prime!')


for number in range(2, 100):
  • Starts at 2 (the first prime).
  • Goes up to 99.

for factor in range(2, int(number ** 0.5) + 1):

Tries dividing number by all integers from 2 up to the square root of number.
Why square root?
If a number has a factor larger than its square root, the matching smaller factor would already have been found.
Example: For 36, factors are 2 × 18, 3 × 12, 6 × 6. Once you check up to 6 (√36), you’ve covered all possibilities.

if number % factor == 0: break

% is the modulus operator (remainder).
If remainder is 0, number is divisible by factor → not prime.
break exits the inner loop immediately.

else: print(f'{number} is prime!')

This else belongs to the for loop, not the if.
It runs only if the loop did not break (meaning no factors were found).
So the number is prime, and it prints the message.

Example trace

  • number = 7

    • Factors checked: 2 → not divisible, 3 → not divisible.

    • Loop ends without break.

    • else runs → prints "7 is prime!".

  • number = 9

    • Factors checked: 2 → not divisible, 3 → divisible.

    • break triggers.

    • else skipped → nothing printed.


The int() function truncates (drops the decimal part), not rounds.

Microsoft Copilot

沒有留言:

發佈留言