DEV Community

Cover image for day 6: comments and escape sequence and flushes in python 🏄‍♀️
aryan015
aryan015

Posted on • Edited on

day 6: comments and escape sequence and flushes in python 🏄‍♀️

complete index

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
Enter fullscreen mode Exit fullscreen mode

second way

'''
this is 
a
multiline
comment
'''
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

output

hello aryan,
 thanks for applying for the course👋
Enter fullscreen mode Exit fullscreen mode

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 "
Enter fullscreen mode Exit fullscreen mode

output

this is new line: \n
this is "
Enter fullscreen mode Exit fullscreen mode

more about python print

Python print is a very powerful method. It takes four parameter.

  1. Object - multiple strings
  2. sep - seperator for objects
  3. end - statement should end with (more in below ex)
  4. file - specify output to particular file
  5. flush
print(*object,sep,end,file,flush=true/false)
Enter fullscreen mode Exit fullscreen mode

sep

# we already have talked about objects in previous blogs 🧡
print("hello","world",sep='-') # hello-world
Enter fullscreen mode Exit fullscreen mode

end

It append whatever pass to it.

print("hello aryan",end="\n")
print("welcome to your dashboard🧡")
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
# 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)
Enter fullscreen mode Exit fullscreen mode
# 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.
Enter fullscreen mode Exit fullscreen mode

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)