0% found this document useful (0 votes)
3 views

4. Python - Naming Conventions

The document outlines Python naming conventions, emphasizing the importance of following these guidelines for identifiers such as variables, functions, and classes to ensure code readability and maintainability. It details specific rules for creating valid identifiers, including restrictions on starting characters, case sensitivity, and the prohibition of keywords and spaces. Additionally, it covers the use of comments in Python programming, explaining single and multi-line comments.

Uploaded by

forunaveenadds
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

4. Python - Naming Conventions

The document outlines Python naming conventions, emphasizing the importance of following these guidelines for identifiers such as variables, functions, and classes to ensure code readability and maintainability. It details specific rules for creating valid identifiers, including restrictions on starting characters, case sensitivity, and the prohibition of keywords and spaces. Additionally, it covers the use of comments in Python programming, explaining single and multi-line comments.

Uploaded by

forunaveenadds
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Science – Python Naming Conventions

4. PYTHON – NAMING CONVENTIONS

Table of Contents

1. Identifier........................................................................................................ 2
2. Why should we follow naming conventions? ............................................... 2
3. Points to follow for identifiers in Python ...................................................... 3
# Point 1 ......................................................................................................... 3
# Point 2 ......................................................................................................... 4
# Point 3 ......................................................................................................... 5
# Point 4 ......................................................................................................... 6
# Point 5 ......................................................................................................... 6
Examples......................................................................................................... 7
Common error ................................................................................................... 7
4. Python identifiers table ................................................................................. 8
5. Comments in program................................................................................. 10
Purpose of comments ................................................................................... 10
1. Singe line comments ................................................................................. 10
2. Multi line comments ................................................................................. 11

1|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

4. PYTHON - NAMING CONVENTIONS

1. Identifier

✓ A name in a python program is called identifier.


✓ This name can be,

o Package name
o Module name
o Variable name
o Function name
o Class name
o Method name

✓ Python creator made some suggestions to the programmers regarding


how to write identifiers in a program.

2. Why should we follow naming conventions?

✓ While writing the program if we follow the naming conventions then the
written code is,

o Easy to understand.
o Easy to read.
o Easy to debug.

2|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

3. Points to follow for identifiers in Python

✓ We need to follow few points to define an identifiers,

# Point 1

✓ While writing an identifier we can use,

o Alphabets, either upper case or lower case


o Numbers from 0 to 9
o Underscore symbol (_)

✓ If we are using any other symbol then we will get syntax error.

Program Creating a valid identifier


Name demo1.py

student_id = 101
print(student_id)

Output
101

Program Creating an invalid identifier


Name demo2.py

$tudent_id = 101
print($tudent_id)

Error

SyntaxError: invalid syntax

3|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

# Point 2

✓ We can write an identifier with number but identifier should not start
with digit.

Program Creating a valid identifier


Name demo3.py

student_id123 = 101
print(student_id123)

Output
101

Program Creating an invalid identifier


Name demo4.py

123tudent_id = 101
print(123tudent_id)

Error
SyntaxError: invalid decimal literal

4|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

# Point 3

✓ Identifiers are case sensitive.

Program Creating a valid identifier


Name demo5.py

value = 10
print(value)

Error
10

Program Identifier is a case sensitive


Name demo5.py

value = 10
print(VALUE)

Error
NameError: name 'VALUE' is not defined

5|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

# Point 4

✓ We cannot use keywords as identifiers.

Program We should not use keywords to create an identifiers


Name demo6.py

if = 10
print(if)

Error
SyntaxError: invalid syntax

# Point 5

✓ Spaces are not allowed in between the identifier.

Program Spaces not allowed between identifier


Name demo7.py

student id = 101
print(student id)

Error
SyntaxError: invalid syntax

6|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

Examples

✓ 435student # invalid
✓ student564 # valid
✓ student565info # valid
✓ $tudent # invalid
✓ _student_info # valid
✓ class # invalid
✓ def # invalid

Common error

✓ SyntaxError: invalid syntax

7|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

4. Python identifiers table

✓ This table we can understand while studying upcoming topics.

Identifier Conventions to follow for identifiers

1. class ✓ In python, a class name should start with upper


case and remaining letters are in lower case.
✓ If name having multiple words, then every nested
word should start with upper case letter.

o 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 ✓ Names should be in lower case.


✓ If name having multiple words, then separating
4. variable words with underscore (_) is good practice.

5. function o Example: student_id

6. method

7. Non-public ✓ Non-public instance variables should begin with


instance variables underscore (_), we can say private data.

o Example: _balance

8. constants ✓ Names should be in upper case.


✓ If name having multiple words, then separating
words with underscore (_) is good practice.

8|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

o Example: IN_PROGRESS

9. Non-accessible ✓ Few variables, class constructors (topic in object


entities oriented programming) names having two
underscores symbols starting and ending

o Example: __init__(self)

9|Page 4.PYTHON-NAMING CONVENTIONS


Data Science – Python Naming Conventions

5. Comments in program

✓ There are two types of comments

1. Single line comments


2. Multi line comments

Purpose of comments

✓ Comments are useful to describe about the code in an easy way.


✓ Python ignores comments while running the program.

1. Singe line comments

✓ By using single line comment, we can comment only a single line.


✓ To comment single line, we need to use hash symbol #

Program A program with single line comment


Name demo8.py

#This is Basic program in python


print("Welcome to python programming")

output
Welcome to python programming

10 | P a g e 4.PYTHON-NAMING CONVENTIONS
Data Science – Python Naming Conventions

2. Multi line comments

✓ By using multi line comment we can comment multiple lines.


✓ To comment multiple lines, we need to use triple double quotes symbol.

Program A program with multi line comments


Name demo9.py

"""Author Daniel
Project Python project
Location Bengaluru"""

print("Welcome to python programming")

output
Welcome to python programming

11 | P a g e 4.PYTHON-NAMING CONVENTIONS

You might also like