3. Naming conventions in python
ü What
is an identifier?
ü Why should we follow naming conventions?
ü Rules to define identifiers in Python:
ü Summary points about rules
ü Validating identifier names by taking examples
ü Python program total identifiers in simple table
ü Smart
suggestions while writing identifiers
ü Indentation
and Comments
ü Programs
3. Naming conventions
What is the identifier?
ü
A name in a python
program is called an identifier.
ü
This name can be,
o
class name
o
package name
o
variable name
o
module name
o
function name
o
method name
ü
Python developers
made some suggestions to the programmers regarding how to write identifiers in
program.
Why should we follow the naming
conventions?
ü
If we follow the
naming conventions, then the written code is,
o
Easy to understand.
o
Easy to read.
o
Easy to debug.
Rules to define identifiers in Python:
1. The only allowed characters to
write identifier in python are,
o
Alphabets, these can be either lower case or upper case.
o
Digits (0 to 9)
o
Underscore symbol (_)
ü
If we are using any
other symbol like ( $, !, -, etc) then
we will get a syntax error.
Program printing
variable name
Name demo1.py
student_id=101
print(student_id)
Output
101
|
Program printing the variable name which having symbols in name
Name demo2.py
$tudent_id=101
print($tudent_id)
Error
SyntaxError: invalid syntax
|
2. Identifier allowed digits, but the identifier should not start with a digit.
Program printing a variable name which having digits in the end of the name
Name demo3.py
student_id123=101
print(student_id123)
Output
101
|
Program printing
variable name which starts with digits, it is invalid
Name demo4.py
123tudent_id=101
print(123tudent_id)
Error
SyntaxError: invalid syntax
|
3. Identifiers are case sensitive.
Program To prove python is case
sensitive
Name demo5.py
a = 10
b = 20
print(A +
B)
Error NameError: name 'A' is not defined
|
4. We cannot use keywords as
identifiers.
Program printing
variable name given as keyword name, it is invalid
Name demo6.py
if = 10
print(if)
Error
SyntaxError: invalid syntax
|
5. Spaces are not allowed between
identifier.
Program spaces
not allowed between identifier
Name demo7.py
student id=101
print(student id)
Error
SyntaxError: invalid syntax
|
Summary points
1. The only allowed characters to
write identifier in python are,
o
Alphabets, these can be either lower case or upper case.
o
Digits (0 to 9)
o
Underscore symbol
(_)
ü
If we are using any
other symbol like ( $, !, -, etc) then
we will get syntax error.
2. Identifier allowed digits, but
identifier should not start with digit.
3. Identifiers are case sensitive.
4. We cannot use keywords as
identifiers.
5. Spaces are not allowed between
identifier.
Validate the below identifiers
ü
435student # invalid
ü
student564 # valid
ü
student565info # valid
ü
$tudent # invalid
ü
_student_info # valid
ü
class # invalid
ü
def # invalid
Commonly expected errors
1. SyntaxError: invalid syntax
Python program identifiers
1. class
|
ü class names should start with
upper case and remaining letters are in lower case.
ü If name having multiple
words, then every inner word should start with upper case letter.
ü Example: StudentInfo
ü Info: This rule is applicable for classes created by users only; the in-built class names used all are in lower-case.
|
2. package
3. module
4. variable
5. function
6. method
|
ü Names should be in lower case.
ü If name having multiple
words, then separating words with underscore (_) is good practice.
ü Example: one_two
|
7. Non-public
instance variables
|
ü Non-public instance variables
should begin with underscore (_), we can say private data
ü Example: _one
|
8. constants
|
ü Constants names should be
written in all capital letters.
ü If name having multiple
words, then separating words with underscore (_) is good practice.
ü Example: ONE_TWO
|
9. Non-accessible
entities
|
ü Some variables and functions
not accessible outside.
ü Those variables and function
names started with two underscores symbols.
ü Example: __init__(self)
|
Smart suggestions while writing identifiers
ü Be descriptive for
identifiers,
o
A
variable name should describe exactly what it contains.
Example
a = 10 #
valid but not recommended
student_id = 10 # valid and highly
recommended
o
A
function name should describe what exactly what kind of operation is performing.
Example
abc() #
valid but not recommended
sum() # valid and highly recommended
ü Don’t use
abbreviations unnecessarily.
o
Abbreviations
may be ambiguous and more difficult to read.
Example
- fourth_bentch_middle_student_id = 10 # valid but not recommended
- student_id = 10 # valid and highly recommended
Indentation and Comments
Indentation
ü
In Python, we need
to group the statements by using indentation.
Why indentation
ü
Indentation keeps
separate the group of statements.
ü
The recommended
indentation is 4 spaces.
ü
We must follow the
order of indentation otherwise we will get IndentationError
Program valid indentation
Name demo8.py
print(“statement one”)
print(“statement
two”)
print(“statement
three”)
output
statement one
statement two
statement three
|
Program Invalid indentation
Name demo9.py
print(“statement one”)
print(“statement two”)
print(“statement
three”)
Error
IndentationError: unexpected indent
|
Program Invalid indentation
Name demo10.py
print(“statement one”)
print(“statement two”)
print(“statement
three”)
Error
IndentationError: unexpected indent
|
Comments
ü
Comments are useful
to describe the code in an easy way.
ü
Python ignores
comments while running the program.
ü
To comment python
code, we can use hash symbol #
Program showing comments
in program
Name demo11.py
# This is Basic program in
python
print(“Welcome to python
programming”)
output
|