Identifiers, Keywords, and Data Types
Identifiers
Definition: An identifier is a name given to a variable, function, class, module, or other object in Python. It serves as a symbolic reference to a value or entity stored in memory.
Identifiers are fundamental to programming because they allow programmers to refer to data and operations using meaningful names rather than memory addresses.
Rules for Defining Identifiers
Python has strict rules for valid identifiers. Violating these rules results in a SyntaxError.
- Alphabet Symbols: Identifiers can contain alphabet symbols (both uppercase and lowercase: A–Z, a–z), digits (0–9), and the underscore symbol (_).
- Case Sensitivity: Python identifiers are case-sensitive. The names total, Total, and TOTAL represent three different identifiers.
- First Character Rule: The first character of an identifier must be an alphabet letter or an underscore. It cannot begin with a digit.
- No Special Characters: Identifiers cannot contain special symbols such as $, @, #, %, &, *, or !.
- Reserved Words: Identifiers cannot be Python reserved keywords (such as if, for, while, class, def).
- No Length Limit: Python does not impose a maximum length on identifiers, but excessively long names are discouraged for readability.
Python Program:
Code Explanation: The program demonstrates valid identifier naming conventions. Attempting to use invalid identifiers would result in syntax errors. The convention in Python is to use lowercase with underscores for variable names (snake_case) and capitalized words for class names (PascalCase).
Conventions for Identifiers
While not enforced by the interpreter, the Python community follows these conventions:
- Variables and Functions: Use lowercase with underscores (student_name, calculate_total).
- Constants: Use uppercase with underscores (MAX_VALUE, PI).
- Classes: Use PascalCase (StudentRecord, BankAccount).
- Private Variables: Prefix with a single underscore (_balance).
- Strongly Private Variables: Prefix with double underscores (__password).
- Magic Methods: Surround with double underscores (__init__, __str__).
Reserved Words (Keywords)
Definition: Reserved words, also known as keywords, are predefined words in Python that have special meanings and cannot be used as identifiers.
Python 3.x contains 35 reserved keywords:
False, None , True , and , as ,assert ,async, await, break,class,continue, def,del,elif,else,except,finally,for,from,global,if,import,in,is,lambda,nonlocal,not,or,pass,raise,return,try,while,with,yield
Important Points to Remember
- All keywords except True, False, and None are written in lowercase.
- Keywords cannot be redefined or used as variable names.
- The async and await keywords were added in Python 3.5 for asynchronous programming.
Fundamental Data Types
- int : Integer values
- float : Floating-point decimal values
- complex :Complex numbers with real and imaginary parts
- bool :Boolean values (True or False)
- str : Strings (sequences of characters)