for loops
I'm used to List.filter so getting used to for loops again takes time.
Make sure you've exited the loop before raising an exception!
new_list= []
defloop(list):
foriteminlist:
ifitem==3:
new_list.append(item)
# The correct indentation is OUTSIDE the loop,# not within the `if` or `for` loop.raiseHTTPException(
status_code=404,
detail="Item with number 3 was not found"
)
if statements
In Elm you have must use the else keyword.
It's optional in Python.
defhas_three(list):
iflen(list) ==3:
return"Yes!"return"No!"
Using classes
You can use . dot notation to access fields ...
Dictionaries require dict["key"] (string accessors)
frompydanticimportBaseModelfromtypingimportOptionalclassItem(BaseModel):
id: intname: Optional[str]
item=Item(id=1)
item.id# returns 1
forloopsMake sure you've exited the loop before raising an exception!
ifstatementsUsing classes