§2024-12-19
機器: jetsonOrinNana dev kit, ubuntu 22.04, uisnf conda ontrol
https://www.programiz.com/python-programming/getting-started
- install python 13.1
$ conda activate R-4.4
$ conda install conda-forge::python (or conda install python=3.8 a specific version)
$ which python
/home/alexlai/anaconda3/envs/R-4.4/bin/python
(R-4.4) alexlai@Jetson:~$ python --version
Python 3.13.1
(R-4.4) alexlai@Jetson:~$ which pip
/home/alexlai/anaconda3/envs/R-4.4/bin/pip
(R-4.4) alexlai@Jetson:~$ pip --version
pip 24.2 from /home/alexlai/anaconda3/envs/R-4.4/lib/python3.13/site-packages/pip (python 3.13)
- comment
# This is an example of a multiline comment
# created using multiple single-line commenced
# The code prints the text Hello World
print("Hello, World!")
'''This is an example
of multiline comment'''
print("Hello, World!")
number1 = 10
number2 = 15
sum = number1 + number2
print("The sum is:", sum)
# print('The product is:', product)
-
syntax: print(object= separator= end= file= flush=)
Here,
- object - value(s) to be printed
- sep (optional) - allows us to separate multiple objects inside print().
- end (optional) - allows us to add add specific values like new line "\n", tab "\t"
- file (optional) - where the values are printed. It's default value is sys.stdout (screen)
- flush (optional) - boolean specifying if the output is flushed or buffered. Default: False
object
print('Good Morning!')
print('It is rainy today')
- output
Good Morning!
It is rainy today
the print() statement only includes the object to be printed. Here, the value for end is not used. Hence, it takes the default value '\n'.
- end=, end= '\t\t'),end= '\n\t') \t- 4 spaces
# print with end whitespac
print('Good Morning!', end= ' ')
print('It is rainy today')
- output
Good Morning! It is rainy today
- sep= '. ')
print('New Year', 2023, 'See you soon!', sep= '. ')
New Year. 2023. See you soon!
import sys
import time
# Write to a file
with open("output.txt", "w") as f:
for i in range(5):
print(f"Writing line {i}", file=f)
time.sleep(1)
# Writing to standard output (console) and flush the output immediately
for i in range(5):
print(f"Flushing line {i}", file=sys.stdout, flush=True)
time.sleep(1)
Explanation: file parameter: In the first loop, print(f"Writing line {i}", file=f) directs the output to a file called output.txt rather than the standard console. The f is a file object that we open with the open() function. flush parameter: In the second loop, flush=True ensures that the output is immediately written to the console (or the file, depending on the file argument). By default, Python's print() is buffered, meaning it may not output immediately to the console or file, especially in cases where large amounts of data are written. The flush=True forces the buffer to be flushed after each print statement. Output: The file output.txt will contain the following:
arduino Copy code Writing line 0 Writing line 1 Writing line 2 Writing line 3 Writing line 4 The console will show the following:
arduino Copy code Flushing line 0 Flushing line 1 Flushing line 2 Flushing line 3 Flushing line 4 You can adjust file and flush to fit the specific behavior you're aiming for in your program.
You said: what is the difference print(f"Flushing line {i}", file=sys.stdout, flush=True) and print(f"Flushing line {i}", file=sys.stdout, flush=False) ChatGPT said: ChatGPT The difference between print(f"Flushing line {i}", file=sys.stdout, flush=True) and print(f"Flushing line {i}", file=sys.stdout, flush=False) lies in how the output is handled with respect to buffering.
Key Concept: Buffering By default, Python's print() function uses output buffering to optimize performance. When output is buffered, the data is not immediately written to the console or file. Instead, it may be held in a buffer and written all at once when the buffer is full or when the program exits.
flush=True When you set flush=True, you are telling Python to force the buffer to be flushed immediately after the print() call. This means that the output is written to the stream (in this case, sys.stdout, i.e., the console) immediately.
Example with flush=True: python Copy code import time import sys
for i in range(5): print(f"Flushing line {i}", file=sys.stdout, flush=True) time.sleep(1) Behavior:
Each print() call will immediately display the output in the console. Even if the system is buffering the output, the flush=True forces Python to write it to sys.stdout without delay. flush=False (default behavior) When you set flush=False (which is the default), Python may buffer the output. This means the data might not be immediately written to the console or file. Instead, it will stay in the buffer until the buffer is full or the program finishes (or the program explicitly flushes the buffer).
Example with flush=False: python Copy code import time import sys
for i in range(5): print(f"Flushing line {i}", file=sys.stdout, flush=False) time.sleep(1) Behavior:
The output might not appear immediately in the console. It will be held in the buffer and only flushed when the buffer is full or when the program ends. This can result in delayed output, especially if the program is long-running or involves waiting (like time.sleep() in this case). In Summary: flush=True: Immediately writes the output to the stream (sys.stdout in this case). The data is flushed from the internal buffer after each print(). flush=False (default): Output is buffered, meaning the data may not appear immediately in the console. The system will wait to flush the buffer based on its own internal buffering rules (e.g., when the buffer fills up or the program exits). When to Use Each: flush=True: Useful when you want to ensure that each print() statement is immediately visible, for example, when you are printing progress updates in real-time or debugging. flush=False: This is usually fine for normal output. It helps with performance because buffering reduces the number of write operations to the console.
ChatGPT can make mistakes. Check important info. ?