Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
1. Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the rest
of the line:
Example
print("Hello, World!") #This is a comment
A comment does not have to be text that explains the code, it can also be
used to prevent Python from executing code:
Example
#print("Hello, World!")
print("Cheers, Mate!"
Multiline Comments
,Python does not really have a syntax for multiline comments.
To add a multiline comment you could insert a # for each line:
Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Or, not quite as intended, you can use a multiline string.
Since Python will ignore string literals that are not assigned to a variable, you
can add a multiline string (triple quotes) in your code, and place your
comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
As long as the string is not assigned to a variable, Python will read
the code, but then ignore it, and you have made a multiline comment.
Python keywords
Every language contains words and a set of rules that would make a
sentence meaningful. Similarly, in Python programming language,
there are a set of predefined words, called Keywords which along
with Identifiers will form meaningful sentences when used
together. Python keywords cannot be used as the names
of variables, functions, and classes.
In this article, we will learn about Python keywords and how to use
them to perform some tasks.
Python Keywords
Keywords in Python are reserved words that can not be used as a
variable name, function name, or any other identifier.
List of Keywords in Python
,Keywor Descriptio Keywor Descriptio Keywor Descriptio
d n d n d n
Represents an
expression It is a non-
and It is a Logical False nonlocal
that will local
Operator
result in not variable
being true.
It is used to It is a
as finally It is used with not
create an Logical
exceptions
alias name Operator
It is a
assert It is used for for It is used to or Logical
debugging create Loop
Operator
pass is used
when the
To import
break Break out a from pass user doesn’t
specific parts
Loop want any
of a module
code to
execute
It is used to raise is used
class It is used to global declare a raise to raise
define a class global exceptions or
variable errors.
continu Skip the next To create a return is
iteration of a if Conditional return used to end
e
loop Statement the execution
Represents
It is used to It is used to an
def define the import import a True expression
Function module that will
result in true.
It is used to
It is used to Try is used
del is test if two try
delete an to handle
variables are
object errors
equal
, Keywor Descriptio Keywor Descriptio Keywor Descriptio
d n d n d n
To check if a While Loop
Conditional
value is is used to
elif statements, in while
present in a execute a
same as else-
Tuple, List, block of
if
etc. statements
with
Used to
It is used in a statement is
else lambda create an with
conditional used in
anonymous
statement exception
function
handling
yield
try-except is keyword is
except used to None It represents a yield used to
handle these null value create a
errors generator
function
Getting the List of all Python keywords
We can also get all the keyword names using the below code.
Python3
import keyword
# printing all keywords at once using "kwlist()"
print("The list of keywords is : ")
print(keyword.kwlist)
Output:
The list of keywords are:
['False', 'None', 'True', 'and', 'as', 'assert', 'async',
'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']