The snippet library.
Small, practical examples. Search by task or narrow things down below.
Calculate a simple total
Python: Multiply a unit price by a quantity, then add a delivery fee.
Python✓ Tested
Call a library function
Python: Use a standard absolute-value function with a small negative number.
Python✓ Tested
Choose between two branches
Python: Use if and else to handle an available or empty stock count.
Python✓ Tested
Combine text with a name
Python: Construct a greeting from a fixed prefix and a stored name.
Python✓ Tested
Compare a value with a limit
Python: Turn a comparison into a readable yes or no result.
Python✓ Tested
Convert known numeric text to an integer
Python: Convert a trusted literal to an integer before adding. Validate external input before converting it.
Python✓ Tested
Count the items in a collection
Python: Read the collection’s length instead of hard-coding the loop limit.
Python✓ Tested
Divide using a fractional number
Python: Use a floating-point operand to preserve the fractional part of a division.
Python✓ Tested
Explain a calculation with a comment
Python: Keep the code executable while documenting why a fixed input is used.
Python✓ Tested
Find what remains after division
Python: Use the remainder operator to count items left after forming complete groups.
Python✓ Tested
Handle three possible outcomes
Python: Check high, normal and low values with an ordered conditional chain.
Python✓ Tested
Measure a short text value
Python: Read the length of an ASCII string. Non-ASCII text may use multiple code units or bytes depending on the language.
Python✓ Tested