[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
You don’t need to use any particular framework or library from this class. But do it if it saves time.
A compact replacement for some accumulator patterns.
Simple list comprehensions
for loop.Write a dict comprehension that gives the indices of each word in a list of words. For example, if words = ['hello', 'world'], the output should be {'hello': 0, 'world': 1}.
Do these:
Suppose we have a sorted list:
We want to find the letter grade for, say, 89. Can we do this faster than searching the whole list?
Exercise: Write the code to search the whole list.
Trick: check the item in the middle element to see whether to look in the left or right half.
1.66 μs ± 7.1 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
'Random|variable|generators.|bytes|-----|uniform|bytes|(values|between|0|and|255)|integers|--------|uniform|within|range|sequences|---------|pick'
'2**19937-1.|extensively|implemented|threadsafe.|distributions|distributions|distributions|----------------------|------------------------------|---------------------------------------------'
Some material useful for simulations
CSV files
JSON (JavaScript Object Notation) files
Pickle
Suppose you want to add thunderstorms in your population simulation. For simpliciticy, they happen each day with probability p. Remember that random.random() generates a random number between 0 and 1. What fills in the blank?
import runpy; runpy.run_module("turtledemo")Do them now. Come get the prof out in the hall when everyone’s done.
Code you don’t have to write, or even install!
import re
ssn_regex = re.compile(r"""
^ # match the beginning of the string
(\d{3}) # match exactly 3 digits
- # match one dash
(\d{2}) # match exactly 2 digits
-
(\d{4}) # match exactly 4 digits
$ # match end of string
""", re.VERBOSE)
def is_valid_ssn(ssn):
match = ssn_regex.match(ssn)
if match:
print(match.groups())
return True
return False
is_valid_ssn("123-45-6789")('123', '45', '6789')
True
enumerate0 A
1 B
2 C
3 D
4 E
5 F
6 G
Example: Spelling Alphabet
['Charlie', 'Sierra']
Is there a better way?
['Charlie', 'Sierra']
Live demo. CodeMirror
Here are a few options, playing with different types of absurdity:
1. **The disgruntled cloud contemplates beneath the philosophizing pebble.**
2. **The fluffy purple toaster hiccups through the apathetic washing machine.**
3. **The iridescent narwhal procrastinates into the existential sloth.**
4. **The silent magenta sock breathes upon the enthusiastic potato.**
5. **The whistling polka-dotted carrot sings to the melancholy stapler.**
To set this up:
llm package (Manage Packages in Thonny, or pip install llm).llm-gemini in the same way.LLM_GEMINI_KEY=
and paste the key in the right-hand side (without quotes). Then restart Thonny.
(alternatively, run llm keys set gemini on a system Terminal.)
Reference: llm API
This way the model can remember the context of the conversation.
1. Causes Earth's ocean tides.
2. Humans walked upon its surface.
3. Always shows its same face.
4. Covered in countless craters.
5. It's slowly drifting away.
LLM called the uppercase function with: Always use functions. Repeat everything you've heard so far.
ALWAYS USE FUNCTIONS. REPEAT EVERYTHING YOU'VE HEARD SO FAR.
The : str and -> str are Python syntax to indicate the types of the input and output.