Python Identifiers and Keywords- Basic Syntax
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).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Name and name are two different identifiers in Python.
Here are naming conventions for Python identifiers:
For example:
Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example:
A comment can appear on a line by itself or they can also be at the end of line.
Example
or
For adding multi-line comment in a program, we can:
1. “”” Calculating area of a rectangle “””
2. “”” Calculating area of
a rectangle “””
We should use as many useful comments as we can, to explain
*Any assumptions made
*important details or decisions made in the program. This will make program more readable. We already know the importance of comments (documented in the program).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Name and name are two different identifiers in Python.
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.
Keywords
They are the words used by Python interpreter to recognize the structure of program. As these words have specific meaning for interpreter, they cannot be used for any other purpose.Lines and Indentation
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.For example:
|
Multi-Line Statements
|
|
Quotation in Python
|
Comments
As the program gets bigger, it becomes difficult to read it, and to make out what it is doing by just looking at it. So, it is good to add notes to the code, while writing it. These notes are known as comments. In Python, comment start with ‘#’ symbol. Anything written after # in a line is ignored by interpreter, i.e. it will not have any effect on the program.A comment can appear on a line by itself or they can also be at the end of line.
Example
|
|
- i) Place '#' in front of each line, or
- ii) Use triple quoted string. They will only work as comment, when they are not being used as docstring. (A docstring is the first thing in a class/function /module, and will be taken up in details when we study functions).
1. “”” Calculating area of a rectangle “””
2. “”” Calculating area of
a rectangle “””
We should use as many useful comments as we can, to explain
*Any assumptions made
*important details or decisions made in the program. This will make program more readable. We already know the importance of comments (documented in the program).
No comments: