python:python-code
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| python:python-code [10/07/2023 21:22] – created - external edit 127.0.0.1 | python:python-code [14/02/2025 10:48] (current) – [Print both keys and values] andrew | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Python code snippets ====== | ====== Python code snippets ====== | ||
| + | |||
| + | ===== FOR loop ===== | ||
| + | |||
| + | Looping through all the items in a dictionary. | ||
| + | |||
| + | ==== Print all the keys ==== | ||
| + | |||
| + | <code python> | ||
| + | exampledict = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | for x in thisdict: | ||
| + | print(x) | ||
| + | </ | ||
| + | < | ||
| + | class | ||
| + | engine | ||
| + | built | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Print all the values - version 1 ==== | ||
| + | |||
| + | <code python> | ||
| + | exampledict = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | for x in exampledict: | ||
| + | print(exampledict[x]) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | Modified Hall | ||
| + | Witherslack Hall | ||
| + | 1948 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Print all the values - version 2 ==== | ||
| + | |||
| + | This uses the Python " | ||
| + | <code python> | ||
| + | exampledict = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | for x in exampledict.values(): | ||
| + | print(x) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | Modified Hall | ||
| + | Witherslack Hall | ||
| + | 1948 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Print both keys and values ==== | ||
| + | |||
| + | Using the " | ||
| + | |||
| + | <code python> | ||
| + | exampledict = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | for x,y in exampledict.items(): | ||
| + | print(x, y) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | class Modified Hall | ||
| + | engine Witherslack Hall | ||
| + | built 1948 | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| ===== CLI input selection for an option ===== | ===== CLI input selection for an option ===== | ||
python/python-code.1689024160.txt.gz · Last modified: by 127.0.0.1
