day 6: comments and escape sequence and flushes in python 🏄♀️
aryan015
Posted on July 27, 2024
how to write comments in python
You can write comments in to way. The first one is #
and other one is using three quotes.
one way
# this is comment
second way
'''
this is
a
multiline
comment
'''
escape sequence gives you super power how you write a code
The purpose of the ES are adding line-break, using special characters etc.
line-break
# you can add a line break in your print
print("hello aryan,\n thanks for applying for the course👋")
# below code snippet is wrong
print("hello aryan,
thanks for applying")
output
hello aryan,
thanks for applying for the course👋
you might want to use double quotes inside double quotes ?
you can use \(char)
to show on the console output
print("this is new line : \\n") # it will print \n
print("this is \"") # this will print "
output
this is new line: \n
this is "
more about python print
Python print is a very powerful method. It takes four parameter.
- Object - multiple strings
- sep - seperator for objects
- end - statement should end with (more in below ex)
- file - specify output to particular file
- flush
print(*object,sep,end,file,flush=true/false)
sep
# we already have talked about objects in previous blogs 🧡
print("hello","world",sep='-') # hello-world
end
It append whatever pass to it.
print("hello aryan",end="\n")
print("welcome to your dashboard🧡")
file (bit advance will talk about later)
👋
flush
This is a boolean that specifies whether the output should be flushed (i.e., forced to be written out) immediately. default value is false
.
print("Hello, World!", flush=True)
description
The flush parameter in the print() function is used to control the output buffering behavior. By default, output to the console or a file is buffered, meaning it is stored in a temporary location before being written out. This can improve performance but sometimes you need the output to be written immediately. This is where the flush parameter comes in handy.
usage
print("hello, world", flush=True) # wont wait for buffer
# in this example, each number is printed immediately, without waiting for the loop to finish.
import time
for i in range(5):
print(i,end=" ",flush=True)
time.sleep(1)
# intractive prompts
print("do you want to continue(y/n)?",end=" ",flush=True)
response = input()
# Here, the prompt is displayed immediately, allowing the user to see it before they enter their response.
Why use flush
✍ ?
- Immediate feedback - Ensures that the user sees the output right away, which is crucial for interactive applications.
- Debugging - Helps in debugging by making sure that all print statements are executed in real-time.
- Logging - Useful in logging scenarios where you want to capture events as they happen.
Buffer
Buffers are nothing but the temporary storage🏬 used to store data while they are being transferred🚌.
note
- flush is very important in respect to interviews. I want to cover it prior to any topic 🧡.
Posted on July 27, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.