What are Keywords ?
- Keywords are reserved words and they have meanings.
- Keywords are defined by python language.
- Python have 35 standard keywords and 5 soft keywords.
- we cannot use keywords as variable name or function name.
If we use keyword as a variable name , it will throw a SyntaxError.
Example:
for=10
print(for)
Output:
SyntaxError: invalid syntax
Here , for is a python keyword , so we cannot use that as a variable name.
Python Keywords :
| Keyword | Purpose |
|---|---|
if |
Checks a condition. |
else |
Executes a block when the if condition is false. |
elif |
Checks another condition if the previous condition is false. |
for |
Used to create a for loop. |
while |
Used to create a while loop. |
break |
Terminates the loop immediately. |
continue |
Skips the current iteration and moves to the next iteration. |
pass |
Acts as an empty statement without performing any action. |
def |
Defines a function. |
return |
Returns a value from a function. |
class |
Defines a class. |
import |
Imports a module. |
from |
Imports specific members from a module. |
as |
Creates an alias for a module or imported member. |
try |
Defines a block for exception handling. |
except |
Handles exceptions. |
finally |
Executes code regardless of whether an exception occurs. |
raise |
Raises an exception explicitly. |
True |
Represents the Boolean true value. |
False |
Represents the Boolean false value. |
None |
Represents the absence of a value. |
and |
Returns True if both conditions are true. |
or |
Returns True if at least one condition is true. |
not |
Reverses the Boolean value of a condition. |
in |
Checks whether an element exists in a sequence or collection. |
is |
Checks whether two variables refer to the same object. |
lambda |
Creates an anonymous function. |
global |
Refers to a global variable. |
nonlocal |
Refers to a variable in an enclosing function. |
del |
Deletes a reference or item. |
assert |
Checks a condition and raises an AssertionError if false. |
with |
Used for resource management. |
async |
Defines an asynchronous function. |
await |
Waits for an asynchronous operation. |
yield |
Produces a value from a generator function. |
How to display all the keywords in python ?
Python provides a built-in module called keyword to identify Python keywords.
Example:
print("Standard keywords ")
for i in range(0, len(keyword.kwlist)):
print(i+1 ,"-" ,keyword.kwlist[i])
print("Total standard keywords:", len(keyword.kwlist))
print("Soft Keywords")
for i in range (0, len(keyword.softkwlist)):
print(i+1 ,"-",keyword.softkwlist[i])
print("Total soft keywords:", len(keyword.softkwlist))
Output:
Standard keywords
1 - False
2 - None
3 - True
4 - and
5 - as
6 - assert
7 - async
8 - await
9 - break
10 - class
11 - continue
12 - def
13 - del
14 - elif
15 - else
16 - except
17 - finally
18 - for
19 - from
20 - global
21 - if
22 - import
23 - in
24 - is
25 - lambda
26 - nonlocal
27 - not
28 - or
29 - pass
30 - raise
31 - return
32 - try
33 - while
34 - with
35 - yield
Total standard keywords: 35
Soft Keywords
1 - _
2 - case
3 - lazy
4 - match
5 - type
Total soft keywords: 5
How to check whether a word is a keyword or not ?
Example:
import keyword
print(keyword.iskeyword("if"))
print()
print(keyword.iskeyword("java"))
Output:
True
False
What are Identifiers?
- Identifiers are the names given to variables, functions, classes, modules, and other objects in Python.
- They are used to identify and access elements in a program.
- We create identifiers according to Python naming rules.
Example :
name="Raksha"
age=22
def display() :
print("Name:",name , "Age:" , age)
display()
Output:
Name: Raksha Age: 22
Here,
name and age --> variable identifier
display() --> function identifier
Rules for Naming Identifiers
1. Identifiers can contain Alphabetsc (a-z) (A-Z)
Name= "Raksha" //Valid
name="Rakshan" //Valid
print(name)
print(Name)
Both are valid identifiers.
2. Identifiers can contain numbers , but not start with numbers
Animal1="Tiger" // Valid
2Animal ="Lion" // Invalid
print(Animal1)
print(2Animal)
Here 2Animal is Invalid and it throw error like SyntaxError: invalid decimal literal
3. Identifiers can contain underscores (_)
Student_No = 101 //Valid
print(Student_No)
4 . Identifiers cannot contain special characters
Student$name="Rakshan" //Invalid
print(Student$name)
5 . Keywords cannot be used as a identifiers
if=20 //Invalid
animal="Dog" //Valid
if=20 is Invalid , because if is a Keyword
6 . Identifiers cannot contain spaces
animal name="Tiger" //Invalid

Top comments (0)