Fix python code tag

This commit is contained in:
Isabelle Viktoria Maciohsek
2021-01-09 23:28:19 +02:00
parent 6ca625f686
commit d4abc910af
6 changed files with 10 additions and 10 deletions

View File

@ -11,7 +11,7 @@ A common debate among Python developers seems to stem from the retrieval of dict
Although you can achieve the same result using either one, `dict.get()` is usually preferred, as it accepts a second argument which acts as the default value shall the key not exist in the given dictionary. Due to this property, `dict.get()` will always return a value, whereas `dict[key]` will raise a `KeyError` if the given key is missing. Although you can achieve the same result using either one, `dict.get()` is usually preferred, as it accepts a second argument which acts as the default value shall the key not exist in the given dictionary. Due to this property, `dict.get()` will always return a value, whereas `dict[key]` will raise a `KeyError` if the given key is missing.
```python ```py
a = { 'max': 200 } a = { 'max': 200 }
b = { 'min': 100, 'max': 250 } b = { 'min': 100, 'max': 250 }
c = { 'min': 50 } c = { 'min': 50 }

View File

@ -11,7 +11,7 @@ excerpt: Learn two ways to format a string in Python with this quick tip.
[Formatted string literals](https://docs.python.org/3/reference/lexical_analysis.html?highlight=lexical%20analysis#formatted-string-literals), commonly known as f-strings, are strings prefixed with `'f`' or `'F'`. These strings can contain replacement fields, enclosed in curly braces (`{}`). [Formatted string literals](https://docs.python.org/3/reference/lexical_analysis.html?highlight=lexical%20analysis#formatted-string-literals), commonly known as f-strings, are strings prefixed with `'f`' or `'F'`. These strings can contain replacement fields, enclosed in curly braces (`{}`).
```python ```py
name = 'John' name = 'John'
age = 32 age = 32
@ -22,7 +22,7 @@ print(f'{name} is {age} years old') # 'John is 32 years old'
The [`str.format()`](https://docs.python.org/3/library/stdtypes.html?highlight=str%20format#str.format) method works very much alike f-strings, the main difference being that replacement fields are supplied as arguments instead of as part of the string. The [`str.format()`](https://docs.python.org/3/library/stdtypes.html?highlight=str%20format#str.format) method works very much alike f-strings, the main difference being that replacement fields are supplied as arguments instead of as part of the string.
```python ```py
name = 'John' name = 'John'
age = 32 age = 32

View File

@ -18,7 +18,7 @@ The main difference between the two is that the `is` keyword checks for referenc
Here are some examples to clear up any confusion: Here are some examples to clear up any confusion:
```python ```py
a = [1, 2, 3] a = [1, 2, 3]
b = a b = a
c = [x for x in a] c = [x for x in a]

View File

@ -11,7 +11,7 @@ Python's named tuples are a very simple yet interesting feature that can make a
For example, a point in the two-dimensional plane can be represented using two coordinates. In a regular tuple, these values would be accessed by index (`[0]` and `[1]`), but if we define a named tuple, `Point`, we can access them using `x` and `y` instead (although we can still use indexes, too, if we want): For example, a point in the two-dimensional plane can be represented using two coordinates. In a regular tuple, these values would be accessed by index (`[0]` and `[1]`), but if we define a named tuple, `Point`, we can access them using `x` and `y` instead (although we can still use indexes, too, if we want):
```python ```py
from collections import namedtuple from collections import namedtuple
# Regular tuple # Regular tuple
@ -24,7 +24,7 @@ q = Point(3, 5) # q.x = 3, q.y = 5
Apart from the increased readability of your code, named tuples provide a few other quality of life improvements. First and foremost, they allow for default values to be specified via the `defaults` iterable argument. Secondly, they have the ability to automatically rename duplicate or invalid fields via the `rename` boolean argument. And, finally, they even provide a convenient option to specify field names as a list or comma/space-separated string. Apart from the increased readability of your code, named tuples provide a few other quality of life improvements. First and foremost, they allow for default values to be specified via the `defaults` iterable argument. Secondly, they have the ability to automatically rename duplicate or invalid fields via the `rename` boolean argument. And, finally, they even provide a convenient option to specify field names as a list or comma/space-separated string.
```python ```py
from collections import namedtuple from collections import namedtuple
Point = namedtuple('Point', ['x', 'y', 'z'], defaults = [1]); Point = namedtuple('Point', ['x', 'y', 'z'], defaults = [1]);

View File

@ -13,7 +13,7 @@ Python provides two ways to sort a list, the built-in list method `list.sort()`
The primary difference between the two is that `list.sort()` will sort the list in-place, mutating its indexes and returning `None`, whereas `sorted()` will return a new sorted list leaving the original list unchanged. Another difference is that `sorted()` accepts any iterable while `list.sort()` is a method of the `list` class and can only be used with lists. The primary difference between the two is that `list.sort()` will sort the list in-place, mutating its indexes and returning `None`, whereas `sorted()` will return a new sorted list leaving the original list unchanged. Another difference is that `sorted()` accepts any iterable while `list.sort()` is a method of the `list` class and can only be used with lists.
```python ```py
nums = [2, 3, 1, 5, 6, 4, 0] nums = [2, 3, 1, 5, 6, 4, 0]
print(sorted(nums)) # [0, 1, 2, 3, 4, 5, 6] print(sorted(nums)) # [0, 1, 2, 3, 4, 5, 6]

View File

@ -11,7 +11,7 @@ excerpt: Learn 3 easy ways to swap the values of two variables in Python.
The simplest way to swap the values of two variables is using a `temp` variable. The `temp` variables is used to store the value of the fist variable (`temp = a`), allowing you to swap the value of the two variables (`a = b`) and then assign the value of `temp` to the second variable. The simplest way to swap the values of two variables is using a `temp` variable. The `temp` variables is used to store the value of the fist variable (`temp = a`), allowing you to swap the value of the two variables (`a = b`) and then assign the value of `temp` to the second variable.
```python ```py
a = 11 a = 11
b = 7 b = 7
@ -29,7 +29,7 @@ Another way to swap the values of two variables, without using a temporary varia
This method of variable swapping and permutation can be used for more than two variables as long as the same number of variables are on both sides of the statement. This method of variable swapping and permutation can be used for more than two variables as long as the same number of variables are on both sides of the statement.
```python ```py
a = 11 a = 11
b = 7 b = 7
@ -43,7 +43,7 @@ print(b) # 11
If the two variables are numbers, their values can be swapped using arithmetic operators such as addition and subtraction (`+`, `-`) or multiplication and division (`*`, `/`). This swapping method is based on calculating the sum of the two numbers and then swapping them using the sum and the difference from the sum. If the two variables are numbers, their values can be swapped using arithmetic operators such as addition and subtraction (`+`, `-`) or multiplication and division (`*`, `/`). This swapping method is based on calculating the sum of the two numbers and then swapping them using the sum and the difference from the sum.
```python ```py
a = 11 a = 11
b = 7 b = 7