Python Basics: keywords- all you need to know.

Python Basics: keywords- all you need to know.

Keywords are fundamental concepts of any programming language. They help programmers to write code more efficiently by allowing them to use its functionality. Keywords are reserved words with special meanings and purposes. Different languages have different sets of keywords. In this article, you will learn the fundamentals of keywords in Python to write efficient code.

Python keywords are reserved words with certain functionality and meaning to perform specific operations. We can not use keywords as identifiers, variables, functions or class names in the program. If you try to use a keyword as a variable, then Python will raise a syntax error.

There are a total of 35 keywords in Python 3. Keywords are case sensitive and should use as it is. The list of all keywords in python is as below.

List of keywords in Python

kw_table.png The following code snippets will return a list of keywords in Python.

using keywords library

import_kw.png Print a list of keywords in Python IDE using the keyword module as shown in the above code.

using help function

help.png

Or using the help function as shown in the above code.

True, False, None

True, False, and None are value keywords. These are the only capitalized keywords; all other keywords are lowercase.

True, False — True/False is the boolean/Truth value which returned based on a condition.

True_or_False.png

True and False are the boolean instances that return a True if a given condition is satisfied otherwise return False. 1 and 0 represent True and False in Python.

None —None is a constant value in Python that represents an absence of values or null values. For example, a function without a return statement returns a None.

None.png

and, or, not, is, in

and, or, not are the logical operator keywords used on statements(either True or False) to perform Logical AND, Logical OR, and Logical NOT respectively.

and— return True only if both conditions are true otherwise return false.

or — return True if any one of the conditions is true, only if both conditions are false returns false.

not — invert the results, if the result of a condition is True return False and vice-versa.

And_or_not.png

AND, OR, NOT are logical operators used to check the multiple conditions.

is — In python is is used to check the object identity. It means that it is used to check the memory location of two variables. Returns True if variables point towards the same memory location.

is.png

The is operator is used to check object identity.

in in is the membership operator in Python. It is used to check if the value is in sequence(such as list, tuple, string etc.) or not. It is also used in for loop to iterate through a sequence.

in.png

The in is the membership operator to check if the value is in sequence or not.

if, else, elif, for, while, break, continue

These are the control statements in Python. if, else, elif executes a code of block based on a condition.

if_else.png

for, while — these are the looping control structures in Python. for and while are loops that are used to perform iterative tasks.

for_while.png

Break and continue keywords are used in loops.

break — The break statement terminates the loop containing it. Control of the program moves out of the for loop. The program counter will point towards the statements immediately after the for loop. if the break statement is inside a nested loop, the break will terminate the innermost loop.

break.png

continue — A continue statement skips the remaining lines of code inside a loop and starts the next iteration. Unlike break, continue will jump back to the top of the loop, rather than stopping it.

continue.png

def, lambda, class, pass, with

These are the essential structural keywords that use while creating functions and classes. Python will not be able to recognize that the particular block of code is a function or a class without these keywords.

def — def is used in function definitions while creating user-defined functions. The function is a reusable block of code to perform a specific task. It helps break the code into smaller pieces of code that execute as a unit. The function makes the code more organized and manageable.

function.png

lambda — lambda is used to create a single-line function also known as an anonymous function.

lambda.png

Class — class is used while creating classes in object-oriented Python. A class is a way to bind data and its associated functions together. it allows data hidden from external use.

class.png

Pass — The pass statement is used as a placeholder for a function or conditional body when you create a block of code without any statements. An empty function or class will raise an error, so we can use the pass statement to construct a body that does nothing.

pass.png

With — the with is used while working with files. with keyword ensures that the files are closed properly even when an exception occurs.

with.png

return, yield

The functionality of both keywords is the same to return a value from a function. The only difference between them is that the return keyword is used in the normal function to return a value at the end of the execution of the function and the yield keyword is used in the generator function to return a value.

return.png

The return statement terminates a function entirely, while the yield statement pauses the function saving all its states and later continues from where they left off.

yeild.png

import, from, as

from, import — from and import keywords are used to import modules in the current namespace of the program.

from_import.png

as — As is used to create an alias while importing modules. It means renaming a module while importing it.

as.png

try, except, raise, finally

These keywords are used while writing exceptions in the program. Exceptions are basically errors that occur during the execution of the program.

try, except — try, except are used to catch errors. The try statement specifies exception handlers and cleans up the code and except clause specifies one or more exception handlers.

try_except.png

ZeroDivisionError will raise if the value of the denominator is 0.

raise — Using the raise keyword we can explicitly raise an error.

raise.png

The code snippets check the value of n. if the value of n is 0, it will raise an exception ZeroDivisionError.

finally — finally is used with try…except block to close up resources or file streams. finally, ensure that the block of code executes no matter what.

del, global, nonlocal

del — The del keyword is used to delete variables or containers in the program.

del.png

global and nonlocal are used to describe the variable scopes in the program.

global — it is used to define a variable inside a function to global. To modify the value of the global variable inside the function, the variable must declare global in that function. Otherwise, a new local variable with the same name will be created.

global.png

the global variable is nothing but a variable declared in the main program outside of any function.

nonlocal — Nonlocal is similar to the global only difference is that it is used in nested function (function inside other function). The local variable of the outer function can only modify inside the inner function if it is declared as nonlocal. Otherwise, the local variable will be created in the nested function.

nonlocal.png

assert

assert — The assert is debugging keyword that is used for debugging purposes. It helps to find a bug in our assumptions. If the condition is False it will raise an assertion error and only continue execution if the condition is True.

asser.png

The assert will check the value of the variable entered by the user based on the condition. if the condition is true it does nothing. However, if the condition is false it will raise an assertion error with a message if provide.

async, await

These are keywords for asynchronous programming. These keywords are provided by the asyncio library in Python. They are used to write concurrent code in Python.

Thank you so much for reading this far. I hope this article helps you to understand the keywords and their uses in Python.

Did you find this article valuable?

Support Madhuri's blog by becoming a sponsor. Any amount is appreciated!