Python Tutorials
- Python
- How to install Python?
- PIP
- How to Run Python Program
- Python Identifiers,Statement, Indentation and Comments
- Variable
- Data type
- Decision Making
- Python Loops
- Break,Continue, Pass
- Functions
- Predefine Functions
- Lambda Functions
- Variable Scope
- List
- Tuple
- Python Sets
- Python Dictionary
- Python String
- String Formating
- Input/Output
- File Handling (Input / Output)
- Iterators
- Python Modules
- Python Date
- Python JSON
- Classes and Objects
- Constructor
- Polymorphism
- Encapsulation
- inheritance
- Class or Static Variables in Python
- class method vs static method
- Abstraction
- Exception Handling
- MySql Python
- MySql Create Database
- MySql CRUD
- Django
What is Identifiers?
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). An identifier cannot start with a digit. Python does not allow punctuation characters such as @, $, and % within identifiers.
File name : index.php
Here are naming conventions for Python identifiers −
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strongly private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Lines and Indentation
Indentation is nothing but adding whitespaces before the statement when it is needed. Without indentation Python doesn't know which statement to be executed to next. Indentation also defines which statements belong to which block. If there is no indentation or improper indentation, it will display "IndentationError" and interrupt our code.
Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.
A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line.
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.
File name : index.py
Generally, four whitespaces are used for indentation and are preferred over tabs
for i in range(1,11):
print(i)
if i == 5:
break
File name : index.py
num = [1, 2, 3, 4, 5, 6, 7]
for i in num:
print(i)
if i==3:
break
print("End of for loop")
File name : index.php
Indentation can be ignored in line continuation, but it's always a good idea to indent. It makes the code more readable. For example:
if True:
print('Hello')
a = 5
Python Comments
Comments are very important while writing a program. They describe what is going on inside a program, so that a person looking at the source code does not have a hard time figuring it out.
In Python, we use the hash (#) symbol to start writing a comment.
File name : index.php
Multi-line comments
Another way of doing this is to use triple quotes, either ''' or """.
These triple quotes are generally used for multi-line strings. But they can be used as a multi-line comment as well.
"""This is also a
perfect example of
multi-line comments"""
Docstrings in Python
A docstring is short for documentation string. Python docstrings (documentation strings) are the string literals that appear right after the definition of a function, method, class, or module. Triple quotes are used while writing docstrings. For example:
File name : index.php
def double(num):
"""Function to double the value"""
return 2*num