diff --git a/blog_posts/python-dict-getkey-vs-dictkey.md b/blog_posts/python-dict-getkey-vs-dictkey.md index d931dc13d..9a00167ab 100644 --- a/blog_posts/python-dict-getkey-vs-dictkey.md +++ b/blog_posts/python-dict-getkey-vs-dictkey.md @@ -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. -```python +```py a = { 'max': 200 } b = { 'min': 100, 'max': 250 } c = { 'min': 50 } diff --git a/blog_posts/python-fstrings-str-format.md b/blog_posts/python-fstrings-str-format.md index 1c8bd086b..b48c71988 100644 --- a/blog_posts/python-fstrings-str-format.md +++ b/blog_posts/python-fstrings-str-format.md @@ -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 (`{}`). -```python +```py name = 'John' 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. -```python +```py name = 'John' age = 32 diff --git a/blog_posts/python-identity-equality.md b/blog_posts/python-identity-equality.md index baa167eb2..45e036ca2 100644 --- a/blog_posts/python-identity-equality.md +++ b/blog_posts/python-identity-equality.md @@ -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: -```python +```py a = [1, 2, 3] b = a c = [x for x in a] diff --git a/blog_posts/python-named-tuples.md b/blog_posts/python-named-tuples.md index 813221a86..f98615656 100644 --- a/blog_posts/python-named-tuples.md +++ b/blog_posts/python-named-tuples.md @@ -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): -```python +```py from collections import namedtuple # 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. -```python +```py from collections import namedtuple Point = namedtuple('Point', ['x', 'y', 'z'], defaults = [1]); diff --git a/blog_posts/python-sortedlist-vs-list-sort.md b/blog_posts/python-sortedlist-vs-list-sort.md index f0b213473..504345611 100644 --- a/blog_posts/python-sortedlist-vs-list-sort.md +++ b/blog_posts/python-sortedlist-vs-list-sort.md @@ -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. -```python +```py nums = [2, 3, 1, 5, 6, 4, 0] print(sorted(nums)) # [0, 1, 2, 3, 4, 5, 6] diff --git a/blog_posts/python-swap-variables.md b/blog_posts/python-swap-variables.md index d608969f1..b8c5cbee3 100644 --- a/blog_posts/python-swap-variables.md +++ b/blog_posts/python-swap-variables.md @@ -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. -```python +```py a = 11 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. -```python +```py a = 11 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. -```python +```py a = 11 b = 7