Comments and Doc Strings¶
Let us understand how we can add doc strings and comments in Python.
- We use doc strings to provide instructions to use a function.
- Doc strings are provided immediately after function name. We can specify it as standard Python string.
- People typically enclose doc string with in 3 quotes (single or double). However, we can use one quote as well.
- Typically comments start with #
- We need to start a comment with # always in each line.
In [1]:
print('Hello World')
Hello World
In [2]:
print("Hello World")
Hello World
In [3]:
print("Hello World from 'itversity'")
Hello World from 'itversity'
In [4]:
print('Hello World from "itversity"')
Hello World from "itversity"
In [5]:
print("""Hello World from 'itversity'""")
Hello World from 'itversity'
In [6]:
print("""Hello World from "itversity" """)
Hello World from "itversity"
In [7]:
var1 = 'World'
var2 = 'itversity'
print(f'Hello {var1} from {var2}')
Hello World from itversity
In [8]:
print(f'Hello {var1} from {var2}'.format(var1='world', var2='itversity'))
Hello World from itversity
In [9]:
def foo():
"""Dummy function to demonstrate the usage of doc strings"""
# Returns nothing
"""Multi-line comment - first line
Multi-line comment - second line
"""
return
In [10]:
foo
Out[10]:
<function __main__.foo()>
In [11]:
foo?
Signature: foo() Docstring: Dummy function to demonstrate the usage of doc strings File: /tmp/ipykernel_556/997110409.py Type: function
In [12]:
help(foo)
Help on function foo in module __main__: foo() Dummy function to demonstrate the usage of doc strings