0% found this document useful (0 votes)
14 views3 pages

Q4.3) Explain Keywords in Python With Examples.

Uploaded by

vasukommaraju369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Q4.3) Explain Keywords in Python With Examples.

Uploaded by

vasukommaraju369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PYTHON

Q4.3) Explain Keywords in Python with examples.


Answer:
Definition
 Keywords in Python are reserved words that have special meaning in the language.
 They cannot be used as identifiers (variable, function, or class names).
 Python uses keywords to define syntax and structure of the program.

Characteristics of Keywords
1. Reserved by the Python interpreter.
2. Always in lowercase.
3. Cannot be redefined or used as variable names.
4. Serve as the building blocks of Python programs.

Examples of Python Keywords


Python has 36–37 keywords (depending on version). Here’s the full list categorized by
functionality:

1. Boolean & Constants (3 Keywords)

Keyword Meaning / Use


True Boolean value TRUE
False Boolean value FALSE
None Represents null / no value

2. Logical Operators / Conditionals (7 Keywords)

Keyword Meaning / Use


if Conditional statement
elif Else if for multiple conditions
else Alternative branch for if
and Logical AND operator
or Logical OR operator
not Logical NOT operator
is Checks object identity

3. Loops / Iteration (5 Keywords)

Keyword Meaning / Use


for Loop through a sequence
while Loop until condition is True
break Exit loop immediately
continue Skip current iteration of loop
pass Null statement (does nothing)

TEJA KOTAMRAJU
Page 1
PYTHON
4. Functions / Classes (5 Keywords)

Keyword Meaning / Use


def Define a function
return Return a value from function
lambda Define anonymous function
class Define a class
nonlocal Refer to variable in outer but non-global scope

5. Exception Handling / Error Management (4 Keywords)

Keyword Meaning / Use


try Start a block to catch exceptions
except Handle exceptions
finally Execute code regardless of exception
raise Raise an exception manually

6. Modules / Import (3 Keywords)

Keyword Meaning / Use


import Import a module
from Import specific items from module
as Give alias to module or object

7. Scope / Variable Declaration (3 Keywords)

Keyword Meaning / Use


global Declare a variable as global
nonlocal Refer to variable in outer but non-global scope
del Delete variable or object

8. Context Managers (1 Keyword)


Keyword Meaning / Use
with Used for context manager / resource handling

9. Asynchronous Programming (2 Keywords)

Keyword Meaning / Use


async Define asynchronous function
await Wait for asynchronous operation

10. Iterators / Generators (1 Keyword)

Keyword Meaning / Use


yield Used to return value from generator function

TEJA KOTAMRAJU
Page 2
PYTHON
11. Assertions / Debugging (1 Keyword)

Keyword Meaning / Use


assert Debugging aid – tests condition

How to View All Keywords in Python


Python provides a built-in module keyword to list all keywords.
import keyword

print("Python Keywords:")
print(keyword.kwlist)
Output (Python 3.11 example):
['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']

Rules for Using Keywords


1. Cannot be used as variable names.
2. if = 10 # ❌ Error: cannot use keyword as identifier
3. Keywords are case-sensitive (If ≠ if).
4. Must be used according to their purpose in Python syntax.

Note:-
 Keywords are essential building blocks of Python programs.
 They define the syntax, flow, and structure of the code.
 Correct usage of keywords ensures error-free and readable code.

TEJA KOTAMRAJU
Page 3

You might also like