Update page 'Day 2 homework - Sloetjes'
@@ -1,7 +1,7 @@
|
|||||||
# Xuanyi Wang\_Day-2\_Homework\_03/31/23
|
# Xuanyi Wang\_Day-2\_Homework\_03/31/23
|
||||||
|
|
||||||
**
|
**
|
||||||
##### 1. Annotate the following python expressions with the specific types of each literal, as well as the overall return value:**
|
1. Annotate the following python expressions with the specific types of each literal, as well as the overall return value:**
|
||||||
> i. i = 3
|
> i. i = 3
|
||||||
>
|
>
|
||||||
> ii. f = 3.0
|
> ii. f = 3.0
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
```
|
```
|
||||||
The overall return value of each expression is the value assigned to the corresponding variable, which is the literal itself.
|
The overall return value of each expression is the value assigned to the corresponding variable, which is the literal itself.
|
||||||
|
|
||||||
**##### 2. For each of the types string, bytestring, integer, floating point, and imaginary literals, construct a 5x5 table showing what happens when the character "0" or number 0 is compared to each other type.**
|
**2. For each of the types string, bytestring, integer, floating point, and imaginary literals, construct a 5x5 table showing what happens when the character "0" or number 0 is compared to each other type.**
|
||||||
|
|
||||||
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
||||||
|---|---------|---------|---------|---------|---------|
|
|---|---------|---------|---------|---------|---------|
|
||||||
@@ -34,7 +34,7 @@ The overall return value of each expression is the value assigned to the corresp
|
|||||||
| **0.0**| Not equal | Not equal | Equal | Equal | Not Equal |
|
| **0.0**| Not equal | Not equal | Equal | Equal | Not Equal |
|
||||||
| **0j** | Not equal | Not equal | Equal | Not equal| Equal |
|
| **0j** | Not equal | Not equal | Equal | Not equal| Equal |
|
||||||
|
|
||||||
**##### 3. Now do the same thing for the + and == operators for each pair of types in two other 5x5 tables.**
|
**3. Now do the same thing for the + and == operators for each pair of types in two other 5x5 tables.**
|
||||||
|
|
||||||
###### The "+" operator case:
|
###### The "+" operator case:
|
||||||
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
||||||
@@ -55,7 +55,7 @@ The overall return value of each expression is the value assigned to the corresp
|
|||||||
| **0.0**| False | False | True | True| False |
|
| **0.0**| False | False | True | True| False |
|
||||||
| **0j** | False | False | True | False| True |
|
| **0j** | False | False | True | False| True |
|
||||||
|
|
||||||
**##### 4. Write a small program that uses the AST module to open the lexer/parser example, and dumps the resulting AST. See if there are any arguments to the parse method which make the output easier to read.**
|
**4. Write a small program that uses the AST module to open the lexer/parser example, and dumps the resulting AST. See if there are any arguments to the parse method which make the output easier to read.**
|
||||||
|
|
||||||
```
|
```
|
||||||
import ast
|
import ast
|
||||||
@@ -71,13 +71,13 @@ tree = ast.parse(source)
|
|||||||
print(ast.dump(tree, indent=4))
|
print(ast.dump(tree, indent=4))
|
||||||
```
|
```
|
||||||
|
|
||||||
**##### 5. What does this do? `print("\007")` Can you figure out why?**
|
**5. What does this do? `print("\007")` Can you figure out why?**
|
||||||
|
|
||||||
The statement `print("\007")` prints **a bell character to the console**. The `“\007”` escape sequence represents **the ASCII code for the bell character**, which is also known as the alert or bell control character.
|
The statement `print("\007")` prints **a bell character to the console**. The `“\007”` escape sequence represents **the ASCII code for the bell character**, which is also known as the alert or bell control character.
|
||||||
|
|
||||||
When this character is printed to a console or terminal, it can produce **an audible bell sound or visual flash**, depending on the configuration of the console or terminal. The behavior can **vary depending on the operating system and the type of console or terminal being used**.
|
When this character is printed to a console or terminal, it can produce **an audible bell sound or visual flash**, depending on the configuration of the console or terminal. The behavior can **vary depending on the operating system and the type of console or terminal being used**.
|
||||||
|
|
||||||
**##### 6. What does this do? `print("abcdefg\rhijklmnop")` Can you figure out why?**
|
**6. What does this do? `print("abcdefg\rhijklmnop")` Can you figure out why?**
|
||||||
|
|
||||||
The code `print("abcdefg\rhijklmnop")` **will output `"hijklmnopg"` on the console**, replacing the first character of `"abcdefg"` with the character `'h'`. The reason for this is the `'\r'` character in the middle of the string.
|
The code `print("abcdefg\rhijklmnop")` **will output `"hijklmnopg"` on the console**, replacing the first character of `"abcdefg"` with the character `'h'`. The reason for this is the `'\r'` character in the middle of the string.
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ In Python, **the `'\r'` character is a carriage return character**, which instru
|
|||||||
|
|
||||||
So in this case, the string "abcdefg" is printed, then **the `'\r'` character moves the cursor back to the beginning of the line**, and the string "hijklmnop" is printed, **overwriting the first character of `"abcdefg"` with the character `'h'`**. This results in the output `"hijklmnopdefg"` being printed to the console or terminal.
|
So in this case, the string "abcdefg" is printed, then **the `'\r'` character moves the cursor back to the beginning of the line**, and the string "hijklmnop" is printed, **overwriting the first character of `"abcdefg"` with the character `'h'`**. This results in the output `"hijklmnopdefg"` being printed to the console or terminal.
|
||||||
|
|
||||||
**##### 7. This is an HTTP server. Run it and connect to http://localhost:8000 . Why doesn't this do the same thing as what you see above? (don't forget to shut down the HTTP server afterward.)**
|
**7. This is an HTTP server. Run it and connect to http://localhost:8000 . Why doesn't this do the same thing as what you see above? (don't forget to shut down the HTTP server afterward.)**
|
||||||
|
|
||||||
```
|
```
|
||||||
import http.server
|
import http.server
|
||||||
@@ -136,7 +136,7 @@ httpd.serve_forever()
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
**##### 8. Think of a way someone might try to commit a crime or hide an embezzlement scheme using the answers to the above two questions. Hint: applications usually output on web pages, but developers and system administrators usually troubleshoot in the console.**
|
**8. Think of a way someone might try to commit a crime or hide an embezzlement scheme using the answers to the above two questions. Hint: applications usually output on web pages, but developers and system administrators usually troubleshoot in the console.**
|
||||||
|
|
||||||
##### Answer:
|
##### Answer:
|
||||||
One way someone might try to hide an embezzlement scheme using the answers to the above question is by using the `"\r"` character to overwrite the console output of a financial transaction.
|
One way someone might try to hide an embezzlement scheme using the answers to the above question is by using the `"\r"` character to overwrite the console output of a financial transaction.
|
||||||
|
|||||||
Reference in New Issue
Block a user