From ed59cf3a435f2264754a41e2e82c8ac51a74d2fa Mon Sep 17 00:00:00 2001 From: j Date: Wed, 29 Mar 2023 17:29:52 +0200 Subject: [PATCH] Update page 'Home' --- Home.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/Home.md b/Home.md index 428532b..e5fe37d 100644 --- a/Home.md +++ b/Home.md @@ -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 @@ -90,4 +90,61 @@ 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. -[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. \ No newline at end of file +[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. \ No newline at end of file