TeachingBee

Is Python Case Sensitive When Dealing With Identifiers

Is Python Case Sensitive When Dealing With Identifiers

This tutorial we will discuss about identifiers in python. We will also see the naming rules, best practices, and reserved keywords and classes for Python Identifiers. We will also answer some of the questions like Is Python Case Sensitive When Dealing With Identifiers and what is maximum length of identifier in python.

Let’s get started with Identifiers in Python.

What are Identifiers in Python?

An identifier is a user-defined name to represent the basic building blocks of Python like a variable, function, class, module, etc. The identifier is a combination of character digits and underscore. The identifier should start with a character or Underscore then use a digit. The characters are A-Z or a-z, an UnderScore ( _ ) , and digit (0-9). we should not use special characters ( #, @, $, %, ! ) in identifiers.

Examples of valid identifiers:

  • variable1
  • _abc2
  • _2_variable
  • pqr_3

Examples of invalid identifiers:

  • !abc1
  • 2pqr
  • 2_abc
  • variable#2

Rules for Naming Identifiers in Python

  1. A Python identifier can be a combination of lowercase/ uppercase letters, digits, or an underscore. The following are allowed in Identifiers in Python:
    • Lowercase letters (a to z)
    • Uppercase letters (A to Z)
    • Digits (0 to 9)
    • Underscore (_)
  2. An identifier cannot begin with a digit
  3. Special symbols like ( #, @, $, %, ! ) are not allowed in the identifier name.
  4. Keywords cannot be used as an identifiers in python.
    We will discuss more about keywords further in the article.

Is Python Case Sensitive When Dealing With Identifiers

Python is a case sensitive programming language. Thus, “Variable” and “variable” are two different identifiers in Python.Thus, if a variable is named ‘Name’, then an error will occur if the variable is called ‘name’. Thus, Python is Case Sensitive When Dealing With Identifiers

Name="Joseph"
print(name)

# Output: NameError: name 'name' is not defined

Maximum Length Of Identifier In Python

Python identifiers can be unlimited in length but as per PEP-8 you should limit identifier to a maximum of 79 character for better readability but there is no restriction.

Reserved Keywords And Classes In Python

Reserved Keywords

Keywords are predefined words and reserved words that can be used in Python to indicate special meanings. The keywords are used to specify the syntax of the code. Keywords cannot be used to identifier, function or name variables. With the exception of None,True and False all keywords in Python are written in lowercase. Python 3.7 contains 33 keywords.

Nonebreakexceptinraise
Falseawaitelseimportpass
andcontinueforlambdatry
Trueclassfinallyisreturn
asdeffromnonlocalwhile
asyncelififnotwith
assertdelglobaloryield
Reserved Keywords In Python

Reserved Classes

  1. Single leading underscore (_*): This identifier allows you to store the results of the last evaluation in your interactive interpreter. These results are stored within the module __builtin__. These variables are private and cannot be imported using “from module import *”.
  2. Double leading and trailing underscores (__*__): This notation is only used by system-defined names. These names are defined by the interpreter, and it’s implementations. This convention is not recommended for additional names.
  3. Leading double underscores (__*): These category names can be used in the context of a class description. These are rewritten to avoid name conflicts between private variables of base classes and derived classes.

Best Practices for Identifiers In Python

It is important to adhere to the rules but it is also important to follow these recommended practices.

  • Capital letters should be used for class names. For example Student, Person etc.
  • Uppercase should be used for any class names that contain multiple words. For example StudentsRecord, PersonDetails etc.
  • For variables, functions and module names, you should use small letters. For example, name, age, foo() etc.
  • If variables, functions, or module names contain multiple words, then you should separate them by an underscore(_). For example student_name, person_object etc.
  • Private variables can be starts with an underscore(_). For e.g. _private_details etc.
  • Avoid underscore at beginning and last of variable name as it is used by the python-built-in types.
  • If there are two underscores at the end of an identifier, it is likely that the identifier refers to a language-defined name. Avoid having underscores at both the beginning and end of your identifier name.
  • To clarify the intent of identifier names, keep them meaningful. For example moblie_number, permanent_address
  • It’s best to begin a function name with “is” if it returns boolean values. For example, isPalindrome(), isDigit() etc.
  • The length of an identifier name in python is unlimited. Keep it short and to the point. For example, the_employee_permanent_address can be better named as emp_permanent_addr.

Conclusion

In this article we discussed all about identifiers in python, naming rules and best practices to follow to make your code better readable.

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Complex Data Type In Python

Complex Data Type In Python

The complex() function is an essential building block in Python for working with complex numbers across scientific computing, signal processing, physics, engineering, and more fields. This article provides a comprehensive

list function python

Comprehensive Guide To List Function Python

Introduction The list() function in Python is used to create new lists from existing iterables like strings, tuples, dictionaries etc. The list type is one of the most commonly used

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In