The snippet library.
Small, practical examples. Search by task or narrow things down below.
Name a value that stays fixed
Python: Use a constant declaration or the language’s constant naming convention for a fixed limit.
Python✓ Tested
Print a number with two decimal places
Python: Format a small decimal value consistently for display.
Python✓ Tested
Read one character from ASCII text
Python: Use a valid zero-based index to read the first character.
Python✓ Tested
Read the beginning of a text value
Python: Extract or display the first three ASCII characters from a known nonempty string.
Python✓ Tested
Read the first collection element
Python: Access index zero only after establishing that the collection is nonempty.
Python✓ Tested
Repeat a fixed number of times
Python: Use a counted loop to print three numbered steps.
Python✓ Tested
Repeat while a condition holds
Python: Count down while a positive number remains, updating the loop variable on each iteration.
Python✓ Tested
Replace one collection element
Python: Update an existing element at a known valid index.
Python✓ Tested
Require two conditions to be true
Python: Combine a lower and an upper bound to check whether a value is in range.
Python✓ Tested
Run your first program
Python: Start with a complete program that prints one line. Follow the run instructions below.
Python✓ Tested
Store a value in a named variable
Python: Give a value a descriptive name and print it.
Python✓ Tested
Update a running count
Python: Change a variable after its initial value has been assigned.
Python✓ Tested