Skip to main content

Comments, Escape Sequences & Print Statement

 Comments are used to write something which the programmer does not want to execute. Comments can be written to mark author name, date when the program is written, adding notes for your future self, etc.

  • Comments are used to make the code more understandable for the programmer.
  • The Interpreter does not execute comments.

There are two types of comments in Python Language -:

  • Single Line Comment
  • Multi-Line Comment

Single Line Comment: Single Line comments are the comments which are written in a single line, i.e., they occupy the space of a single line.

  • We use # (hash/pound to write single-line comments).
  • E.g., the below program depicts the usage of comments.
import os
#This is a comment
print("Main code started")

#Now I will write my code here:
print(os.listdir())

Multi-Line Comment: Multi-Line comments are the comments which are created by using multiple lines, i.e., they occupy more than one line in a program.

  • We use ' ' '….. Comment ….' ' ' for writing multi-line comments in Python (Use lines enclosed with three quotes for writing multi-line comments). An example of a multi-line comment is shown below:
import os
'''This is a comment
Author: Harry
Date: 27 November 2020
Multi-line comment ends here
'''
print("Main code started")

#Now I will write my code here:
print(os.listdir())

Python Print() Statement:

print() is a function in Python that allows us to display whatever is written inside it. In case an operation is supplied to print, the value of the expression after the evaluation is printed in the terminal. For example,

import os
import flask

# print statement for printing strings
print("Harry is a programmer")

# Print statement with a literal
print(1+87)

#This will print "Harry is a programmer" and 88 on the screen respectively!

end: end argument allows us to put something at the end of the line after it is printed. In simple words, it allows us to continue the line with " " or ',' or anything we want to put inside these quotes of the end.
It simply joins two different print statements using some string or even by space. Example:

import os
import flask

# print statement for printing strings
print("Harry is a programmer", end="**")

# Print statement with a literal
print(1+87)

#This will print "Harry is a programmer**88" on the screen 

Escape Sequences :

  • An Escape Sequence character in Python is a sequence of characters that represents a single character.
  • It doesn't represent itself when used inside string literal or character.
  • It is composed of two or more characters starting with backslash \ but acts as a single character. Example \n depicts a new line character.

Some more examples of escape sequence characters are shown below:

Commonly Used Escape Sequences:

Escape SequencesDescription 
\n Inserts a new line in the text at the point
\\Inserts a backslash character in the text at the point
\"

Inserts a double quote character in the text at that point

\'Inserts a single quote character in the text at that point
\t

Inserts a tab in the text at that point

\fInserts a form feed ln the text at that point
\r

Inserts a carriage return in the text at that point

\bInserts a backspace in the text at that point

Code file as described

#Please dont remove this line
"""
This is a
Multiline Comment
"""
"""
This is a comment
"""
# print("Subscribe CodeWithHarry now","Bhai video bhi like kar dena")
# print("next line")
# print("C:\'narry")
print("Harry is \n good boy \t1") #comment after statement

Comments

Popular posts from this blog

Using Modules & Pip In Python

  Sometimes we have to use someone else's code in our program because it saves us a lot of time. Today, we will learn a technique to use code that is not written by us but will enhance the quality of our program, save us time and energy, and of course, it is legal and free. Let us understand what utilities like modules and pip are, Module  – Module or library is a file that contains definitions of several functions, classes, variables, etc. which is written by someone else for us to use. Pip  – Pip is a package manager for Python i.e., pip command can be used to download any external module in Python. It is something that helps us to get code written by someone else. We can install a module in our system by using pip command : Open cmd or Powershell in your system. And then, type pip install module_name and press enter. Once you do that, the module will start downloading and will install automatically on your computer. Example, for installing flask I will do this: After p...

Beginner Friendly Full Python Tutorials(Teaser) | Python Tutorials For Absolute Beginners

  Python is one of the most famous and recent Programming Languages.  I took this initiative of starting this course as I always wanted to make it easy for people to learn Python. On top of that, I got a lot of requests to teach Python in depth. In this course, we will be learning Python from the very beginning to the advanced level, where we will be doing projects like Jarvis desktop assistant and an Indian Railways announcement system. Most importantly, I will be giving some quizzes and exercises to test your skills regularly. I will be providing questions, and then you have to try solving them. This Python tutorial will enhance your programming skills, and it will also help you become a pro programmer.  Technology is enhancing, and human beings always try to reduce their manual efforts. After learning Programming, one can reduce his/her manual efforts to accomplish his/her work on the machine.  Programming allows us to minimize manual work. With the help of progra...

What Is Programming and Why Python?

  Guido Van Rossum created the Python Programming language in February 1991. Python is an interpreted, high-level, general-purpose programming language. Python allows us to create a game, build web apps, do general-purpose scripting, etc. Before we dive into the details of Python, let's understand what the term Programming or Coding means? Programming helps human beings to reduce hours of manual efforts. In today's era, demand for programming is growing rapidly; i.e., there is a huge need for software developers and programmers in IT (Tech) Industries. To write code on any language, we need a friendly platform where we can write the code and can execute it. For this, we use IDEs. IDE – An IDE (Integrated Development Environment) is a software application that provides many comprehensive facilities to programmers for software or application development. Python is used by many of the best tech companies. Few of those companies are: Instagram Facebook Google Reddit Spotify Quora D...