Professional Documents
Culture Documents
In Python, statements that are the same level to the right belong to the same
block. We can use four whitespaces to define indentation. Let's see the following
lines of code.
For Example:
Output:
Explanation:
In the above code, for loop has a code blocks and if the statement has its code
block inside for loop, both indented with four whitespaces. The last print()
statement is not indented; that's means it doesn't belong to for loop.
Comments in Python:
Comments are essential for defining the code and help us and other to
understand the code. By looking the comment, we can easily understand the
intention of every line that we have written in code. We can also find the error
very easily, fix them, and use in other applications.
In Python, we can apply comments using the # hash character. The Python
interpreter entirely ignores the lines followed by a hash character. A good
programmer always uses the comments to make code understandable. Let's see
the following example of a comment.
Types of Comment:
Python provides the facility to write comments in two ways- single line comment
and multi-line comment.
Marks = 90
We can also write a comment next to a code statement. Consider the following
example.
‘‘‘
This is an example
Of multi-line comment
Using triple-quotes
‘‘‘
Python Identifiers:
A variable name must start with either an English letter or underscore (_).
Python Variables:
Variable names can be a group of both the letters and digits, but they have
to begin with a letter or an underscore.
Identifier naming:
All the characters except the first character may be an alphabet of lower-
case(a-z), upper-case (A-Z), underscore, or digit (0-9).
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: a123, _n, n_9, etc.
Python does not bind us to declare a variable before using it in the application. It
allows us to create a variable at the required time.
We don't need to declare explicitly variable in Python. When we assign any value
to the variable, that variable is declared automatically.
print("John")
Output:
John
In the above print statement, we have created a string object. Let's check the type
of it using the Python built-in type() function.
type("John")
Output:
<class 'str'>
a = 50
a = 50
b=a
The variable b refers to the same object that a points to because Python does not
create another object.
Object identity:
In Python, every created object identifies uniquely in Python. Python provides the
guaranteed that no two objects will have the same identifier. The built-in id()
function, is used to identify the object identifier. Consider the following example.
Output:
We assigned the b = a, a and b both point to the same object. When we checked
by the id() function it returned the same number. We reassign a to 500; then it
referred to the new object identifier.
Variable Names:
We have already discussed how to declare the valid variable. Variable names can
be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and
underscore character(_). Consider the following example of valid variables names.
Output: