0% found this document useful (0 votes)
34 views7 pages

Python Variables, Identifiers, Keywords

The document provides an overview of variables, identifiers, and keywords in Python, explaining how variables are assigned values and can change types during execution. It details the rules for naming identifiers, including case sensitivity and restrictions on characters, as well as the significance of keywords that have predefined meanings in Python. Examples are provided to illustrate these concepts and their proper usage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Python Variables, Identifiers, Keywords

The document provides an overview of variables, identifiers, and keywords in Python, explaining how variables are assigned values and can change types during execution. It details the rules for naming identifiers, including case sensitivity and restrictions on characters, as well as the significance of keywords that have predefined meanings in Python. Examples are provided to illustrate these concepts and their proper usage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

K.C.S.

KASI NADAR COLLEGE OF ARTS & SCIENCE


(Belongs to [Link] Fund)

VARIABLES, IDENTIFIERS AND


KEYWORDS IN PYTHON
By,
[Link],
Assistant Professor,
Department Of Computer Science.
VARIABLES
• A variable is a name that is associated with a value.
• The assignment operator, = , is used to assign values to variables.
• An immutable value is a value that cannot be changed.
Eg: >>>num=10
>>> k=num
>>> print(k)
O/P : 10
• In Python the same variable can be associated with values of different
type during program execution.
Eg : var = 12 integer
var = 12.45 float
var = 'Hello' string
VARIABLE ASSIGNMENT AND KEYBOARD INPUT
• The value can come from the user by use of the input function.
Eg :
>>> name=input('what is your name : ')
O/P : what is your name : ABC
• All input is returned by the input function as a string type.
• For the input of numeric values, Python provides built-in type conversion
functions int() and float().
Eg :
>>> age=int(input('Enter your age : '))
O/P : Enter your age : 18
IDENTIFIER
• An identifier is a sequence of one or more characters used to name a given
program element.
• Python is case sensitive.
Eg : username is not same as userNAME.
• Identifiers may contain letters and digits, but cannot begin with a digit.
• The underscore character, _, is also allowed but it should not be used as
the first character.
• Spaces are not allowed as part of an identifier.
NAMING AN IDENTIFIER
Valid identifiers Invalid identifiers Reason invalid

totalSales ‘totalsales’ Quotes not allowed

totalsales Total sales Spaces not allowed

salesFor2020 2020sales Cannot begin with a digit

Sales_for_2020 _sales2020 Should not begin with an


underscore

• To check whether a given identifier is a keyword in python.


>>> 'exit' in dir(__builtins__)
O/P : True
>>> 'exit_program' in dir(__builtins__)
O/P : False
KEYWORDS IN PYTHON
• A keyword is an identifier that has predefined meaning in a programming
language.
• Cannot be used as a “regular” identifier.
Eg: >>> and=10
O/P : SyntaxError: invalid syntax
THANK YOU!!!

Common questions

Powered by AI

Python's use of keywords simplifies programming by providing predefined identifiers with specific meanings and functions, such as 'if', 'else', and 'for'. These cannot be used as ordinary identifiers to avoid conflicts and ensure the language's syntax rules are adhered to. Although this restriction limits naming flexibility, it simplifies understanding and maintaining code by avoiding ambiguity and misinterpretation of code functionality .

Python's assignment operator allows assignment of different types to the same variable during runtime, providing design flexibility. In large codebases, this means that developers can change variable roles dynamically, facilitating adaptation and reducing boilerplate code. However, it also introduces chaos if not carefully managed, as the dynamic typing can lead to complex debugging scenarios and maintenance challenges due to unpredictable variable states. Strategic use of type hints and documentation can mitigate these risks .

Python's case sensitivity means that identifiers with different cases are considered distinct (e.g., 'username' and 'userNAME' are separate identifiers). This necessitates precise adherence to naming conventions and consistency in case use to prevent errors. The main pitfall is unintentional naming conflicts and bugs arising from inconsistent case usage, potentially leading to incorrect variable binding and logic errors. Ensuring consistent use of case in naming conventions is crucial for code reliability and clarity .

Immutability in Python refers to the inability to change the actual value of an object after it has been created. Immutable objects, such as integers and strings, cannot be altered, so operations on such variables result in the creation of a new object and variable reassignment. This concept is important because it affects memory usage and performance, as well as how variables behave when used in functions or loops. Understanding immutability is crucial when managing object references and ensuring predictable program behavior .

An identifier in Python is a sequence of characters used to name program elements such as variables, functions, classes, etc. A variable, on the other hand, is a specific type of identifier that is associated with a value. The distinction is significant because while variables are used to store data that can change during program execution, identifiers as a whole are names that help in organizing and accessing the code elements systematically. This difference ensures clarity and prevents errors in scope and data management within the program .

Python manages user input using the input() function, which reads the information entered by a user and returns it as a string. For numeric input, this means explicit type conversion functions like int() or float() must be used to convert the string to a number type, as all input defaults to string. This ensures that arithmetic operations and numeric computations can be performed correctly on user-provided data .

Python allows a variable to be associated with values of different types at different times during program execution. A single variable can initially be assigned an integer, and later a string or a float without type declaration. This type flexibility, known as dynamic typing, simplifies writing of code by reducing the requirement for explicit type management, but it also imposes the need for careful handling of variables to prevent type-related errors during runtime .

Valid identifiers in Python must start with a letter (A-Z or a-z) or an underscore (_), followed by letters, underscores, or digits (0-9). They cannot begin with a digit, contain spaces, or use quotes. Underscore at the start means the identifier has specific conventions or implications (like weak private variables). Adhering to these rules is crucial for ensuring code readability, avoiding syntax errors, and maintaining compatibility with Python's syntax interpreter. Non-compliance can lead to invalid identifier creation and syntax errors during code execution .

Python's naming convention restrictions, such as forbidding spaces and starting identifiers with digits, contribute to improved code readability and error prevention. They enforce a uniform structure that helps developers quickly understand variable roles and types, minimizing confusion. Additionally, these rules prevent syntax errors related to misinterpretation by the parser, ensuring that code elements remain distinct and predictable, which is crucial in collaborative and complex coding environments .

An identifier must not coincide with any of the reserved keywords in Python, which have predefined meanings for the interpreter and cannot be used for naming variables or functions (e.g., 'if', 'while'). Differentiation matters because using keywords as identifiers would lead to syntax errors and disrupt the code execution flow, as keywords are integral to structure and control in Python programs. Proper differentiation is essential for creating valid and functional Python programs .

You might also like