Update page 'Home'

j
2023-03-29 17:29:52 +02:00
parent 57637b9338
commit ed59cf3a43
+58 -1
@@ -42,7 +42,7 @@ Question: Python doesn't have the concept of multi-line comments like other C-li
*/ */
``` ```
Sometimes, you want to "turn off" a large block of code this way for testing. How would you do that in Python? Sometimes, you want to "turn off" a large block of code this way for testing. How would you do that in Python? [2]
### 2.1.4 ### 2.1.4
@@ -91,3 +91,60 @@ Question: do you know why Python uses "j" for complex numbers instead of "i" lik
You don't have to know what these operators all do... actually I don't even recognize some of them, but you should know what a `unary operator` and `binary operator` are, and the difference between them. You can ignore the section about delimiters, that's probably included for people working on the Python interpreter itself. You don't have to know what these operators all do... actually I don't even recognize some of them, but you should know what a `unary operator` and `binary operator` are, and the difference between them. You can ignore the section about delimiters, that's probably included for people working on the Python interpreter itself.
[1] You will probably never have to write BNF grammars, but you should be able to read them because it is by far the fastest way to learn a new language. [1] You will probably never have to write BNF grammars, but you should be able to read them because it is by far the fastest way to learn a new language.
[2] One way that I don't recommend is using triple quotes, because this still returns a string that can be cast unintentionally to a value. I asked GPT-4 for an example and it came up with what I thought was a pretty good one:
Here's an example demonstrating an unintended side effect of using triple quotes for commenting that can change the program logic:
Consider the following code, where we have a function that counts the number of even numbers in a given list and has a print statement to display intermediate results:
```python
def count_evens(numbers):
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
print(f"{num} is even.")
return count
my_list = [1, 2, 3, 4, 5, 6]
print(f"Number of even numbers: {count_evens(my_list)}")
```
Now, let's say we want to comment out the print statement using triple quotes:
```python
def count_evens(numbers):
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
"""
print(f"{num} is even.")
"""
return count
my_list = [1, 2, 3, 4, 5, 6]
print(f"Number of even numbers: {count_evens(my_list)}")
```
This code seems fine at first glance, but it introduces an unintended side effect. The triple quotes are being used inside an `if` block, which means that the multi-line string created between the triple quotes is now part of the block's content.
The multi-line string is treated as an expression with a value, and since it's not `None`, it's considered a "truthy" value in a boolean context. This means that the presence of the multi-line string is affecting the control flow, and the `if` block is no longer only dependent on the `num % 2 == 0` condition. The logic of the program is unintentionally changed.
To avoid this issue, you should use the hash symbol (`#`) to comment out code:
```python
def count_evens(numbers):
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
# print(f"{num} is even.")
return count
my_list = [1, 2, 3, 4, 5, 6]
print(f"Number of even numbers: {count_evens(my_list)}")
```
By using the hash symbol to comment out the print statement, the program logic remains unchanged, and the control flow is not affected.