You are on page 1of 8

Python Comments

Comments are used in programming to describe the purpose of the code block
This help other programmers the intent of the code
Comments can be used to make the code more readable.

Comments are completely ignored by the compilers and interpreters


When executing a program, the Python interpreter ignores the comments and only interprets
the code.

Python provides three kinds of comments including block comment, inline comment,
and documentation string.

To write a comment in Python, put the hash mark # before your comment:

# This is a comment
print("Hello World")
Python ignores everything after the hash mark and up to the end of the line. You
can insert them anywhere in your code, even inline with other code:

print("Hello World”) # This won't run

Python Multiline Comments

Python doesn’t have a way to write multiline comments as in languages such


as C, Java

# So you can't
# just do this
$
# in python

In the above example, the first line will be ignored by the program, but the other
lines will raise a Syntax Error.

In contrast, a language like Java will allow you to spread a comment out over
multiple lines quite easily:
/* You can easily
write multiline
comments in Java */
Everything between /* and */ is ignored by the program.

While Python doesn’t have native multiline commenting functionality, you can
create multiline comments in Python. There are two simple ways to do so.

The first way is simply by pressing the return key after each line, adding a new
hash mark and continuing your comment from there:

def multiline_example():
# Comment1
# Comment2
# Comment3
Each line that starts with a hash mark will be ignored by the program.

Another thing we can do is use multiline strings by wrapping your comment


inside a set of triple quotes:

"""
Comment1
Comment2
Comment3

"""
This is like multiline comments in Java, where everything enclosed in the triple
quotes will function as a comment.

While this gives you the multiline functionality, this isn’t technically a comment.
It’s a string that’s not assigned to any variable, so it’s not called or referenced by
your program. Still, since it’ll be ignored at runtime and won’t appear in the
bytecode, it can effectively act as a comment
Python Variables
A Python variable is a reserved memory location to store values.

Python Variable Types


Every value in Python has a datatype. Different data types in Python are
Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be
declared by any name or even alphabets like a, aa, abc, etc.

How to Declare and use a Variable


Creating variables

To define a variable, use the following syntax:

variable_name = value

Let see an example. We will define variable in Python and declare it as “x” and
print it.

x=100
print (x)

Re-declare a Variable
we can re-declare Python variables even after we have declared once.

Here we have Python declare variable initialized to x=10

Later, we re-assign the variable f to value “prakash”

# Declare a variable and initialize it


X=10
print f
# re-declaring the variable works
X=’prakash’
print x
Naming variables

name a variable, need to adhere to some rules. If you don’t, you’ll get an
error.

The following are the variable rules that you should keep in mind:

 Variable names can contain only letters, numbers, and underscores (_). They
can start with a letter or an underscore (_), not with a number.
 Variable names cannot contain spaces. To separate words in variables, you use
underscores for example  Employee_Name.
 Variable names cannot the same as keywords, reserved words, and built-in
functions in Python.
 Identifier name must not contain any white-space, or special character (!, @, #,
%, ^, &, *).
 Identifier names are case sensitive; for example, my name, and MyName is not
the same.
 Examples of valid identifiers: x123, _x, x_9, etc.
 Examples of invalid identifiers: 1a, x%4, x 9, etc.

The following guidelines help you define good variable names:

 Variable names should be concise and descriptive. For example,


the active_user variable is more descriptive than the au.

 Use underscores (_) to separate multiple words in the variable names.


 Variable names are case-sensitive

 Python also allows chained assignment, which makes it possible to assign


the same value to several variables simultaneously:
 >>>
 >>> a = b = c = 300
 >>> print(a, b, c)
 300 300 300
 The chained assignment above assigns 300 to the variables a, b,
and c simultaneously.
Object References
Python is a highly object-oriented language. In fact, virtually every item of data in a Python
program is an object of a specific type or class

Consider this code:

>>>

>>> print(300)
300
When presented with the statement print(300), the interpreter does the following:

 Creates an integer object


 Gives it the value 300
 Displays it to the console

You can see that an integer object is created using the built-in type() function:

>>>

>>> type(300)
<class 'int'>
A Python variable is a symbolic name that is a reference or pointer to an object. Once an
object is assigned to a variable, you can refer to the object by that name. But the data itself is
still contained within the object.

For example:

>>>

Object Identity
In Python, every object that is created is given a number that uniquely identifies it. It is
guaranteed that no two objects will have the same identifier during any period in which their
lifetimes overlap. Once an object’s reference count drops to zero and it is garbage collected,
as happened to the 300 object above, then its identifying number becomes available and may
be used again.

The built-in Python function id() returns an object’s integer identifier. Using


the id() function, you can verify that two variables indeed point to the same object:

>>>

>>> n = 300
>>> m = n
>>> id(n)
60127840
>>> id(m)
60127840
>>> m = 400
>>> id(m)
60127872
After the assignment m = n, m and n both point to the same object, confirmed by the fact
that id(m) and id(n) return the same number. Once m is reassigned to 400, m and n point to
different objects with different identities.

>>> m = 300
>>> n = 300
>>> id(m)
60062304
>>> id(n)
60062896
With the statement m = 300, Python creates an integer object with the value 300 and sets m as
a reference to it. n is then similarly assigned to an integer object with value 300—but not the
same object. Thus, they have different identities, which you can verify from the values
returned by id().

But consider this:

1405569120
Here, m and n are separately assigned to integer objects having value 30. But in this
case, id(m) and id(n) are identical!

>>> name = "Bob"


>>> Age = 54
>>> has_W2 = True
>>> print(name, Age, has_W2)
Bob 54 True
But this one is not, because a variable name can’t begin with a digit:

>>> 1099_filed = False


SyntaxError: invalid token
Note that case is significant. Lowercase and uppercase letters are not the same. Use of the
underscore character is significant as well. Each of the following defines a different variable:

>>>

>>> age = 1
>>> Age = 2
>>> aGe = 3
>>> AGE = 4
>>> a_g_e = 5
>>> _age = 6
>>> age_ = 7
>>> _AGE_ = 8

>>> print(age, Age, aGe, AGE, a_g_e, _age, age_, _AGE_)


1 2 3 4 5 6 7 8
There is nothing stopping you from creating two different variables in the same program
called age and Age, or for that matter agE. But it is probably ill-advised. It would certainly be
likely to confuse anyone trying to read your code, and even you yourself, after you’d been
away from it awhile.

variable a name that is descriptive enough to make clear what it is being used for. For
example, suppose

 Camel Case: Second and subsequent words are capitalized, to make word boundaries easier
to see. (Presumably, it struck someone at some point that the capital letters strewn
throughout the variable name vaguely resemble camel humps.)
 myCustomerName=”Prakash”

 Pascal Case: Identical to Camel Case, except the first word is also capitalized.
 MyCustomerName=”Prakash”

 Snake Case: Words are separated by underscores.


 My_Customer_Nmae=”Prakash”

 Snake Case should be used for functions and variable names.

 Pascal Case should be used for class names

Reserved Words (Keywords)

Python
Keywords      

False def If raise

None del import return

True elif In try

And else Is while

As except lambda with


Python
Keywords      

Assert finally nonlocal yield

Break for not

Class from or

Continue global pass

You can see this list any time by typing help("keywords") to the Python interpreter.
Reserved words are case-sensitive and must be used exactly as shown. They are all entirely
lowercase, except for False, None, and True.

Trying to create a variable with the same name as any reserved word results in an error:

>>>

>>> for = 3
SyntaxError: invalid syntax

You might also like