0% found this document useful (0 votes)
19 views

Keywords in Python

The document discusses the various keywords in the Python programming language, outlining 35 unique keywords grouped into categories like value, operator, control flow, iteration, structure, exception handling, asynchronous programming and variable handling keywords. It provides details on the usage and purpose of each keyword.

Uploaded by

nishagarghnd825
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Keywords in Python

The document discusses the various keywords in the Python programming language, outlining 35 unique keywords grouped into categories like value, operator, control flow, iteration, structure, exception handling, asynchronous programming and variable handling keywords. It provides details on the usage and purpose of each keyword.

Uploaded by

nishagarghnd825
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Keywords in Python are reserved

words that can not be used as a


variable name, function name, or
any other identifier.
35 Unique Python Keywords
Value Keywords (3): True, False, None
Operator Keywords (5): and, or, not, in, is
Control Flow Keywords (3): if, elif, else
Iteration Keywords (5): for, while, break, continue, else
Structure Keywords(6): def, class, with, as, pass, lambda
Returning Keywords (2): return, yield
Import Keywords (3): import, from, as
Exception-Handling Keywords (6): try, except, raise,
finally, else, assert
Asynchronous Programming Keywords (2): async, await
Variable Handling Keywords (3): del, global, nonlocal
Python code to view list of keywords
Value Keywords: True, False, None
• True and False are truth values in Python. They
are the results of comparison operations or
logical (Boolean) operations in Python.
None keyword
• None is a special constant in Python that
represents the absence of a value or a null value.
Operator Keywords (5): and, or, not, in, is
• and, or, not are the logical operators in
Python. and will result into True only if both the
operands are True.
• or will result into True if any of the operands is True.
• not operator is used to invert the truth value.
• The in keyword is used to check if a value is present in a
sequence (list, range, string etc.). The in keyword is also
used to iterate through a sequence in a for loop.
• is keyword is used in Python for testing object
identity. is keyword is used to test if the two variables
refer to the same object. It returns True if the objects
are identical and False if not.
Control Flow Keywords (3): if, elif, else

• if, else, elif are used for conditional


branching or decision making.
• When we want to test some condition and
execute a block only if the condition is true,
then we use if and elif.
• elif is short for else if.
• else is the block which is executed if the
condition of if and elif are false.
Iteration Keywords (5): for, while, break, continue, else

• for is used for looping. Generally we use for when


we know the number of times we want to loop.
While keyword
• while is used for looping in Python.
• The statements inside a while loop continue to
execute until the condition for the while loop
evaluates to False or a break statement is
encountered.
break, continue keywords
• break and continue are used
inside for and while loops to alter their
normal behavior.
• break will end the smallest loop it is in
and control flows to the statement
immediately below the loop.
• continue causes to end the current
iteration of the loop, but not the whole
loop.
Structure Keywords(6): def, class, with, as, pass,
lambda

• def is used to define a user-defined


function.
• Function is a block of related statements,
which together does some specific task.
It helps us organize code into
manageable chunks and also to do some
repetitive task.
class keyword
• class is a collection of related attributes and
methods that try to represent a real-world
situation.
with keyword
• The with statement in Python helps you with
resource management. It ensures you don’t
accidentally leave any resources open.
• A common example of using
the with statement is opening a file.
• The with statement automatically closes the
file after you’ve completed writing it.
as keyword
• as is used to create an alias while importing a
module. It means giving a different name (user-
defined) to a module while importing it.
• for example, Python has a standard library
called matplotlib and pyplot is one of its
module.
• We may use as keyword to reduce code size i.e.,
“matplotlib.pyplot” can be replaced by “rtu”
to reduce code size using as keyword.
Try this example in edublocks
pass keyword in python
• pass is a null statement in Python. Nothing
happens when it is executed. It is used as a
placeholder.
• Sometimes, pass is used when the user
doesn’t want any code to execute. So user
can simply place pass where empty code is
not allowed, like in loops, function
definitions, class definitions, or in if
statements. So using pass statement user
avoids this error.
lambda keyword
• lambda is used to create an
anonymous function (function with no
name).
• It is an inline function that does not
contain a return statement.
• It consists of an expression that is
evaluated and returned.
return keyword
• return statement is used inside a function to
exit it and return a value.
yield keyword
• yield is used inside a function like
a return statement. But yield returns a
generator function. Generator functions act
as iterators which can be used as a value in
one of the iterations of the loop.
Import Keywords (3): import, from, as

• The import keyword is used to


import other modules into a Python
script.
• The from keyword is used to import
only a specified section from a
module.
Exception-Handling Keywords (6): try, except,
raise, finally, else, assert
• Exceptions are basically errors that suggests
something went wrong while executing our
program.
• The try block is used to check some code for errors i.e.
the code inside the try block will execute when there is
no error in the program.
• Whereas the code inside the except block will execute
whenever the program encounters some error in the
preceding try block.
• The final block always executes after normal
termination of try block or after try block terminates
due to some exceptions.
raise keyword
• The raise keyword is used to raise an exception.
• You can define what kind of error to raise, and the
text to print to the user.
assert keyword
• assert keyword is used for debugging
purposes.
• While programming, sometimes we wish to
know the internal state or check if our
assumptions are true. assert helps us do
this and find bugs more conveniently.
• assert is followed by a condition.
• If the condition is true, nothing happens. But
if the condition is false, AssertionError is
raised.
Asynchronous Programming Keywords (2):
async, await

• The async and await keywords are provided


by the asyncio library in Python. They are
used to write concurrent code in Python.
• the async keyword specifies that the
function will be executed asynchronously.
• The await keyword makes the program wait
for time specified in seconds.
Variable Handling Keywords (3): del, global, nonlocal

• The del keyword is used to delete objects. In


Python everything is an object, so the del keyword
can also be used to delete variables, lists, or parts
of a list etc.
global keyword
• A global keyword is a keyword that allows a user
to modify a variable outside of the current scope.
• It is used to create global variables in python from
a non-global scope i.e., inside a function.
nonlocal keyword
• The nonlocal variables are present in a
nested block.
• A keyword nonlocal is used and the value
from the nearest enclosing block is taken.
• If we change the value of a nonlocal variable,
the changes appear in the local variable.
The output will be "nonlocal" both the times as the
value of a has been changed by the inner function.

You might also like