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 ๐งก.
Top comments (0)