Professional Documents
Culture Documents
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.
Python provides three kinds of comments including block comment, inline comment,
and documentation string.
# 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:
# 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.
"""
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.
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.
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.
>>>
>>> print(300)
300
When presented with the statement print(300), the interpreter does the following:
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.
>>>
>>> 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().
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!
>>>
>>> age = 1
>>> Age = 2
>>> aGe = 3
>>> AGE = 4
>>> a_g_e = 5
>>> _age = 6
>>> age_ = 7
>>> _AGE_ = 8
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”
Python
Keywords
Class from or
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