PROPOSAL.md
"OIM3640: GitHub settings"
Due to the snowstorm, we're meeting on Webex today.
int
float
str
bool
NoneType
+
-
*
/
//
%
**
def
return
print()
if
elif
else
for
range
while
break
continue
in
Type your answers in the Webex chat!
What is the output?
count = 0 for letter in 'mississippi': if letter == 's': count += 1 print(count)
What keywords can exit a loop early? Skip to the next iteration?
What will be the last line of output? Type in chat.
n = 6 while n > 0: print(n) n = n - 2
while n >= 0:
while n != 0:
n = 5 while n != 0: print(n) n = n - 2
What does this function return for uses_any('hello', 'xyz')? Type in chat.
uses_any('hello', 'xyz')
def uses_any(word, letters): for letter in word: if letter in letters: return True return False
uses_any('hello', 'aeiou')
return False
What does each function do differently? Type in chat.
def version_a(word): def version_b(word): for letter in word: for letter in word: if letter in 'aeiou': if letter in 'aeiou': print(letter) return letter print('Done') return 'None found'
What happens when you call version_a('hello')? What about version_b('hello')?
version_a('hello')
version_b('hello')
for or while? Which loop type is better for each task?
a. Print each character in a string b. Keep rolling a die until you get a 6 c. Count the vowels in a word d. Ask the user for input until they type "done"
False
What we'll learn:
Access individual characters with bracket notation (zero-indexed):
fruit = 'banana' fruit[0] # 'b' fruit[1] # 'a' fruit[-1] # 'a' (last character)
len(fruit) # 6 fruit[6] # IndexError! (valid indices: 0 to 5)
We already used for letter in word: - now we understand why it works!
for letter in word:
Extract a portion of a string with [start:end]:
[start:end]
fruit = 'banana' fruit[0:3] # 'ban' (includes start, excludes end) fruit[3:6] # 'ana' fruit[:3] # 'ban' (omit start = from beginning) fruit[3:] # 'ana' (omit end = to end) fruit[:] # 'banana' (copy the whole string)
You cannot change a string in place:
greeting = 'Hello, world!' greeting[0] = 'J' # TypeError!
Instead, create a new string:
new_greeting = 'J' + greeting[1:] new_greeting # 'Jello, world!'
This is a key difference from lists (coming in Chapter 9).
Methods are functions called on an object using dot notation:
word = 'banana' word.upper() # 'BANANA' word.lower() # 'banana' word.startswith('ban') # True word.count('a') # 3 word.replace('a', 'o') # 'bonono'
Important: these return new strings. The original is unchanged!
word # still 'banana'
Python has a built-in re module for pattern matching in strings:
re
import re text = "Call me at 617-555-1234" result = re.search(r'\d{3}-\d{3}-\d{4}', text) result.group() # '617-555-1234'
The NYT Spelling Bee gives you 7 letters (one is the center):
T L N [O] C F I
Rules:
O
Let's think before we code. What checks does each word need to pass?
words.txt
What functions should we write?
uses_only(word, letters) # Ch7 search pattern! is_bee_valid(word, letters, center)
What patterns from last session do you recognize here?
Use what you've learned about strings and search patterns to build this!
uses_only(word, letters)
is_bee_valid(word, letters, center)
Feel free to use AI to help, but understand every line before moving on.
Scoring:
Challenges:
score_word(word, letters)
is_pangram(word, letters)
Use the remaining time to work on any of these (stay on the call if you have questions):
I'm here to help - ask in chat or unmute!
logs/wk06.md
Next session: Quiz 1 + Lists (Chapter 9)
global styles