mirror of
https://github.com/onkelbeh/cheatsheets.git
synced 2026-08-15 13:34:53 +02:00
Add missing syntax highlighting to python.md (#2154)
Fixes missing code highlighting in half of the Python code snippets. The top half of the page had code examples formatted as code blocks with 4 space character indentations and no syntax highlighting as a result. The bottom half on the other hand, used code blocks surrounded by "```py" formatting brackets which hints to many markdown renders that syntax highlighting should be applied. This unifies the code blocks in the top half of the page to use the same formatting syntax used in the bottom half with syntax highlighting. This change was prompted by requests to add the syntax highlighting in comments on the https://devhints.io/python page.
This commit is contained in:
19
python.md
19
python.md
@@ -5,10 +5,13 @@ category: Python
|
||||
|
||||
### Tuples (immutable)
|
||||
|
||||
```py
|
||||
tuple = ()
|
||||
```
|
||||
|
||||
### Lists (mutable)
|
||||
|
||||
```py
|
||||
list = []
|
||||
list[i:j] # returns list subset
|
||||
list[-1] # returns last element
|
||||
@@ -35,10 +38,13 @@ category: Python
|
||||
zip(list1, list2) # returns list of tuples with n-th element of both list1 and list2
|
||||
list.sort() # sorts in-place, returns None
|
||||
sorted(list) # returns sorted copy of list
|
||||
",".join(list) # returns a string with list elements seperated by comma
|
||||
",".join(list) # returns a string with list elements separated by comma
|
||||
|
||||
```
|
||||
|
||||
### Dict
|
||||
|
||||
```py
|
||||
dict = {}
|
||||
dict.keys()
|
||||
dict.values()
|
||||
@@ -47,18 +53,22 @@ category: Python
|
||||
dict.get("key") # ...this returns None
|
||||
dict.setdefault("key", 1)
|
||||
**dict # expands all k/v pairs in place
|
||||
```
|
||||
|
||||
### Iteration
|
||||
|
||||
```py
|
||||
for item in ["a", "b", "c"]:
|
||||
for i in range(4): # 0 to 3
|
||||
for i in range(4, 8): # 4 to 7
|
||||
for i in range(1, 9, 2): # 1, 3, 5, 7
|
||||
for key, val in dict.items():
|
||||
for index, item in enumerate(list):
|
||||
```
|
||||
|
||||
### [String](https://docs.python.org/2/library/stdtypes.html#string-methods)
|
||||
|
||||
```py
|
||||
str[0:4]
|
||||
len(str)
|
||||
|
||||
@@ -92,25 +102,31 @@ category: Python
|
||||
'"Yes," they said.'
|
||||
>>> '"Isn\'t," they said.'
|
||||
'"Isn\'t," they said.'
|
||||
```
|
||||
|
||||
### Casting
|
||||
|
||||
```py
|
||||
int(str)
|
||||
float(str)
|
||||
str(int)
|
||||
str(float)
|
||||
'string'.encode()
|
||||
```
|
||||
|
||||
### Comprehensions
|
||||
|
||||
```py
|
||||
[fn(i) for i in list] # .map
|
||||
map(fn, list) # .map, returns iterator
|
||||
|
||||
filter(fn, list) # .filter, returns iterator
|
||||
[fn(i) for i in list if i > 0] # .filter.map
|
||||
```
|
||||
|
||||
### Regex
|
||||
|
||||
```py
|
||||
import re
|
||||
|
||||
re.match(r'^[aeiou]', str)
|
||||
@@ -120,6 +136,7 @@ category: Python
|
||||
expr = re.compile(r'^...$')
|
||||
expr.match(...)
|
||||
expr.sub(...)
|
||||
```
|
||||
|
||||
## File manipulation
|
||||
|
||||
|
||||
Reference in New Issue
Block a user