Comments and print function

Comments and Print function

Introduction

“Every success story requires a first step”

In this article, we will discuss the basic topics in python to get started. We will be discussing comments, and print functions in detail.

Comments

When we start to write code in python, it is always important to let users understand the code. This process of understanding code is made simple with help of comments. Whenever we write something as comments the compiler will ignore those lines from compiling. These comments are only for human understanding. 

Imagine, after becoming a great coder, you will have to write thousands of lines of code, during that time writing comments will become a great practice to precisely explain the code to everyone.

In python, there are two types of comments,

  • Single line comment
  • Multi-line comment / Doc String

Single line comment

In python, comments are written with help of the hash symbol (#). Lines to be commented on are always prefixed with #. But # is used to comment on that particular line alone.

Syntax:

#statement - this statement is a comment
Statement # this statement is not a comment

Multi-line comment / DocString

Python does not contain any official methods to do multi-line comments, but we can consider three double quotes(“””) as multi-line commenting as it serves the purpose. When you write multi-line comments in this format the interpreter compiles them but when we use to # the interpreter ignores them.

See also  Dictionary in python

Officially three double quotes are said as docstring in python. We will see in detail about docstring and their uses later.

Syntax

""" Docstring can also 
be written as multi-line comments """

We can also prefix every line we need to comment with hash which will also serve the purpose.

To comment or uncomment multiple lines simply select the desired code and press Ctrl+/ on windows or Cmd+/ on Mac.

Print function

The print function is used to print/display the output to the screen.

Syntax

print("statements") # whatever we write inside double quotes will be printed

We can use either single quotes or double quotes to wrap the text that you need to print. But, using both at the same time is prohibited.

If we need to print something as it is without processing we use single/double-quotes.

Example

print("5+6") #print 5+6 without any addition
print(5+6) # print 11 after adding the numbers
print("5"+"6") # prints 56 as this addition does concatenation

In the above code snippet,

  • Line1 prints 5+6 as the whole line is inside double quotes it considered them as a string and did not process.
  • Line2 prints 11 as there are no double quotes we consider them as two numbers and add them because of the + operator and print the result.
  • Line3, prints 56 as 5 and 6 are inside double quotes they are considered as string and + operator found between two string will do concatenation means joining instead of addition.

Empty print statement

If we are writing empty print statements, that will by default print a new line/ it will create an empty line for the user.

print() # this will print the newline

End parameter

The print function in python has an option to specify the compiler what to do after printing the line, by default it will print a new line, which means it will leave the cursor to the next line from the text printed. But we can explicitly change them using the end parameter

See also  Decision-making using python

Example

print("Hi", end=",")
print("How are you?")

In the above code snippet, 

  • Line 1 prints Hi and since we mentioned end to comma, the cursor will just put a comma and still be in the same line.
  • In line 2, the print statement prints how are you in the same line, and since there is no end parameter written it automatically goes to the next line.

Output

Hi, how are you?

Escape Sequence

To perform some special operations inside string we use escape characters. The backslash (\) is considered an escape character in python. 

For example, if I want to print a new line inside my print statement itself I can use \n whenever my compiler encounters \n it automatically makes the cursor go to the next line.

Some of the frequently used escape characters are,

\’Used to put single quotes in a string surrounded by single quotes
\”Used to put double quotes in a string surrounded by double quotes
\nUsed to insert a new line
\t Used to insert a tab space
\bUsed to insert a backspace
\\ Used to insert a backslash
\oooUsed to insert an octal value
\xhhUsed to insert hexadecimal value

Practice sheet

  1. Write a program to print about yourself in 5 different lines. And add a comment above them telling the program’s content.
  2. Write a single print statement to print the following pattern,

You can always use the comment box to discuss the answers for your practice sheets, doubts can also be asked in the same.

Conclusion

I hope, this article gives a clear idea about comments and print functions in python. 

See also  50+ Best python projects with source code [2023]

“Practice makes a man perfect”

So do not forget to practice the practice sheets for cent percent learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.