3.Identifiers,Variables and Keywords

 Identifiers

Identifiers are names used to identify variables, functions, classes, modules, and other objects in Python.

Rules for framing identifiers

1.Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _. 
Names like myClass, var_1 and print_this_to_screen, all are valid example.

2.An identifier cannot start with a digit. 
1variable is invalid, but variable1 is a valid name.

3.Keywords cannot be used as identifiers.
Eg: global = 1 is invalid

4.We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a@=0 is invalid

5.An identifier can be of any length.

Things to Remember
  • Python is a case-sensitive language. This means, variable X and x are not the same.
  • Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count=10 would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap.
  • Multiple words can be separated using an underscore, like this_is_a_long_variable.

Variables

A variable can be used to store a certain value or object. In Python, all numbers (and everything else, including functions) are objects. A variable is created through assignment. Their type is assigned dynamically.
Eg:
x='binu'
y=23
z=24.5
l=399379379379387983793773973977000102
use type(object) to know the type of the variable object
Eg: type(x)      type(y)            type(z)                    type(l)
<type 'str'>     <type 'int'>   <type 'float'>           <type 'long'>
use dir(object) to know the functions associated with the object
When we create a program, we often like to store values so that it can be used later. We use objects to capture data, which then can be manipulated by computer to provide information. By now we know that object/ variable is a name which refers to a value. It has a type and also store some value.

Variable can be mutable or immutable. A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place.

Things to Remember:
  • Variables are created when they are first assigned a value.
  • Variables type is determined by the value which is assigned.
  • Variables must be assigned a value before using them in expression,
  • Variables refer to an object and are never declared ahead of time.

Keywords

Keywords are reserved words that have a special meaning in Python. They are part of the syntax and cannot be used as identifiers.

We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.

In Python, keywords are case sensitive.

There are 36 keywords in Python 3.12. This number can vary slightly over the course of time.

All the keywords except True, False and None are in lowercase and they must be written as they are. The list of all the keywords is given below.

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

The following Python code will list all keywords

import keyword 
print(keyword.kwlist)

 ['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']

Summary
  • Identifiers: Names for variables, functions, etc. Must follow specific rules.
  • Variables: Containers for storing data values.
  • Keywords: Reserved words with special meanings in Python.

By understanding and properly using identifiers, variables, and keywords, you can write clear and effective Python code.

Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

46.Classes and Objects in Python- Accessors and mutators

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020