Для обеспечения удобства пользователей данный сайт использует файлы cookie
def fibonacci(n): a, b = 0, 1 for _ in range(n): print(a, end=" ") a, b = b, a + b | Mistake | How to Think | Python Fix | |---------|--------------|-------------| | Off‑by‑one | “Is my last iteration correct?” | Check range(start, stop) – stop is exclusive. | | Variable not defined | “Where was this variable created?” | Define before use. | | Wrong output | “Add print statements to see intermediate values.” | Use print() or debugger ( pdb ). |
# Problem: Tell if number is even or odd def even_or_odd(num): if num % 2 == 0: return "even" else: return "odd" (Variables) When to use: Remembering things as you go.
# Problem: Find largest number in a list def find_max(numbers): current_max = numbers[0] # track state for num in numbers: if num > current_max: current_max = num return current_max Exercise 1: FizzBuzz (testing conditionals + loops) Write a program that prints 1 to 100. For multiples of 3 print “Fizz”, for 5 “Buzz”, for both “FizzBuzz”.
# Problem: Sum numbers 1 to N # Pseudocode: total = 0; for each i from 1 to N, add i to total def sum_to_n(n): total = 0 for i in range(1, n+1): total += i return total (Conditionals) When to use: Different actions based on data. Python tool: if / elif / else
| Step | Action | Python Example Thinking | |------|--------|--------------------------| | 1 | the problem | “What are the inputs? Outputs? Constraints?” | | 2 | Plan without code | Write steps in plain English (pseudocode). | | 3 | Implement in Python | Translate pseudocode to Python. | | 4 | Test & Debug | Run with sample inputs, fix errors. | 2. Core Programming Patterns (with Python) Pattern A: Repetition (Loops) When to use: Doing the same action many times. Python tool: for , while
for i in range(1, 101): if i % 15 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i) Check if a word reads the same backward.
def is_palindrome(word): # Remove spaces and lowercase cleaned = word.replace(" ", "").lower() return cleaned == cleaned[::-1] # slicing trick def are_anagrams(word1, word2): return sorted(word1.lower()) == sorted(word2.lower()) Exercise 4: Fibonacci (recursion or iteration) Print the first N Fibonacci numbers.
def fibonacci(n): a, b = 0, 1 for _ in range(n): print(a, end=" ") a, b = b, a + b | Mistake | How to Think | Python Fix | |---------|--------------|-------------| | Off‑by‑one | “Is my last iteration correct?” | Check range(start, stop) – stop is exclusive. | | Variable not defined | “Where was this variable created?” | Define before use. | | Wrong output | “Add print statements to see intermediate values.” | Use print() or debugger ( pdb ). |
# Problem: Tell if number is even or odd def even_or_odd(num): if num % 2 == 0: return "even" else: return "odd" (Variables) When to use: Remembering things as you go. think like a programmer python edition pdf
# Problem: Find largest number in a list def find_max(numbers): current_max = numbers[0] # track state for num in numbers: if num > current_max: current_max = num return current_max Exercise 1: FizzBuzz (testing conditionals + loops) Write a program that prints 1 to 100. For multiples of 3 print “Fizz”, for 5 “Buzz”, for both “FizzBuzz”. def fibonacci(n): a, b = 0, 1 for
# Problem: Sum numbers 1 to N # Pseudocode: total = 0; for each i from 1 to N, add i to total def sum_to_n(n): total = 0 for i in range(1, n+1): total += i return total (Conditionals) When to use: Different actions based on data. Python tool: if / elif / else | # Problem: Tell if number is even
| Step | Action | Python Example Thinking | |------|--------|--------------------------| | 1 | the problem | “What are the inputs? Outputs? Constraints?” | | 2 | Plan without code | Write steps in plain English (pseudocode). | | 3 | Implement in Python | Translate pseudocode to Python. | | 4 | Test & Debug | Run with sample inputs, fix errors. | 2. Core Programming Patterns (with Python) Pattern A: Repetition (Loops) When to use: Doing the same action many times. Python tool: for , while
for i in range(1, 101): if i % 15 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i) Check if a word reads the same backward.
def is_palindrome(word): # Remove spaces and lowercase cleaned = word.replace(" ", "").lower() return cleaned == cleaned[::-1] # slicing trick def are_anagrams(word1, word2): return sorted(word1.lower()) == sorted(word2.lower()) Exercise 4: Fibonacci (recursion or iteration) Print the first N Fibonacci numbers.